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