2
0

Allow ConnPool to have MaxConnections of 1

This commit is contained in:
Jack Christensen
2015-05-25 09:54:28 -05:00
parent 07a11abc07
commit db5300358a
3 changed files with 8 additions and 16 deletions
+6
View File
@@ -109,6 +109,8 @@ func (d *Driver) Open(name string) (driver.Conn, error) {
// *sql.DB and typecasting to *stdlib.Driver a reference to the pgx.ConnPool can
// be reaquired later. This allows fast paths targeting pgx to be used while
// still maintaining compatibility with other databases and drivers.
//
// pool connection size must be at least 2.
func OpenFromConnPool(pool *pgx.ConnPool) (*sql.DB, error) {
d := &Driver{Pool: pool}
name := fmt.Sprintf("pgx-%d", openFromConnPoolCount)
@@ -126,6 +128,10 @@ func OpenFromConnPool(pool *pgx.ConnPool) (*sql.DB, error) {
// that would mean that prepared statements would be lost (which kills
// performance if the prepared statements constantly have to be reprepared)
stat := pool.Stat()
if stat.MaxConnections <= 2 {
return nil, errors.New("pool connection size must be at least 2")
}
db.SetMaxIdleConns(stat.MaxConnections - 2)
db.SetMaxOpenConns(stat.MaxConnections)