From a43713ce14ba3990a297030c872a1d051cf06477 Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Sun, 18 Dec 2022 11:32:15 +0200 Subject: [PATCH] [#1291] added condition to switch between the db pools in case of dry submit --- forms/record_upsert.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/forms/record_upsert.go b/forms/record_upsert.go index 3e6d6e10..7ce43ba7 100644 --- a/forms/record_upsert.go +++ b/forms/record_upsert.go @@ -675,9 +675,16 @@ func (form *RecordUpsert) DrySubmit(callback func(txDao *daos.Dao) error) error return err } - // use the default app.Dao().ConcurrentDB() to prevent changing the transaction form.Dao - // and causing "transaction has already been committed or rolled back" error - dryDao := daos.New(form.app.Dao().ConcurrentDB()) + var dryDao *daos.Dao + if form.dao.ConcurrentDB() == form.dao.NonconcurrentDB() { + // it is already in a transaction and therefore use the app concurrent db pool + // to prevent "transaction has already been committed or rolled back" error + dryDao = daos.New(form.app.Dao().ConcurrentDB()) + } else { + // otherwise use the form noncurrent dao db pool + dryDao = daos.New(form.dao.NonconcurrentDB()) + } + return dryDao.RunInTransaction(func(txDao *daos.Dao) error { tx, ok := txDao.DB().(*dbx.Tx) if !ok { @@ -685,11 +692,6 @@ func (form *RecordUpsert) DrySubmit(callback func(txDao *daos.Dao) error) error } defer tx.Rollback() - txDao.BeforeCreateFunc = nil - txDao.AfterCreateFunc = nil - txDao.BeforeUpdateFunc = nil - txDao.AfterUpdateFunc = nil - if err := txDao.SaveRecord(form.record); err != nil { return err }