简体   繁体   中英

Reuse connection in database connection pool

As per my understanding, database connection pool usually works this way:

  1. create n connections during app initialization and put them into a cache(eg a list)
  2. a thread will require a connection to do some operation to the db and return the connection back when it has finished
  3. when there is no available connection in the cache, the thread in step2 will be waiting util a connection is pushed back to the cache

My question is :

Can we execute multiple db operations through one connection after we acquire it from the pool instead of do one db operation then put it back? it's seems more efficient, because it saves the time acquiring and putting back the connection. (under multiple threads condition, there must be some cost of locking when add and get from the connection pool)

can anyone help? Thks!

Yes, the database connection can be used for multiple operations each time it is acquired from the pool, and this behavior is typical for database applications that use pooling. For example, a connection might be acquired once and reused for several operations during the handling of a request to a REST service. This lifecycle also often involves managing those operations as a single transaction in the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM