Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

database/gdb: how to use nesting Transaction in two dao of two different group #3648

Open
fr4nkhe opened this issue Jun 18, 2024 · 0 comments

Comments

@fr4nkhe
Copy link

fr4nkhe commented Jun 18, 2024

What do you want to ask?

I have two dao of two different group

func NewOrderDao() *OrderDao {
	return &OrderDao{
		group:   "shop",
		table:   "s_order",
		columns: orderColumns,
	}
}
func NewUserDao() *UserDao {
	return &UserDao{
		group:   "user",
		table:   "s_user",
		columns: userColumns,
	}
}

When I execute these two dao using nested transaction, it does not use SAVEPOINT keywords, resulting in transaction 2 not rolling back when transaction 3 rolls back.
code:

g.DB("shop").Transaction(context.Background(), func(ctx context.Context, tx gdb.TX) error {
		_, err := shopDao.Order.Ctx(ctx).Data(&shopEntity.Order{
			TenantId: 3,
			PayTime:  gtime.Now(),
		}).InsertAndGetId()
		if err != nil {
			return err
		}

		err = g.DB("user").Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
			_, err := userDao.User.Ctx(ctx).Data(&userEntity.User{
				TenantId: 3,
			}).InsertAndGetId()
			return err
		})
		if err != nil {
			return err
		}

		err = g.DB("user").Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
			_, err := userDao.User.Ctx(ctx).Data(&userEntity.User{
				TenantId: 4,
			}).InsertAndGetId()
			if err != nil {
				return err
			}
			return gerror.New("111") //rollback
		})
		if err != nil {
			return err
		}
		return nil
	})

result:

2024-06-18 18:38:09.095 [DEBU] {dc60b25efd12da175702df3e605d51b4} [187 ms] [shop] [db_small_shop] [rows:0  ] [txid:1] BEGIN
2024-06-18 18:38:09.113 [DEBU] {dc60b25efd12da175702df3e605d51b4} [ 17 ms] [shop] [db_small_shop] [rows:32 ] [txid:1] SHOW FULL COLUMNS FROM `s_order`
2024-06-18 18:38:09.143 [DEBU] {dc60b25efd12da175702df3e605d51b4} [ 30 ms] [shop] [db_small_shop] [rows:1  ] [txid:1] INSERT INTO `s_order`(`tenant_id`,`pay_time`) VALUES(3,'2024-06-18 18:38:09')
2024-06-18 18:38:11.042 [DEBU] {dc60b25efd12da175702df3e605d51b4} [189 ms] [user] [db_small_shop] [rows:0  ] [txid:2] BEGIN
2024-06-18 18:38:11.065 [DEBU] {dc60b25efd12da175702df3e605d51b4} [ 22 ms] [user] [db_small_shop] [rows:12 ] [txid:2] SHOW FULL COLUMNS FROM `s_user`
2024-06-18 18:38:11.105 [DEBU] {dc60b25efd12da175702df3e605d51b4} [ 40 ms] [user] [db_small_shop] [rows:1  ] [txid:2] INSERT INTO `s_user`(`id`,`tenant_id`) VALUES(0,3)
2024-06-18 18:38:11.128 [DEBU] {dc60b25efd12da175702df3e605d51b4} [ 23 ms] [user] [db_small_shop] [rows:0  ] [txid:2] COMMIT
2024-06-18 18:38:11.205 [DEBU] {dc60b25efd12da175702df3e605d51b4} [ 76 ms] [user] [db_small_shop] [rows:0  ] [txid:3] BEGIN
2024-06-18 18:38:11.236 [DEBU] {dc60b25efd12da175702df3e605d51b4} [ 30 ms] [user] [db_small_shop] [rows:1  ] [txid:3] INSERT INTO `s_user`(`id`,`tenant_id`) VALUES(0,4)
2024-06-18 18:38:11.255 [DEBU] {dc60b25efd12da175702df3e605d51b4} [ 18 ms] [user] [db_small_shop] [rows:0  ] [txid:3] ROLLBACK
2024-06-18 18:38:11.272 [DEBU] {dc60b25efd12da175702df3e605d51b4} [ 17 ms] [shop] [db_small_shop] [rows:0  ] [txid:1] ROLLBACK

how to solve this problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant