简体   繁体   English

独立Java多线程应用程序的Db连接池是否有意义?

[英]Does Db connection pool for standalone Java multi threaded application make sense?

I have been through these questions - choose a db connection pool , Is DB connection pooling all that important and java - DataSource for standalone application - no application server 我经历过这些问题 - 选择数据库连接池数据库连接池是否重要java-DataSource用于独立应用程序 - 没有应用程序服务器

and those don't answer my curiosity. 那些不能回答我的好奇心。

What I have is a standalone multi threaded Java application which is supposed to perform a data load to DB in an off or low load window but has to be fast enough to complete within a limited time. 我所拥有的是一个独立的多线程Java应用程序,它应该在关闭或低负载窗口中对DB执行数据加载,但必须足够快以在有限的时间内完成。

Number of Java threads are configurable but limited to a maximum number. Java线程数是可配置的,但仅限于最大数量。

As far as DB connections go, I am currently getting a new connection for each thread and closing it down when that thread is done. 就数据库连接而言,我正在为每个线程获取一个新连接,并在该线程完成时将其关闭。 My reasons of not using third party DB connection pool are, 我不使用第三方数据库连接池的原因是,

1.Number of maximum Java threads are limited to fixed limit and that limit is manageable by DB ( Its DB2 database ) 1.最大Java线程数限制为固定限制,该限制可由DB(其DB2数据库)管理

2.Avoid unnecessary wait for connection from DB pools and avoid clash or wait times among multiple threads ( in case no connections in pool are free ) 2.避免不必要的等待来自数据库池的连接,避免多个线程之间的冲突或等待时间(如果池中的连接没有空闲)

so in my scenario, would DB connection pool really be needed or would I face any challenges in long run or would that just be a nice to have feature? 所以在我的场景中,是否真的需要数据库连接池,或者从长远来看我是否会面临任何挑战,或者只是一个很好的功能?

Connection Pools make sense in case of web apps since you don't know number of requests/threads in advance but I am not sure about any advantage for standalone apps with fixed max threads. 连接池在Web应用程序的情况下是有意义的,因为您事先不知道请求/线程的数量,但我不确定具有固定最大线程的独立应用程序的任何优势。

I am thinking of using C3P0 connection pools if needed. 我想如果需要使用C3P0连接池。

Using a connection pool has a few advantages over not using one; 使用连接池比不使用连接池有一些优点;

  1. At thread start time, if a connection is available in the pool then the thread will not block to open a new connection on start. 在线程启动时, 如果连接在池中可用 ,则线程不会阻止打开在启动一个新的连接。
  2. A connection can be returned to the pool when one of your threads completes, this reduces the load on the database server as client connections persist (through the application execution); 当一个线程完成时,可以将连接返回到池,这可以减少客户端连接持续存在时的数据库服务器负载(通过应用程序执行); instead of being set up and torn down multiple times 而不是多次设置和拆除

Simply limit the max number of connections in your pool to match your mentioned database limits and make sure you return your connections the pool (with c3p0 I believe you just close the Connection , which is wrapped by their PooledConnection ). 只需限制池中的最大连接数以匹配您提到的数据库限制,并确保将连接返回到池中(使用c3p0我相信您只需close Connection ,它由PooledConnection包装)。

Generally, a connection pool reduces the time when establishing a connection to the database, because it reuses connections, ie, instead of opening and closing connections, it opens several connections and manages these connections (eg, closes the connection after a specific amount of time, always have a pool of connections available, ...). 通常,连接池减少了建立与数据库的连接的时间,因为它重用连接,即不是打开和关闭连接,而是打开多个连接并管理这些连接(例如,在特定时间后关闭连接) ,总是有一个连接池,...)。

That said, I'm not sure that your application would benefit from a connection pool (maybe you want to benchmark it). 也就是说,我不确定您的应用程序是否会从连接池中受益(可能您希望对其进行基准测试)。 If you have a standalone application with a fixed number of threads, the performance of the application might benefit from a connection pool, eg, if connections can be re-used. 如果您有一个具有固定线程数的独立应用程序,则应用程序的性能可能会受益于连接池,例如,如果可以重新使用连接。 That means, if your application starts all threads at the beginning and finalizes each computation and closes the connection, a connection pool may not help. 这意味着,如果您的应用程序在开始时启动所有线程并完成每个计算并关闭连接,则连接池可能无济于事。 But if the threads are randomly started and closed that might be of benefit. 但是如果线程是随机启动和关闭的话,那可能是有益的。

Regarding the connection pool implementation to use, personally I used C3P0 and HikariCP ( https://brettwooldridge.github.io/HikariCP/ ). 关于要使用的连接池实现,我个人使用了C3P0和HikariCP( https://brettwooldridge.github.io/HikariCP/ )。 I can highly recommend the latter - but that's just a personal opinion. 我强烈推荐后者 - 但这只是个人观点。

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

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