tx.go 2.8 KB

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