improve logging for transaction feature for package gdb

This commit is contained in:
John Guo
2021-05-02 15:58:28 +08:00
parent bd84b97614
commit cdc97e9b2b
5 changed files with 148 additions and 4 deletions

View File

@ -32,11 +32,11 @@ type DB interface {
// Model creation.
// ===========================================================================
// Table function is deprecated, use Model instead.
// The DB interface is designed not only for
// relational databases but also for NoSQL databases in the future. The name
// "Table" is not proper for that purpose any more.
// Also see Core.Table.
// Deprecated, use Model instead.
// Also see Core.Table.
Table(table ...string) *Model
// Model creates and returns a new ORM model from given schema.
@ -191,6 +191,8 @@ type DB interface {
mappingAndFilterData(schema, table string, data map[string]interface{}, filter bool) (map[string]interface{}, error) // See Core.mappingAndFilterData.
convertFieldValueToLocalValue(fieldValue interface{}, fieldType string) interface{} // See Core.convertFieldValueToLocalValue.
convertRowsToResult(rows *sql.Rows) (Result, error) // See Core.convertRowsToResult.
addSqlToTracing(ctx context.Context, sql *Sql) // See Core.addSqlToTracing.
writeSqlToLogger(v *Sql) // See Core.writeSqlToLogger.
}
// Core is the base struct for database management.

View File

@ -9,6 +9,7 @@ package gdb
import (
"database/sql"
"fmt"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/util/gconv"
"reflect"
@ -36,7 +37,27 @@ func (tx *TX) Commit() error {
_, err := tx.Exec("RELEASE SAVEPOINT " + tx.transactionKey())
return err
}
return tx.tx.Commit()
var (
sqlStr = "COMMIT"
mTime1 = gtime.TimestampMilli()
err = tx.tx.Commit()
mTime2 = gtime.TimestampMilli()
sqlObj = &Sql{
Sql: sqlStr,
Type: "TX.Commit",
Args: nil,
Format: sqlStr,
Error: err,
Start: mTime1,
End: mTime2,
Group: tx.db.GetGroup(),
}
)
tx.db.addSqlToTracing(tx.db.GetCtx(), sqlObj)
if tx.db.GetDebug() {
tx.db.writeSqlToLogger(sqlObj)
}
return err
}
// Rollback aborts current transaction.
@ -48,7 +69,27 @@ func (tx *TX) Rollback() error {
_, err := tx.Exec("ROLLBACK TO SAVEPOINT " + tx.transactionKey())
return err
}
return tx.tx.Rollback()
var (
sqlStr = "ROLLBACK"
mTime1 = gtime.TimestampMilli()
err = tx.tx.Rollback()
mTime2 = gtime.TimestampMilli()
sqlObj = &Sql{
Sql: sqlStr,
Type: "TX.Rollback",
Args: nil,
Format: sqlStr,
Error: err,
Start: mTime1,
End: mTime2,
Group: tx.db.GetGroup(),
}
)
tx.db.addSqlToTracing(tx.db.GetCtx(), sqlObj)
if tx.db.GetDebug() {
tx.db.writeSqlToLogger(sqlObj)
}
return err
}
// Begin starts a nested transaction procedure.