简体   繁体   English

重用数据库连接池中的连接

[英]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) 在应用初始化期间创建n个连接并将其放入缓存(例如列表)
  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 当缓存中没有可用的连接时,步骤2中的线程将在等待将连接推回到缓存中

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) (在多线程情况下,从连接池进行addget时必须有一定的锁定成本)

can anyone help? 有人可以帮忙吗? Thks! ks!

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. 例如,在处理对REST服务的请求期间,连接可能只获得一次,并可以重复用于若干操作。 This lifecycle also often involves managing those operations as a single transaction in the database. 此生命周期通常还涉及将这些操作作为数据库中的单个事务进行管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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