Sqlx: No connection pool set?

Created on 23 Mar 2017  路  5Comments  路  Source: jmoiron/sqlx

Hi
How can I set connection pool options with sqlx?
Can I change your Open funtion to this for connection pool purpose:
db, _ = sql.Open("mysql", "root:@tcp(127.0.0.1:3306)/test?charset=utf8")
db.SetMaxOpenConns(2000)
db.SetMaxIdleConns(1000)

Most helpful comment

@davcamer promoted fields act like ordinary fields of a struct except that they cannot be used as field names in composite literals of the struct. For example:

type Point struct {
    X, Y int
}

type Circle struct {
    Point
    Radius int
}

type Wheel struct {
    Circle
    Spokes int
}

func main() {
    var w Wheel
    w = Wheel{8, 8, 5, 20}  // compile error: unknown fields
    w = Wheel{X: 8, Y: 8, Radius: 5, Spokes: 20}  // compile error: unknown fields
    w = Wheel{Circle{Point{8, 8}, 5}, 20}  // complies
}

Because of this inconsistency, when names are optional in dot expressions, I prefer to always be explicit.

All 5 comments

@yzk0281 sqlx.DB contains a pointer to sql.DB

type DB struct {
    *sql.DB
    driverName string
    unsafe     bool
    Mapper     *reflectx.Mapper
}

So you can:

    var DB *sqlx.DB

    DB = sqlx.Connect(driverName, dataSourceName)

    DB.DB.SetMaxOpenConns(1000) // The default is 0 (unlimited)
    DB.DB.SetMaxIdleConns(10) // defaultMaxIdleConns = 2
    DB.DB.SetConnMaxLifetime(0) // 0, connections are reused forever.

@jhngrant
Thanks for answering 路~

@jhngrant
Good idea

Since sqlx.DB has a sql.DB as an embedded type, the extra layer of indirection (DB.DB) isn't needed.

Saying it another way:

    var DB *sqlx.DB

    DB = sqlx.Connect(driverName, dataSourceName)

    DB.DB.SetMaxOpenConns(1000) // The default is 0 (unlimited)
    DB.DB.SetMaxIdleConns(10) // defaultMaxIdleConns = 2
    DB.DB.SetConnMaxLifetime(0) // 0, connections are reused forever.

is exactly the same as

    var DB *sqlx.DB

    DB = sqlx.Connect(driverName, dataSourceName)

    DB.SetMaxOpenConns(1000) // The default is 0 (unlimited)
    DB.SetMaxIdleConns(10) // defaultMaxIdleConns = 2
    DB.SetConnMaxLifetime(0) // 0, connections are reused forever.

Effective Go describes this as:

The methods of embedded types come along for free

in the section on embedding.

I wanted to call this out because I was troubleshooting something, came across this issue and had a little panic of "I'm calling these funcs directly on sqlx.DB -- how is it even compiling!"

@davcamer promoted fields act like ordinary fields of a struct except that they cannot be used as field names in composite literals of the struct. For example:

type Point struct {
    X, Y int
}

type Circle struct {
    Point
    Radius int
}

type Wheel struct {
    Circle
    Spokes int
}

func main() {
    var w Wheel
    w = Wheel{8, 8, 5, 20}  // compile error: unknown fields
    w = Wheel{X: 8, Y: 8, Radius: 5, Spokes: 20}  // compile error: unknown fields
    w = Wheel{Circle{Point{8, 8}, 5}, 20}  // complies
}

Because of this inconsistency, when names are optional in dot expressions, I prefer to always be explicit.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davisford picture davisford  路  5Comments

resal81 picture resal81  路  5Comments

railsmechanic picture railsmechanic  路  5Comments

hexdigest picture hexdigest  路  4Comments

luiscvega picture luiscvega  路  4Comments