I use gorm and mariadb
there is a function to update to the terminal table and insert to the terminalLog table. by using the transaction begin commit . before inserting I checked the terminalLog table by calling the GetByUniqueTerminalLog function. If the GetByUniqueTerminalLog function is placed before the transaction begin, it returns the query result data. but if the function is between the transaction begin and commit it does not return the data that was previously inserted into the terminalLog table.
what caused it? what should i do if GetByUniqueTerminalLog is placed between transactions begin commit.
this is the source code snippet
func (r *TerminalRepository) GetByUniqueTerminalLog(ctx *gin.Context, id int64, terminal_id int64, version string, device_id string, payment_id int64) (res *model.TerminalLog, err error) {
log.Debug("TerminalRepository - GetByUniqueTerminalLog() - starting...")
db := r.DbContext.DB
if id > 0 {
db = db.Where("id = ?", id)
}
if terminal_id > 0 {
db = db.Where("terminal_id = ? ", terminal_id)
}
if payment_id > 0 {
db = db.Where("payment_id = ? ", payment_id)
}
if device_id != "" {
db = db.Where("device_id = ? ", device_id)
}
if version != "" {
db = db.Where("version = ? ", version)
}
result := db.Debug().Find(&res)
if result.Error != nil {
return res, result.Error
}
log.Debug("TerminalRepository - GetByUniqueTerminalLog() - finished.")
return res, nil
}
func (r *TerminalRepository) UpdateTerminalVersion(ctx *gin.Context, data *model.Terminal) (err error) {
log.Debug("TerminalRepository - UpdateTerminalVersion() - starting...")
if err = data.Validate(); err != nil {
return err
}
dataUpdate, err := r.GetByUnique(ctx, data.ID, "", 0, "", "")
if err != nil {
return err
}
if dataUpdate.ID < 1 {
return custom.ErrorNotFoundDB(data.TableName(), "id", data.ID)
}
TerminalLog ,err := r.GetByUniqueTerminalLog(ctx,0,dataUpdate.ID,data.Version,"",0)
dataUpdate.Version = data.Version
dataUpdate.InitAudit(constant.OPERATION_SQL_UPDATE, data.UpdatedUser)
tx := r.DbContext.DB.Begin()
result := tx.Updates(&dataUpdate)
if result.Error != nil {
tx.Rollback()
return result.Error
}
if result.RowsAffected < 1 {
tx.Rollback()
return custom.ErrorOperationDB(dataUpdate.TableName(), "update")
}
//TerminalLog ,err := r.GetByUniqueTerminalLog(ctx,0,dataUpdate.ID,data.Version,"",0)
fmt.Printf("TerminalLog %+v\n ",TerminalLog)
if TerminalLog.ID < 1 {
DataTerminalLog := model.TerminalLog{
Version: data.Version,
Activity: "UPDATE_VERSION",
TerminalId: &dataUpdate.ID,
MerchantId: &dataUpdate.MerchantId,
PaymentId: dataUpdate.PaymentId,
DeviceId: dataUpdate.DeviceId,
}
DataTerminalLog.InitAudit(constant.OPERATION_SQL_INSERT, 1)
resultInsertLog := tx.Create(&DataTerminalLog)
if resultInsertLog.Error != nil {
tx.Rollback()
return resultInsertLog.Error
}
if resultInsertLog.RowsAffected < 1 {
tx.Rollback()
return custom.ErrorOperationDB(dataUpdate.TableName(), "insert")
}
}
tx.Commit()
log.Debug("TerminalRepository - UpdateTerminalVersion() - finished.")
return nil
}