diff --git a/cdb/db.go b/cdb/db.go index 30eeb1c..cb2cb2a 100644 --- a/cdb/db.go +++ b/cdb/db.go @@ -64,28 +64,31 @@ func New(dbPool *sql.DB) *DB { return &DB{DB: dbPool, DBLck: InitDbLocker(dbPool), dbPool: dbPool} } -func (odb *DB) CreateTx(ctx context.Context, opts *sql.TxOptions) error { - if odb.hasTx { +func (oDb *DB) CreateTx(ctx context.Context, opts *sql.TxOptions) error { + if oDb.hasTx { return fmt.Errorf("already in a transaction") } - if tx, err := odb.dbPool.BeginTx(ctx, opts); err != nil { + if tx, err := oDb.dbPool.BeginTx(ctx, opts); err != nil { return err } else { - odb.DB = tx - odb.hasTx = true + oDb.DB = tx + oDb.hasTx = true return nil } } -func (odb *DB) CreateSession(ev eventPublisher) { - odb.Session = &Session{ - db: odb.DB, +func (oDb *DB) CreateSession(ev eventPublisher) { + oDb.Session = &Session{ + db: oDb.DB, ev: ev, tables: make(map[string]struct{}), } } func (oDb *DB) Commit() error { + if !oDb.hasTx { + return nil + } tx, ok := oDb.DB.(DBTxer) if !ok { return nil @@ -94,6 +97,10 @@ func (oDb *DB) Commit() error { } func (oDb *DB) Rollback() error { + if !oDb.hasTx { + return nil + } + defer func() { oDb.hasTx = false }() tx, ok := oDb.DB.(DBTxer) if !ok { return nil diff --git a/cdb/db_apps.go b/cdb/db_apps.go index ecc52ca..0a15d8f 100644 --- a/cdb/db_apps.go +++ b/cdb/db_apps.go @@ -220,16 +220,16 @@ func (oDb *DB) AppIDFromObjectID(ctx context.Context, objectID string) (int64, b // q &= db.apps.app == db.nodes.app // return db(q).select(db.apps.ALL).first() // return row -func (odb *DB) AppIDFromObjectOrNodeIDs(ctx context.Context, nodeID, objectID string) (int64, bool, error) { +func (oDb *DB) AppIDFromObjectOrNodeIDs(ctx context.Context, nodeID, objectID string) (int64, bool, error) { if objectID != "" { - if found, ok, err := odb.AppIDFromObjectID(ctx, objectID); err == nil { + if found, ok, err := oDb.AppIDFromObjectID(ctx, objectID); err == nil { // abort on error return found, ok, err } else if ok { return found, ok, err } } - return odb.AppIDFromNodeID(ctx, nodeID) + return oDb.AppIDFromNodeID(ctx, nodeID) } func (oDb *DB) AppsWithoutResponsible(ctx context.Context) (apps []string, err error) {