V
V
Vadim Rublev2020-05-16 11:28:21
MySQL
Vadim Rublev, 2020-05-16 11:28:21

How to make two connection pools (with different DBs)?

How to make two connection pools (with different DBs)?
It just doesn't give. Why not?

db, err := sql.Open("mysql", ConnDB_One) // Connect to one database first.
db2, err := sql.Open("mysql", ConnDB_Two) // Connect to another DB below.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2020-05-16
@EvgenyMamonov

Exactly as you wrote

db1, err := sql.Open("mysql", DSN1) // Сначала к одной БД подключение.
db2, err := sql.Open("mysql", DSN2) // Ниже к другой БД подключение.

// настраиваете пулы как вам нужно
db1.SetMaxIdleConns(10)
db1.SetMaxOpenConns(100)

db2.SetMaxIdleConns(10)
db2.SetMaxOpenConns(100)
Complete code example

package main

import (
    "database/sql"
    "log"

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

func main() {
    db1, err := sql.Open("mysql", `user1:[email protected]/dbname1`) // Сначала к одной БД подключение.
    if err != nil {
        log.Fatal(err)
    }

    db2, err := sql.Open("mysql", `user2:[email protected]/dbname2`) // Ниже к другой БД подключение.
    if err != nil {
        log.Fatal(err)
    }

    // настраиваете пулы как вам нужно
    db1.SetMaxIdleConns(10)
    db1.SetMaxOpenConns(100)

    db2.SetMaxIdleConns(10)
    db2.SetMaxOpenConns(100)
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question