GithubHelp home page GithubHelp logo

Comments (4)

yedf2 avatar yedf2 commented on May 27, 2024

现在就已经支持使用gorm的事务进行处理,参见: https://dtm.pub/ref/sdk.html#orm

from dtm.

yeyudekuangxiang avatar yeyudekuangxiang commented on May 27, 2024

现在就已经支持使用gorm的事务进行处理,参见: https://dtm.pub/ref/sdk.html#orm

这个我看到了,不过我们是UserModel里面有一个*gorm.DB字段。如果在UserModel初始化的时候传给它一个已经开启了事务的*gorm.DB,然后再在UserModel里面使用 u.db.Begin()就会报错。
如果我们使用gorm的事务嵌套就可以了,但是需要dtm提供一个不自动commit/rollback的BranchBarrier.Call方法,也就是我上面说的简单的把现有的Call方法拆成两个方法,一个自动commit/rollback,一个由使用者自己去处理commit/rollback,更加灵活一点

func TestName(t *testing.T) {
	gormDB,_:=gorm.Open(mysql.Open("dsn"))
	//正常情况
	um:=UserModel{db: gormDB}
	um.Insert(User{})
	
	//异常情况 报错 invalid transaction
	gormTx:=gormDB.Begin()
	um2:=UserModel{db: gormTx}
	um2.Insert(User{})
}

type User struct {
	Id int64
}
type UserModel struct {
	db *gorm.DB
}

func (u UserModel) Insert(v User) error {
	tx := u.db.Begin()
	msg := dtmcli.NewMsg("server", "gid")
	return msg.DoAndSubmit("", func(bb *dtmcli.BranchBarrier) error {
		return bb.Call(tx.Statement.ConnPool.(*sql.Tx), func(tx *sql.Tx) error {
			_, err := tx.Exec("insert into user values (?)", v.Id)
			return err
		})
	})
}

from dtm.

yedf2 avatar yedf2 commented on May 27, 2024

感觉你的这个场景需求会不会太小众了?为什么一定要自己开启和结束事务,希望能够详细说明一下为什么需要支持这种场景

from dtm.

yeyudekuangxiang avatar yeyudekuangxiang commented on May 27, 2024

应该是我考虑的有问题,不好意思,感谢回答

from dtm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.