-
Notifications
You must be signed in to change notification settings - Fork 0
/
txn.go
36 lines (30 loc) · 1.12 KB
/
txn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright 2023 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package database
import (
"context"
"database/sql"
"github.com/juju/juju/database/txn"
)
var (
defaultTransactionRunner = txn.NewTransactionRunner()
)
// Txn defines a generic txn function for applying transactions on a given
// database. It expects that no individual transaction function should take
// longer than the default timeout.
// There are no retry semantics for running the function.
//
// This should not be used directly, instead the TrackedDB should be used to
// handle transactions.
func Txn(ctx context.Context, db *sql.DB, fn func(context.Context, *sql.Tx) error) error {
return defaultTransactionRunner.Txn(ctx, db, fn)
}
// Retry defines a generic retry function for applying transactions on a given
// database. It expects that no individual transaction function should take
// longer than the default timeout.
//
// This should not be used directly, instead the TrackedDB should be used to
// handle transactions.
func Retry(ctx context.Context, fn func() error) error {
return defaultTransactionRunner.Retry(ctx, fn)
}