Go pgxpool';s config.AfterRelease未启动
我有以下代码:Go pgxpool';s config.AfterRelease未启动,go,Go,我有以下代码: func (s *SqlServerDatabase) ConnectPool() { config, err := pgxpool.ParseConfig(s.Url) if err != nil { fmt.Println("config Database Fail") fmt.Print(err) } config.AfterRelease = func(conn *pgx.Conn) bo
func (s *SqlServerDatabase) ConnectPool() {
config, err := pgxpool.ParseConfig(s.Url)
if err != nil {
fmt.Println("config Database Fail")
fmt.Print(err)
}
config.AfterRelease = func(conn *pgx.Conn) bool {
fmt.Println("After Releasing")
return true
}
config.BeforeAquire = func(ctx context.Context, conn *pgx.Conn) bool {
fmt.Println("Before Aquiring")
return true
}
conn, err := pgxpool.ConnectConfig(context.Background(), config)
s.PoolConn = conn
}
func (s *SqlServerDatabase) PoolQuery(query string) (pgx.Rows, error) {
conn, err := s.PoolConn.Acquire(context.Background())
if err != nil {
log.Printf("Couldn't get a connection with the database. Reason %v", err)
} else {
// release the connection to the pool after using it
defer conn.Release()
results, err := conn.Query(context.Background(), query)
if err != nil {
log.Printf("Couldn't execute query. Reason %v", err)
} else {
// show the results boy, you got it.
fmt.Printf("%T\n", results)
}
return results, err
}
return nil, err
}
因此:
- 我在
中创建连接。这就是我设置配置和连接的地方ConnectPool
- 然后,在我的查询方法中,我获取一个池,
方法触发并打印beforeaquire
- 但后释放方法从来没有这样做
/*20*/func(c*Conn)Release(){
...
/*30*/if conn.IsClosed()| | conn.PgConn().IsBusy()||
conn.PgConn().TxStatus()!=“我”||
(now.Sub(res.CreationTime())>c.p.maxConnLifetime){
决议销毁()
返回
}
/*“c.后释放”仅在该块后检查和使用*/
因此:在某些情况下,连接会立即断开,AfterRelease
确实不会执行
注:代码链接反映了2021-05-05上
主分支的状态