Skip to content

Latest commit

 

History

History
85 lines (58 loc) · 2.87 KB

README.md

File metadata and controls

85 lines (58 loc) · 2.87 KB

txn: Generic Distributed Transaction Management for Go

License GoDoc Project status Go Report Card

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

Key Features

  • 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.

Installation

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

Usage

  1. Create a Tx Instance:
tx := txn.New()
  1. 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...
  1. 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
}

Adapters

The txn package supports multiple database adapters:

Contributing

Contributions are welcome! Please feel free to submit issues, bug reports, or pull requests.

License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.