Go Transaction Manager is a library for managing SQL transactions in Go applications, offering a simple interface for controlling both isolated and nested transactions. Through context-based transaction management, it ensures atomicity and ease of use.
-
Isolated Transactions: Facilitates the management of isolated transactions across distinct parts of your application, ensuring data integrity.
-
Nested Transactions: Enables nested transactions for more complex transactional operations. This allows transactions within transactions with individual commit or rollback controls.
-
Context-Aware Transactions: Maintains and provides transactional context information, empowering your code with awareness of the current transaction state.
-
Manual and Automated Control: Offers both manual transaction control via the
Begin
function, allowing commit or rollback on demand, and automated control throughBeginFunc
, which automates the commit or rollback based on the function's return value.
To install the go-transact
library, run the following command:
go get github.com/sklyar/go-transact
go-transact
is designed to be extensible and supports various database adapters. Here is a list of currently supported ones:
Adapter | Description |
---|---|
Standard library adapter (transactstd) | The standard SQL adapter provides an easy way to integrate go-transact with any database that conforms to Go's database/sql interface. |
Here's how to get started with go-transact
:
import (
...
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/sklyar/go-transact"
"github.com/sklyar/go-transact/adapters/transactstd"
)
func main() {
sqlDB, err := sql.Open("pgx", os.Getenv("DATABASE_URL"))
checkErr(err)
txManager, db, err := transact.NewManager(transactstd.Wrap(sqlDB))
checkErr(err)
}
In the example above, a PostgreSQL database is initialized and wrapped using the provided standard SQL adapter. A new transaction manager is then created using this adapter.
Below are examples demonstrating the usage of go-transact
.
err = txManager.BeginFunc(ctx, func(ctx context.Context) error {
// execute your transactional operations
return nil // return an error to trigger rollback
})
checkErr(err)
In this example, BeginFunc
manages the transaction life cycle. It automatically starts the transaction and commits it if no errors occur during the execution of the passed function. If an error is returned, BeginFunc
triggers a rollback.
ctx, tx, err := txManager.Begin(ctx)
checkErr(err)
defer tx.Rollback(ctx) // rollback if commit is not called
// execute your transactional operations
err = doSomething(ctx)
checkErr(err)
err = tx.Commit(ctx)
checkErr(err)
Begin
provides more control over transactions. It starts a transaction and returns a transaction object (tx
), which can be manually committed or rolled back.
Both Begin
and BeginFunc
support nested transactions, allowing each transaction to be individually controlled:
txManager.BeginFunc(ctx, func(ctx context.Context) error {
// execute your transactional operations
err = txManager.BeginFunc(ctx, func(ctx context.Context) error {
// execute your nested transactional operations
return nil // return an error to trigger rollback of this nested transaction
})
checkErr(err)
return nil // return an error to trigger rollback of the entire transaction
})
In this example, nested transactions are created using BeginFunc
. If an error is returned from the inner BeginFunc
, it triggers a rollback of the nested transaction. If an error is returned from the outer BeginFunc
, it triggers a rollback of the entire transaction.
Go Transaction Manager is released under the MIT License. See the bundled LICENSE file for details.