How to configure connection pool for Golang database connection?

王林
Release: 2024-06-06 11:21:00
Original
1025 people have browsed it

How to configure connection pool for Go database connection? Create a database connection using the DB type in the database/sql package; set MaxOpenConns to control the maximum number of concurrent connections; set MaxIdleConns to set the maximum number of idle connections; set ConnMaxLifetime to control the maximum life cycle of the connection.

如何为 Golang 数据库连接配置连接池?

#How to configure connection pooling for Go database connections?

Preface
When writing Go programs involving database operations, connection pooling is an effective technology to optimize connection management and improve application performance. It reduces the overhead of establishing new connections by creating and managing database connections in advance.

Implementation of connection pool
Go provides a native package database/sql, which contains the DB type, which provides a connection pool Function. You can configure a connection pool for the DB object through the following steps:

Code example:

package main

import (
    "database/sql"
    "log"

    _ "github.com/go-sql-driver/mysql"
)

func main() {
    // 连接字符串
    connectionString := "user:password@tcp(host:port)/database"

    // 打开数据库连接,配置连接池
    db, err := sql.Open("mysql", connectionString)
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()

    // 设置最大并发连接数
    db.SetMaxOpenConns(10)

    // 设置最大空闲连接数
    db.SetMaxIdleConns(5)

    // 设置最大连接生命周期
    db.SetConnMaxLifetime(time.Minute * 5)
}
Copy after login

Practical case
We take the MySQL database as an example to demonstrate how to configure the connection pool. Suppose we have a table named product and we want to get all product records.

Code Example:

package main

import (
    "context"
    "database/sql"
    "fmt"
    "log"

    _ "github.com/go-sql-driver/mysql"
)

func main() {
    // 连接字符串
    connectionString := "user:password@tcp(host:port)/database"

    // 打开数据库连接,配置连接池
    db, err := sql.Open("mysql", connectionString)
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()

    // 查询所有产品记录
    rows, err := db.QueryContext(context.Background(), "SELECT * FROM product")
    if err != nil {
        log.Fatal(err)
    }
    defer rows.Close()

    // 遍历结果行
    for rows.Next() {
        // 获取每行的值
        var id int
        var name string
        if err := rows.Scan(&id, &name); err != nil {
            log.Fatal(err)
        }

        // 打印结果
        fmt.Printf("Id: %d, Name: %s\n", id, name)
    }
}
Copy after login

In this example, we create a connection pool and use it to perform database queries. By configuring connection pooling, we optimize the management of database connections and improve application performance, especially in high-concurrency environments.

The above is the detailed content of How to configure connection pool for Golang database connection?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template