The txmanager package provides a transaction manager to ensure that multiple storage backends remain synchronized
Also, it's likely that the overall interface will change as usage patterns and best practices emerge
At this stage, the code only supports PostgreSQL servers, but the ability to use other storage backends is part of the overall concept.
Imagine 2 PostgreSQL databases with identical schemas:
CREATE TABLE account (
id SERIAL PRIMARY KEY,
balance NUMERIC
);In order to transfer funds from an account in database 1 to an account in database 2, it is critical that both DML statements either succeed or rollback. Otherwise, money will disappear or magically be created.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
tm := txmanager.Transaction{}
defer tm.Abort("Deferred abort")
pool0 := sql.Open("postgres", connInfo0)
f0 := txmpg.NewFinalizer(ctx, "db0", pool0)
tm.Add(f0)
pool1 := sql.Open("postgres", connInfo1)
f1 := txmpg.NewFinalizer(ctx, "db1", pool1)
tm.Add(f1)
f0.TX.ExecContext(ctx, "UPDATE account SET balance = balance - 1 WHERE ID = 5")
f1.TX.ExecContext(ctx, "UPDATE account SET balance = balance + 1 WHERE ID = 6")
err := tm.Commit()