...

Source file src/github.com/jackc/pgx/v5/pgxpool/tx.go

Documentation: github.com/jackc/pgx/v5/pgxpool

     1  package pgxpool
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/jackc/pgx/v5"
     7  	"github.com/jackc/pgx/v5/pgconn"
     8  )
     9  
    10  // Tx represents a database transaction acquired from a Pool.
    11  type Tx struct {
    12  	t pgx.Tx
    13  	c *Conn
    14  }
    15  
    16  // Begin starts a pseudo nested transaction implemented with a savepoint.
    17  func (tx *Tx) Begin(ctx context.Context) (pgx.Tx, error) {
    18  	return tx.t.Begin(ctx)
    19  }
    20  
    21  // Commit commits the transaction and returns the associated connection back to the Pool. Commit will return ErrTxClosed
    22  // if the Tx is already closed, but is otherwise safe to call multiple times. If the commit fails with a rollback status
    23  // (e.g. the transaction was already in a broken state) then ErrTxCommitRollback will be returned.
    24  func (tx *Tx) Commit(ctx context.Context) error {
    25  	err := tx.t.Commit(ctx)
    26  	if tx.c != nil {
    27  		tx.c.Release()
    28  		tx.c = nil
    29  	}
    30  	return err
    31  }
    32  
    33  // Rollback rolls back the transaction and returns the associated connection back to the Pool. Rollback will return ErrTxClosed
    34  // if the Tx is already closed, but is otherwise safe to call multiple times. Hence, defer tx.Rollback() is safe even if
    35  // tx.Commit() will be called first in a non-error condition.
    36  func (tx *Tx) Rollback(ctx context.Context) error {
    37  	err := tx.t.Rollback(ctx)
    38  	if tx.c != nil {
    39  		tx.c.Release()
    40  		tx.c = nil
    41  	}
    42  	return err
    43  }
    44  
    45  func (tx *Tx) CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error) {
    46  	return tx.t.CopyFrom(ctx, tableName, columnNames, rowSrc)
    47  }
    48  
    49  func (tx *Tx) SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults {
    50  	return tx.t.SendBatch(ctx, b)
    51  }
    52  
    53  func (tx *Tx) LargeObjects() pgx.LargeObjects {
    54  	return tx.t.LargeObjects()
    55  }
    56  
    57  // Prepare creates a prepared statement with name and sql. If the name is empty,
    58  // an anonymous prepared statement will be used. sql can contain placeholders
    59  // for bound parameters. These placeholders are referenced positionally as $1, $2, etc.
    60  //
    61  // Prepare is idempotent; i.e. it is safe to call Prepare multiple times with the same
    62  // name and sql arguments. This allows a code path to Prepare and Query/Exec without
    63  // needing to first check whether the statement has already been prepared.
    64  func (tx *Tx) Prepare(ctx context.Context, name, sql string) (*pgconn.StatementDescription, error) {
    65  	return tx.t.Prepare(ctx, name, sql)
    66  }
    67  
    68  func (tx *Tx) Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error) {
    69  	return tx.t.Exec(ctx, sql, arguments...)
    70  }
    71  
    72  func (tx *Tx) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error) {
    73  	return tx.t.Query(ctx, sql, args...)
    74  }
    75  
    76  func (tx *Tx) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row {
    77  	return tx.t.QueryRow(ctx, sql, args...)
    78  }
    79  
    80  func (tx *Tx) Conn() *pgx.Conn {
    81  	return tx.t.Conn()
    82  }
    83  

View as plain text