简体   繁体   English

具有执行程序或多线程环境的Apache DBCP

[英]Apache DBCP with executor or multithreaded environment

I have the following code in my services.xml 我在services.xml中有以下代码

<bean id="executorService" class="java.util.concurrent.Executors" factory-method="newFixedThreadPool">
    <constructor-arg value="10" />
</bean>
<task:annotation-driven executor="executor" />

<task:executor id="executor" pool-size="10" queue-capacity="100" rejection-policy="CALLER_RUNS" />

In the same project, I also have connections to the database, that use dbcp.BasicDataSource. 在同一个项目中,我还与数据库建立了连接,使用dbcp.BasicDataSource。

I've read that DBCP is effective mostly when your app is single-threaded, NOT multi-threaded. 我已经读过,当您的应用程序是单线程而非多线程时,DBCP很有效。 The use of executor tells me that the app is multi-threaded. 使用执行器告诉我该应用程序是多线程的。 Do you think the use of DBCP here is not proper? 你认为这里使用DBCP不合适吗? Would this be a good practice? 这是一个好习惯吗? Or, am i living an age-old myth that DBCP can't handle multi-hreaded environment? 或者,我生活在一个古老的神话中,DBCP无法处理多种环境?

Any guidance in the right direction would be appreciated. 任何正确方向的指导将不胜感激。

I've read that DBCP is effective mostly when your app is single-threaded, NOT multi-threaded. 我已经读过,当您的应用程序是单线程而非多线程时,DBCP很有效。

Can you provide source of this information? 你能提供这些信息的来源吗? The only issue with DBCP is that it uses single lock to synchronize all operations on a pool, which may become a bottleneck in heavy multi-threaded applications. DBCP的唯一问题是它使用单一锁来同步池上的所有操作,这可能成为繁重的多线程应用程序的瓶颈。

DBCP can't handle multi-threaded environment? DBCP无法处理多线程环境?

Think about it. 想一想。 If your application is only single-threaded, it will never user more than one connection. 如果您的应用程序只是单线程,则永远不会使用多个连接。 JDBC is blocking so you can't use two connections at once from the same thread (simplifying). JDBC是阻塞的,因此您不能在同一个线程中同时使用两个连接(简化)。 That being said, if you only query database from one thread, not only you don't need a connection pool, you don't need a DataSource . 话虽这么说,如果你只从一个线程查询数据库,不仅你不需要连接池,你也不需要DataSource One Connection is enough. 一个Connection就足够了。

So... we are using connection pools mostly in multi-threaded applications especially where number of threads is much larger than number of available connections and threads are competing between each other. 所以...我们主要在多线程应用程序中使用连接池,特别是在线程数远大于可用连接数且线程在彼此之间进行竞争的情况下。 Every sane connection pool is capable of working in multi-threaded environment. 每个理智的连接池都能够在多线程环境中工作。

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

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