Home Backend Development Golang Take you to understand golang mysql database connection pool

Take you to understand golang mysql database connection pool

May 11, 2021 am 11:32 AM
go Database connection pool

The following is an introduction to the golang mysql database connection pool from the golang tutorial column. I hope it will be helpful to friends in need!

Recently, I was using the go language ORM to do some database operations. Finally, I found a bug called invalid connection, so I went to learn about the timeout of the connection pool and mysql. Next, I will use the go ORM. mysql to illustrate (I understand that the languages ​​​​are all connected, and the principles should be the same).

When we want to add, delete, modify and check the database, the first step is to connect to the database

//conn the database
func ConnDb(dbConnString string, dbName string) error {
	maxIdle := 50
	maxConn := 50
	err := orm.RegisterDataBase(dbName, "mysql", dbConnString+"?charset=utf8&loc=Asia%2FShanghai", maxIdle, maxConn)

	if err != nil {
		util.GLogger.Errorw("in Connect DB", "err", err)
	}
	return err
}
Copy after login

The connection here is the mysql database. The maximum connection pool set is 50. The maximum The number of idle connections is 50.

What does this connection pool mainly do? To put it simply, if you want to get data from the database and change the data, you need to establish a pipeline with the database. This is to establish a network connection. We all know that TCP connections are time-consuming, so since it has taken a certain amount of time I've built this pipeline, so how can I take it and use it without throwing it away? Then the connection pool is where these established pipes are stored. The value of 50 can be simply understood as a maximum of 50 pipes. Note that the bigger the better, because if it is too large, it will occupy more memory. Of course, it will take up too much memory. If it is too small, there will be waiting for blocking.

Since these pipes are placed in the connection pool, then the idle connection refers to these idle pipes, then it is obvious that the value setting of the idle connection should not be larger than the size of the connection pool, because the larger the connection The pool will not help you save so many idle connection pipes.

After understanding these simple concepts, what is the workflow like every time you access the database?

#Through this diagram we can clearly see the entire connection access process.

step1 (Get available connections) Go to the connection pool to find available idle connections. If there are no idle connections, then determine whether the connection pool is full. If not, then create a new connection. , if it is full, then wait for the connection to be released; of course, if there is an idle connection, it will directly determine whether the connection has expired. If it has not expired, it will be used directly. If it expires, it will re-judge whether the connection pool is full. No If so, create a new connection and wait when it is full.

step2 (Operation database) After getting this connection, perform addition, deletion, modification and query operations.

step3 (Release connection) After operating the database, you need to release the connection, then the released connection will become idle. If it exceeds the number of idle connections, it will be closed directly. If not, it will be used for waiting.

Then you will notice that this connection will fail:

The timeout of mysql database, when you establish a connection with the database, the database cannot always trust you, then the database There is a timeout, that is, after this period of time, I will not trust your connection. You must connect to me again. Check the various timeout statements set by the database as follows:

show variables like  '%timeout%';
Copy after login

Among them There will be two timeouts: interactive connection timeout (interactive_timeout) and non-interactive connection timeout (wait_timeout)

Interactive connection: you connect to mysql through the command line

Non-interactive connection: It is to connect to mysql in the program

And this non-interactive timeout is when the idle time of the idle connection in the connection pool exceeds the wait_timeout setting. Then at this time, when the program gets the idle connection and makes a query, the original problem will appear, invalid connection.

After understanding the basic principles, the solution to invalid connection is very simple:

1. Extend the wait_timeout time of the database.

2. The program regularly checks these failed connections and discards them in time. Note that the checking time of the program here needs to be less than the wait_timeout value set by mysql.

The above is my understanding of the connection pool. If there are any mistakes, please point them out. Thank you~

The above is the detailed content of Take you to understand golang mysql database connection pool. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to configure connection pool for Golang database connection? How to configure connection pool for Golang database connection? Jun 06, 2024 am 11:21 AM

How to configure connection pooling for Go database connections? Use the DB type in the database/sql package to create a database connection; 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.

In-depth understanding of Golang function life cycle and variable scope In-depth understanding of Golang function life cycle and variable scope Apr 19, 2024 am 11:42 AM

In Go, the function life cycle includes definition, loading, linking, initialization, calling and returning; variable scope is divided into function level and block level. Variables within a function are visible internally, while variables within a block are only visible within the block.

How to match timestamps using regular expressions in Go? How to match timestamps using regular expressions in Go? Jun 02, 2024 am 09:00 AM

In Go, you can use regular expressions to match timestamps: compile a regular expression string, such as the one used to match ISO8601 timestamps: ^\d{4}-\d{2}-\d{2}T \d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-][0-9]{2}:[0-9]{2})$ . Use the regexp.MatchString function to check if a string matches a regular expression.

How to send Go WebSocket messages? How to send Go WebSocket messages? Jun 03, 2024 pm 04:53 PM

In Go, WebSocket messages can be sent using the gorilla/websocket package. Specific steps: Establish a WebSocket connection. Send a text message: Call WriteMessage(websocket.TextMessage,[]byte("Message")). Send a binary message: call WriteMessage(websocket.BinaryMessage,[]byte{1,2,3}).

The difference between Golang and Go language The difference between Golang and Go language May 31, 2024 pm 08:10 PM

Go and the Go language are different entities with different characteristics. Go (also known as Golang) is known for its concurrency, fast compilation speed, memory management, and cross-platform advantages. Disadvantages of the Go language include a less rich ecosystem than other languages, a stricter syntax, and a lack of dynamic typing.

How to avoid memory leaks in Golang technical performance optimization? How to avoid memory leaks in Golang technical performance optimization? Jun 04, 2024 pm 12:27 PM

Memory leaks can cause Go program memory to continuously increase by: closing resources that are no longer in use, such as files, network connections, and database connections. Use weak references to prevent memory leaks and target objects for garbage collection when they are no longer strongly referenced. Using go coroutine, the coroutine stack memory will be automatically released when exiting to avoid memory leaks.

How to use Golang's error wrapper? How to use Golang's error wrapper? Jun 03, 2024 pm 04:08 PM

In Golang, error wrappers allow you to create new errors by appending contextual information to the original error. This can be used to unify the types of errors thrown by different libraries or components, simplifying debugging and error handling. The steps are as follows: Use the errors.Wrap function to wrap the original errors into new errors. The new error contains contextual information from the original error. Use fmt.Printf to output wrapped errors, providing more context and actionability. When handling different types of errors, use the errors.Wrap function to unify the error types.

A guide to unit testing Go concurrent functions A guide to unit testing Go concurrent functions May 03, 2024 am 10:54 AM

Unit testing concurrent functions is critical as this helps ensure their correct behavior in a concurrent environment. Fundamental principles such as mutual exclusion, synchronization, and isolation must be considered when testing concurrent functions. Concurrent functions can be unit tested by simulating, testing race conditions, and verifying results.

See all articles