github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/core/database/trackeddb.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  
    11  // TrackedDB defines an interface for keeping track of sql.DB. This is useful
    12  // knowing if the underlying DB can be reused after an error has occurred.
    13  type TrackedDB interface {
    14  	// Txn executes the input function against the tracked database,
    15  	// within a transaction that depends on the input context.
    16  	// Retry semantics are applied automatically based on transient failures.
    17  	// This is the function that almost all downstream database consumers
    18  	// should use.
    19  	Txn(context.Context, func(context.Context, *sql.Tx) error) error
    20  
    21  	// TxnNoRetry executes the input function against the tracked database,
    22  	// within a transaction that depends on the input context.
    23  	// No retries are attempted.
    24  	TxnNoRetry(context.Context, func(context.Context, *sql.Tx) error) error
    25  
    26  	// Err returns an error if the underlying tracked DB is in an error
    27  	// condition. Depending on the error, determines of the tracked DB can be
    28  	// requested again, or should be given up and thrown away.
    29  	Err() error
    30  }