conn.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package pgxpool
  2. import (
  3. "context"
  4. "sync/atomic"
  5. "github.com/jackc/pgx/v5"
  6. "github.com/jackc/pgx/v5/pgconn"
  7. "github.com/jackc/puddle/v2"
  8. )
  9. // Conn is an acquired *pgx.Conn from a Pool.
  10. type Conn struct {
  11. res *puddle.Resource[*connResource]
  12. p *Pool
  13. }
  14. // Release returns c to the pool it was acquired from. Once Release has been called, other methods must not be called.
  15. // However, it is safe to call Release multiple times. Subsequent calls after the first will be ignored.
  16. func (c *Conn) Release() {
  17. if c.res == nil {
  18. return
  19. }
  20. conn := c.Conn()
  21. res := c.res
  22. c.res = nil
  23. if c.p.releaseTracer != nil {
  24. c.p.releaseTracer.TraceRelease(c.p, TraceReleaseData{Conn: conn})
  25. }
  26. if conn.IsClosed() || conn.PgConn().IsBusy() || conn.PgConn().TxStatus() != 'I' {
  27. res.Destroy()
  28. // Signal to the health check to run since we just destroyed a connections
  29. // and we might be below minConns now
  30. c.p.triggerHealthCheck()
  31. return
  32. }
  33. // If the pool is consistently being used, we might never get to check the
  34. // lifetime of a connection since we only check idle connections in checkConnsHealth
  35. // so we also check the lifetime here and force a health check
  36. if c.p.isExpired(res) {
  37. atomic.AddInt64(&c.p.lifetimeDestroyCount, 1)
  38. res.Destroy()
  39. // Signal to the health check to run since we just destroyed a connections
  40. // and we might be below minConns now
  41. c.p.triggerHealthCheck()
  42. return
  43. }
  44. if c.p.afterRelease == nil {
  45. res.Release()
  46. return
  47. }
  48. go func() {
  49. if c.p.afterRelease(conn) {
  50. res.Release()
  51. } else {
  52. res.Destroy()
  53. // Signal to the health check to run since we just destroyed a connections
  54. // and we might be below minConns now
  55. c.p.triggerHealthCheck()
  56. }
  57. }()
  58. }
  59. // Hijack assumes ownership of the connection from the pool. Caller is responsible for closing the connection. Hijack
  60. // will panic if called on an already released or hijacked connection.
  61. func (c *Conn) Hijack() *pgx.Conn {
  62. if c.res == nil {
  63. panic("cannot hijack already released or hijacked connection")
  64. }
  65. conn := c.Conn()
  66. res := c.res
  67. c.res = nil
  68. res.Hijack()
  69. return conn
  70. }
  71. func (c *Conn) Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error) {
  72. return c.Conn().Exec(ctx, sql, arguments...)
  73. }
  74. func (c *Conn) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error) {
  75. return c.Conn().Query(ctx, sql, args...)
  76. }
  77. func (c *Conn) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row {
  78. return c.Conn().QueryRow(ctx, sql, args...)
  79. }
  80. func (c *Conn) SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults {
  81. return c.Conn().SendBatch(ctx, b)
  82. }
  83. func (c *Conn) CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error) {
  84. return c.Conn().CopyFrom(ctx, tableName, columnNames, rowSrc)
  85. }
  86. // Begin starts a transaction block from the *Conn without explicitly setting a transaction mode (see BeginTx with TxOptions if transaction mode is required).
  87. func (c *Conn) Begin(ctx context.Context) (pgx.Tx, error) {
  88. return c.Conn().Begin(ctx)
  89. }
  90. // BeginTx starts a transaction block from the *Conn with txOptions determining the transaction mode.
  91. func (c *Conn) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error) {
  92. return c.Conn().BeginTx(ctx, txOptions)
  93. }
  94. func (c *Conn) Ping(ctx context.Context) error {
  95. return c.Conn().Ping(ctx)
  96. }
  97. func (c *Conn) Conn() *pgx.Conn {
  98. return c.connResource().conn
  99. }
  100. func (c *Conn) connResource() *connResource {
  101. return c.res.Value()
  102. }
  103. func (c *Conn) getPoolRow(r pgx.Row) *poolRow {
  104. return c.connResource().getPoolRow(c, r)
  105. }
  106. func (c *Conn) getPoolRows(r pgx.Rows) *poolRows {
  107. return c.connResource().getPoolRows(c, r)
  108. }