Skip to content

Commit 42b562e

Browse files
committed
database: Document context limitations of Tx.Commit()
A key finding of Icinga/icingadb#800 was that committing a transaction does not necessarily have to respect the context of the transaction, depending on the database driver. As @lippserd suggested there, I have added notes to the documentation of all relevant database functions.
1 parent afba056 commit 42b562e

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

database/db.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,11 @@ func (db *DB) NamedBulkExec(
540540
// Takes in up to the number of entities specified in count from the arg stream and
541541
// executes a new transaction that runs a new query for each entity in this set of arguments,
542542
// until the arg stream has been processed.
543+
//
543544
// The transactions are executed in a separate goroutine with a weighting of 1
544545
// and can be executed concurrently to the extent allowed by the semaphore passed in sem.
546+
//
547+
// Note that committing the transaction may not honor the context provided, as described further in [DB.ExecTx].
545548
func (db *DB) NamedBulkExecTx(
546549
ctx context.Context, query string, count int, sem *semaphore.Weighted, arg <-chan Entity,
547550
) error {
@@ -776,6 +779,10 @@ func (db *DB) Delete(
776779
// if the function succeeds. If the function returns an error, the transaction is rolled back.
777780
//
778781
// Returns an error if starting the transaction, executing the function, or committing the transaction fails.
782+
//
783+
// Note that committing the transaction may not honor the context provided. For some database drivers, once a COMMIT
784+
// query is started, it will block until the database responds. Therefore, for time-critical scenarios, it is
785+
// recommended to add a select wrapper against the context.
779786
func (db *DB) ExecTx(ctx context.Context, fn func(context.Context, *sqlx.Tx) error) error {
780787
tx, err := db.BeginTxx(ctx, nil)
781788
if err != nil {

0 commit comments

Comments
 (0)