mirror of
https://github.com/jackc/pgx.git
synced 2025-05-31 11:42:24 +00:00
Inform database/sql when connections die
This commit is contained in:
parent
839ddcf75f
commit
d9522a4741
8
conn.go
8
conn.go
@ -116,6 +116,7 @@ func (e ProtocolError) Error() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var NotificationTimeoutError = errors.New("Notification Timeout")
|
var NotificationTimeoutError = errors.New("Notification Timeout")
|
||||||
|
var DeadConnError = errors.New("Connection is dead")
|
||||||
|
|
||||||
// Connect establishes a connection with a PostgreSQL server using config. One
|
// Connect establishes a connection with a PostgreSQL server using config. One
|
||||||
// of config.Socket or config.Host must be specified. config.User
|
// of config.Socket or config.Host must be specified. config.User
|
||||||
@ -966,8 +967,7 @@ func (c *Conn) rxMsg() (t byte, r *MessageReader, err error) {
|
|||||||
|
|
||||||
func (c *Conn) rxMsgHeader() (t byte, bodySize int32, err error) {
|
func (c *Conn) rxMsgHeader() (t byte, bodySize int32, err error) {
|
||||||
if !c.alive {
|
if !c.alive {
|
||||||
err = errors.New("Connection is dead")
|
return 0, 0, DeadConnError
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
@ -987,7 +987,7 @@ func (c *Conn) rxMsgHeader() (t byte, bodySize int32, err error) {
|
|||||||
|
|
||||||
func (c *Conn) rxMsgBody(bodySize int32) (*bytes.Buffer, error) {
|
func (c *Conn) rxMsgBody(bodySize int32) (*bytes.Buffer, error) {
|
||||||
if !c.alive {
|
if !c.alive {
|
||||||
return nil, errors.New("Connection is dead")
|
return nil, DeadConnError
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := c.getBuf()
|
buf := c.getBuf()
|
||||||
@ -1135,7 +1135,7 @@ func (c *Conn) txStartupMessage(msg *startupMessage) (err error) {
|
|||||||
|
|
||||||
func (c *Conn) txMsg(identifier byte, buf *bytes.Buffer, flush bool) (err error) {
|
func (c *Conn) txMsg(identifier byte, buf *bytes.Buffer, flush bool) (err error) {
|
||||||
if !c.alive {
|
if !c.alive {
|
||||||
return errors.New("Connection is dead")
|
return DeadConnError
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
|
@ -36,6 +36,10 @@ type Conn struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Conn) Prepare(query string) (driver.Stmt, error) {
|
func (c *Conn) Prepare(query string) (driver.Stmt, error) {
|
||||||
|
if !c.conn.IsAlive() {
|
||||||
|
return nil, driver.ErrBadConn
|
||||||
|
}
|
||||||
|
|
||||||
name := fmt.Sprintf("pgx_%d", c.psCount)
|
name := fmt.Sprintf("pgx_%d", c.psCount)
|
||||||
c.psCount++
|
c.psCount++
|
||||||
|
|
||||||
@ -52,6 +56,10 @@ func (c *Conn) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Conn) Begin() (driver.Tx, error) {
|
func (c *Conn) Begin() (driver.Tx, error) {
|
||||||
|
if !c.conn.IsAlive() {
|
||||||
|
return nil, driver.ErrBadConn
|
||||||
|
}
|
||||||
|
|
||||||
_, err := c.conn.Execute("begin")
|
_, err := c.conn.Execute("begin")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -74,12 +82,20 @@ func (s *Stmt) NumInput() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Stmt) Exec(argsV []driver.Value) (driver.Result, error) {
|
func (s *Stmt) Exec(argsV []driver.Value) (driver.Result, error) {
|
||||||
|
if !s.conn.IsAlive() {
|
||||||
|
return nil, driver.ErrBadConn
|
||||||
|
}
|
||||||
|
|
||||||
args := valueToInterface(argsV)
|
args := valueToInterface(argsV)
|
||||||
commandTag, err := s.conn.Execute(s.ps.Name, args...)
|
commandTag, err := s.conn.Execute(s.ps.Name, args...)
|
||||||
return driver.RowsAffected(commandTag.RowsAffected()), err
|
return driver.RowsAffected(commandTag.RowsAffected()), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Stmt) Query(argsV []driver.Value) (driver.Rows, error) {
|
func (s *Stmt) Query(argsV []driver.Value) (driver.Rows, error) {
|
||||||
|
if !s.conn.IsAlive() {
|
||||||
|
return nil, driver.ErrBadConn
|
||||||
|
}
|
||||||
|
|
||||||
args := valueToInterface(argsV)
|
args := valueToInterface(argsV)
|
||||||
|
|
||||||
rowCount := 0
|
rowCount := 0
|
||||||
|
Loading…
x
Reference in New Issue
Block a user