在Java中有多少个JDBC连接? [英] How many JDBC connections in Java?

查看:98
本文介绍了在Java中有多少个JDBC连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由大约15个方法组成的Java程序。并且,这些方法在程序的执行期间被非常频繁地调用。目前,我在每个方法中创建一个新的连接并调用它们上的语句(数据库在网络上的另一台机器上设置)。



我想知道的是:我应该在main方法中只创建一个连接,并将它作为参数传递给所有需要连接对象的方法,因为它将显着减少程序中连接对象的数量,而不是在每种方法中非常频繁地创建和关闭连接。



我怀疑我目前的设计并没有非常有效地使用这些资源,并且有很多改进的空间,考虑到这个程序可能在未来。

解决方案

是的,您应该考虑重新使用连接,而不是每次都创建一个新连接。通常的过程是:




  • 猜测你的数据库可以处理多少同时连接(例如,以每个CPU 2或3开始在数据库机器上,直到你发现这太少或太多 - 它往往取决于磁盘绑定你的查询是什么)

  • 创建:实质上是一个类,你可以在每个方法开始时请求下一个空闲连接,然后在每个方法结束时回传到池中。

  • 您的getFreeConnection()方法需要返回一个空闲连接(如果有),否则(1)创建一个新连接,直到您决定允许的最大连接数,或者(2)if

  • 我建议使用Semaphore类来管理连接;我实际上在我的网站上有一篇关于使用信号量管理资源池的文章


  • 几个实用注意事项:




    • 为了获得最佳性能,您需要小心不要在您实际上不使用连接来运行查询时截断连接。如果您从池中取出一次连接,然后将其传递给各种方法,则需要确保不会意外执行此操作。

    • 不要忘记将连接返回游泳池! (try / finally是您的朋友在这里...)

    • 在许多系统上,您无法保持连接打开永远:O / S将在一些最大时间后关闭它们。因此,在您的返回连接池方法中,您需要考虑退役已存在很长时间的连接(构建某种机制来记住,例如通过

    • 您可以考虑使用预准备的语句。​​
    • >
    • 随着时间的推移,您可能需要调整连接池大小


    I have a Java program consisting of about 15 methods. And, these methods get invoked very frequently during the exeuction of the program. At the moment, I am creating a new connection in every method and invoking statements on them (Database is setup on another machine on the network).

    What I would like to know is: Should I create only one connection in the main method and pass it as an argument to all the methods that require a connection object since it would significantly reduce the number of connections object in the program, instead of creating and closing connections very frequently in every method.

    I suspect I am not using the resources very efficiently with the current design, and there is a lot of scope for improvement, considering that this program might grow a lot in the future.

    解决方案

    Yes, you should consider re-using connections rather than creating a new one each time. The usual procedure is:

    • make some guess as to how many simultaneous connections your database can sensibly handle (e.g. start with 2 or 3 per CPU on the database machine until you find out that this is too few or too many-- it'll tend to depend on how disk-bound your queries are)
    • create a pool of this many connections: essentially a class that you can ask for "the next free connection" at the beginning of each method and then "pass back" to the pool at the end of each method
    • your getFreeConnection() method needs to return a free connection if one is available, else either (1) create a new one, up to the maximum number of connections you've decided to permit, or (2) if the maximum are already created, wait for one to become free
    • I'd recommend the Semaphore class to manage the connections; I actually have a short article on my web site on managing a resource pool with a Semaphore with an example I think you could adapt to your purpose

    A couple of practical considerations:

    • For optimum performance, you need to be careful not to "hog" a connection while you're not actually using it to run a query. If you take a connection from the pool once and then pass it to various methods, you need to make sure you're not accidentally doing this.
    • Don't forget to return your connections to the pool! (try/finally is your friend here...)
    • On many systems, you can't keep connections open 'forever': the O/S will close them after some maximum time. So in your 'return a connection to the pool' method, you'll need to think about 'retiring' connections that have been around for a long time (build in some mechanism for remembering, e.g. by having a wrapper object around an actual JDBC Connection object that you can use to store metrics such as this)
    • You may want to consider using prepared statements.
    • Over time, you'll probably need to tweak the connection pool size

    这篇关于在Java中有多少个JDBC连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆