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

此项目中的,数据库事务写法无效(错误) #10

Open
hinego opened this issue Jul 13, 2022 · 3 comments
Open

此项目中的,数据库事务写法无效(错误) #10

hinego opened this issue Jul 13, 2022 · 3 comments

Comments

@hinego
Copy link

hinego commented Jul 13, 2022

	err = dao.User.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
		name := "testinsert1"
		var user = new(entity.User)
		user.Passport = name
		user.Password = name
		user.Nickname = name
		user.Gender = 0
		user.Avatar = name
		user.Status = 1

		_, err := dao.User.Ctx(ctx).Data(user).OmitEmpty().Save()
		if err != nil {
			return err
		}
		_, err = dao.User.Ctx(ctx).Data(user).OmitEmpty().Save()
		if err != nil {
			return err
		}
		return nil
	})

项目中的事务基本都是想上面这种写法,内嵌函数中的数据库操作并没用用到 tx *gdb.TX 而是直接用 dao.User.Ctx(ctx)

这些操作根本就不在事务中运行,所以上面这种写法,会导致第一次写入操作成功(无论第二次操作是否成功)

有效的写法:

	err = dao.User.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
		name := "testinsert1"
		var user = new(entity.User)
		user.Passport = name
		user.Password = name
		user.Nickname = name
		user.Gender = 0
		user.Avatar = name
		user.Status = 1
		_, err := tx.Insert(dao.User.Table(), user)
		if err != nil {
			return err
		}
		_, err = tx.Insert(dao.User.Table(), user)
		if err != nil {
			return err
		}
		return nil
	})


### 还有一个无效的写法(按道理说这是应该支持的写法 不支持为什么不行 应该是BUG了)

	err = dao.User.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
		name := "testinsert1"
		var user = new(entity.User)
		user.Passport = name
		user.Password = name
		user.Nickname = name
		user.Gender = 0
		user.Avatar = name
		user.Status = 1
		_, err := tx.Save(dao.User.Table(), user)
		if err != nil {
			return err
		}
		_, err = tx.Save(dao.User.Table(), user)
		if err != nil {
			return err
		}
		return nil
	})

当然 这种错误写法对单次写入操作并不影响 因为单次写入根本不需要事务

@hinego
Copy link
Author

hinego commented Jul 13, 2022

func (a *cTest) Index(ctx context.Context, req *v1.TestReq) (res *v1.TestRes, err error) {
	err = dao.User.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
		name := "testinsert1"
		var user = new(entity.User)
		user.Passport = name
		user.Password = name
		user.Nickname = name
		user.Gender = 0
		user.Avatar = name
		user.Status = 1
		_, err := tx.Model(dao.User.Table()).Ctx(ctx).Data(user).OmitEmpty().Save()
		if err != nil {
			return err
		}

		_, err = tx.Model(dao.User.Table()).Ctx(ctx).Data(user).OmitEmpty().Save()
		if err != nil {
			return err
		}
		return nil
	})
	g.Log().Debug(ctx, err == nil, err)
	return &v1.TestRes{Content: gconv.String(err)}, err
}

这样写都是无效的。。。。。。

@hinego
Copy link
Author

hinego commented Jul 13, 2022


func (a *cTest) Index(ctx context.Context, req *v1.TestReq) (res *v1.TestRes, err error) {
	err = dao.User.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
		name := "testinsert1"
		var user = new(entity.User)
		user.Passport = name
		user.Password = name
		user.Nickname = name
		user.Gender = 0
		user.Avatar = name
		user.Status = 1
		_, err := tx.Model(dao.User.Table()).Ctx(ctx).Insert(user)
		if err != nil {
			return err
		}

		_, err = tx.Model(dao.User.Table()).Ctx(ctx).Insert(user)
		if err != nil {
			return err
		}
		return nil
	})
	g.Log().Debug(ctx, err == nil, err)
	return &v1.TestRes{Content: gconv.String(err)}, err
}

只有这样才行

@hinego
Copy link
Author

hinego commented Jul 13, 2022

func (a *cTest) Index(ctx context.Context, req *v1.TestReq) (res *v1.TestRes, err error) {
	err = dao.User.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
		name := "testinsert1"
		var user = new(entity.User)
		user.Passport = name
		user.Password = name
		user.Nickname = name
		user.Gender = 0
		user.Avatar = name
		user.Status = 1
		_, err := dao.User.Ctx(ctx).Insert(user)
		if err != nil {
			return err
		}

		_, err = dao.User.Ctx(ctx).Insert(user)
		if err != nil {
			return err
		}
		return nil
	})
	g.Log().Debug(ctx, err == nil, err)
	return &v1.TestRes{Content: gconv.String(err)}, err
}

这样写也有效 那就是save的问题了

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

No branches or pull requests

1 participant