DATA-DOG / go-txdb

Immutable transaction isolated sql driver for golang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

does this support orm?

smiletrl opened this issue · comments

Hey, this library is working well with https://github.com/jmoiron/sqlx, but it seems not working with orm, such as https://github.com/gobuffalo/pop and https://github.com/jinzhu/gorm. Is there any workaround that developer can implement to use this library with these orms? Thanks!

Hi, probably frameworks are still bound by dsn used. see this example. I'm quite certain at least GORM should support it. But in general this library is intended for standard library users, if framework uses it, it should be fine.

Thanks! Let me try.

Hey, I think this is working with orms. The issue I experienced now is I'm going to connect to the same database using sqlx and pop orm in the same time inside multiple single tests. Then I kind of can't make it work together to isolate the two connections inside one single test from other single tests.

The biggest problem now is when I call defer DB.Close(), it will close all the connections for all single tests with different txname. Anyway, I guess this is not this project's issue. Close this one. Thanks for your help.

You can use two different frameworks as long as it gets same dsn for txdb connection, it would be within the same transaction

Yeah, my question is something like this. We will call defer db.Close() two times, and one close might break the other connection:

txName := "tx_name"
dsn := fmt.Sprintf("%s@(%s:%s)/%s", user, host, port, dbName)
txdb.Register(txName, "mysql", dsn)

popdb, err := pop.NewConnection(&deets)
popdb.Dialect.Details().Dialect = txName
err = popdb.Open()
defer popdb.Close()

sqlxdb, err := sqlx.Connect("txName", "random string")
// Close again. Or maybe this one is not necessary?
defer sqlxdb.Close()

Out of curiosity, the second parameter passed to sql.Connect() is not important, and can be any string, as long as the first driver name is the txName. Is that right? pop kind of difficult to set up the second parameter, so just want to confirm.

The DSN string parameter for txdb has an importance. It serves as an identifier for unique transaction.
For example you register txdb txdb.Register("tx_mysql", "mysql", "real_mysql_dsn_string")

Then you tell your database framework to use tx_mysql as a driver. sqlxdb, err := sqlx.Connect("tx_mysql", "tx1"). What txdb driver does, is start a transaction and use that for connection on your sqlxdb. Txdb marks that there is one connection under tx1 name.

If you would close that database, it would rollback transaction since there is only 1 connection used.

But if you open another db, lets say different framework, but you still use tx1 as your txdb driver dsn string. then it would use same connection sqlxdb using and increment the counter for tx1 to 2. If you close one db, it will not rollback that transaction just yet, but just decrement the counter.

Guess you got the point now. So closing both databases and using same DSN is necessary if you want them both to operate on that same transaction.

For instance if you run separate tests in parallel, it makes sense to differenciate that DSN so it could use a separate transaction.

Thanks for the detailed explanation!