The txn
package provides a robust and flexible framework for managing distributed transactions across multiple data sources in your Go applications. By harnessing the power of Go generics, txn
enables a clean, database-agnostic approach to transaction handling, ensuring data consistency and atomicity even in complex, distributed environments.
Before of all, Check the Real World Example
- Distributed Transactions: Coordinate transactions across multiple data sources seamlessly.
- Database Independence: Work with various databases (PostgreSQL, MongoDB etc.) using specialized adapters.
- Clean Architecture: Maintain a clear separation of concerns, keeping your business logic decoupled from data access details.
- Atomicity: Ensure that all operations within a transaction either succeed or fail together, maintaining data integrity.
- Flexibility: Easily extend the framework by creating custom adapters for your specific data sources.
go get github.com/9ssi7/txn
go get github.com/9ssi7/txn/txngorm // For GORM Adapter
go get github.com/9ssi7/txn/txnmongo // For MongoDB Adapter
go get github.com/9ssi7/txn/txnsql // For Native SQL Adapter
- Create a Tx Instance:
tx := txn.New()
- Register Adapters:
gormAdapter := txngorm.New(gormDB)
tx.Register(gormAdapter)
mongoAdapter := txnmongo.New(mongoClient)
tx.Register(mongoAdapter)
sqlAdapter := txnsql.New(sqlDB)
tx.Register(sqlAdapter)
// Register more adapters as needed...
- Manage Transactions:
err := tx.Begin(context.Background())
if err != nil {
// Handle error
}
defer tx.End(context.Background()) // Ensure resources are cleaned up
// Perform operations on each data source using their respective adapters
// ...
if err := tx.Commit(context.Background()); err != nil {
tx.Rollback(context.Background())
// Handle commit error
}
The txn
package supports multiple database adapters:
- txngorm: GORM
- txnmongo: MongoDB
- txnsql: Native SQL
Contributions are welcome! Please feel free to submit issues, bug reports, or pull requests.
This project is licensed under the Apache License 2.0. See the LICENSE file for details.