Building a Go MySQL Driver for Seata AT Mode: A Practical Guide

This article details the development of a Go MySQL driver that fully supports Seata's AT distributed‑transaction mode, covering background, design decisions, driver specifics, initialization steps, code examples, and how it lowers the barrier for Go microservices to use distributed transactions.

Alibaba Cloud Native
Alibaba Cloud Native
Alibaba Cloud Native
Building a Go MySQL Driver for Seata AT Mode: A Practical Guide

Background

In April 2020 a Go distributed‑transaction framework called Seata‑Golang was started. The Java Seata AT mode proxies the DataSource and intercepts SQL to capture before/after snapshots for rollback. The goal for the Go client was to achieve the same functionality with minimal code intrusion.

Using database/sql in Go

Go developers obtain a database handle with sql.Open("mysql", dsn). Transactions are started via db.Begin() or db.BeginTx(ctx, &sql.TxOptions{}), returning a tx object. Queries use tx.Query, modifications use tx.Exec, and the transaction is finished with tx.Commit() or tx.Rollback().

Design Decision

Embedding AT parameters in the DSN would break driver design, so the AT proxy is placed above the database/sql layer. The proxy wraps objects created by database/sql and implements the same Tx interface, adding a custom Begin() method. This preserves the familiar API while providing Seata‑Golang functionality.

Tx Interface

type Tx interface {
  Commit() error
  Rollback() error
}

Driver Details

Do not set interpolateParams=true in the DSN . The driver only handles the Binary protocol; enabling this forces the Text protocol, which is unsupported.

Joining a global transaction : call db.BeginTx(ctx, driver.TxOptions) and embed the XID in the context using mysql.XID.

Initialization : before using the driver, initialize the Seata‑Golang client and register the MySQL driver.

config.InitConf(configPath)
client.NewRpcClient()
mysql.InitDataResourceManager()
mysql.RegisterResource(config.GetATConfig().DSN)

Example Usage

ctx := context.WithValue(context.Background(), mysql.XID, c.Request.Header.Get("XID"))
tx, err := dao.BeginTx(ctx, &sql.TxOptions{
    Isolation: sql.LevelDefault,
    ReadOnly: false,
})

References

Source code (beta): https://github.com/opentrx/mysql

Sample usage: https://github.com/opentrx/seata-go-samples

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

microservicesdatabase/sqldistributed-transactionsmysql-driver
Alibaba Cloud Native
Written by

Alibaba Cloud Native

We publish cloud-native tech news, curate in-depth content, host regular events and live streams, and share Alibaba product and user case studies. Join us to explore and share the cloud-native insights you need.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.