Last active
July 23, 2019 12:23
-
-
Save jfeng45/6939ba25202e828e530a654962fff734 to your computer and use it in GitHub Desktop.
transaction function in data handler layer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TransactionBegin starts a transaction | |
func (sdt *SqlDBTx) TxBegin() (gdbc.SqlGdbc, error) { | |
tx, err := sdt.DB.Begin() | |
sct := SqlConnTx{tx} | |
return &sct, err | |
} | |
func (sct *SqlConnTx) TxEnd(txFunc func() error) error { | |
var err error | |
tx := sct.DB | |
defer func() { | |
if p := recover(); p != nil { | |
tx.Rollback() | |
panic(p) // re-throw panic after Rollback | |
} else if err != nil { | |
tx.Rollback() // err is non-nil; don't change it | |
} else { | |
err = tx.Commit() // if Commit returns error update err with commit err | |
} | |
}() | |
err = txFunc() | |
return err | |
} | |
func (sct *SqlConnTx) Rollback() error { | |
return sct.DB.Rollback() | |
} | |
func (sct *SqlConnTx) Commit() error { | |
return sct.DB.Commit() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment