Skip to content

Commit bc7c840

Browse files
authored
Merge pull request #2195 from LucasHild/master
Add CommitQuery to transaction options
2 parents ad87d47 + 64ca07e commit bc7c840

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

tx.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ type TxOptions struct {
4848
// BeginQuery is the SQL query that will be executed to begin the transaction. This allows using non-standard syntax
4949
// such as BEGIN PRIORITY HIGH with CockroachDB. If set this will override the other settings.
5050
BeginQuery string
51+
// CommitQuery is the SQL query that will be executed to commit the transaction.
52+
CommitQuery string
5153
}
5254

5355
var emptyTxOptions TxOptions
@@ -105,7 +107,10 @@ func (c *Conn) BeginTx(ctx context.Context, txOptions TxOptions) (Tx, error) {
105107
return nil, err
106108
}
107109

108-
return &dbTx{conn: c}, nil
110+
return &dbTx{
111+
conn: c,
112+
commitQuery: txOptions.CommitQuery,
113+
}, nil
109114
}
110115

111116
// Tx represents a database transaction.
@@ -154,6 +159,7 @@ type dbTx struct {
154159
conn *Conn
155160
savepointNum int64
156161
closed bool
162+
commitQuery string
157163
}
158164

159165
// Begin starts a pseudo nested transaction implemented with a savepoint.
@@ -177,7 +183,12 @@ func (tx *dbTx) Commit(ctx context.Context) error {
177183
return ErrTxClosed
178184
}
179185

180-
commandTag, err := tx.conn.Exec(ctx, "commit")
186+
commandSQL := "commit"
187+
if tx.commitQuery != "" {
188+
commandSQL = tx.commitQuery
189+
}
190+
191+
commandTag, err := tx.conn.Exec(ctx, commandSQL)
181192
tx.closed = true
182193
if err != nil {
183194
if tx.conn.PgConn().TxStatus() != 'I' {

0 commit comments

Comments
 (0)