Last active
July 23, 2019 09:40
-
-
Save jfeng45/c3e0f9efd38b1dcf55ce7d3608c84874 to your computer and use it in GitHub Desktop.
use case level transaction interface
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
// TxDataInterface represents operations needed for transaction support. | |
// It only needs to be implemented once for each database | |
// For sqlGdbc, it is implemented for SqlDBTx in transaction.go | |
type TxDataInterface interface { | |
// TxBegin starts a transaction. It gets a DB handler from the receiver and return a TxDataInterface, which has a | |
// *sql.Tx inside. Any data access wrapped inside a transaction will go through the *sql.Tx | |
TxBegin() (TxDataInterface, error) | |
// TxEnd is called at the end of a transaction and based on whether there is an error, it commits or rollback the | |
// transaction. | |
// txFunc is the business function wrapped in a transaction | |
TxEnd(txFunc func() error) error | |
// Return the underline transaction handler, sql.Tx | |
GetTx() gdbc.SqlGdbc | |
} | |
// This interface needs to be included in every data service interface that needs transaction support | |
type EnableTxer interface { | |
// EnableTx enables transaction, basically it replaces the underling database handle sql.DB with sql.Tx | |
EnableTx(dataInterface TxDataInterface) | |
} | |
// UserDataInterface represents interface for user data access through database | |
type UserDataInterface interface { | |
... | |
Update(user *model.User) (rowsAffected int64, err error) | |
// Insert adds a user to a database. The returned resultUser has a Id, which is auto generated by database | |
Insert(user *model.User) (resultUser *model.User, err error) | |
// Need to add this for transaction support | |
EnableTxer | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment