连接池是否有益于多线程Java程序 [英] Would a connection Pool benefit a multithreaded Java program

查看:111
本文介绍了连接池是否有益于多线程Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个java进程,它启动大约60个线程,每个线程访问一个MySql数据库。

I have a java process that starts about 60 threads that each access a MySql database.

我是否可以使用像C3P0这样的连接池?或者它仅适用于Web应用程序(可扩展到许多用户)?

Would I benefit from using a Connection Pool like C3P0? Or is it meant only for Web apps (that scale to lots of users) ?

今天我们有长期存在的JDBC连接(每个线程一个),我的计划是而是在每次SQL查询/插入之前从连接池获取连接。

Today we have long-living JDBC Connections (one per thread), and my plan was to instead get a Connection from the Connection Pool before every SQL query/insert.

我想知道这是否会使我们的应用程序更稳定?另外,如果我将其配置为与数据库中的最大连接数匹配,那么线程是否必须等到有空闲连接?文档不是很清楚(至少不适合我)。

I wonder whether that would make our application more stable? Also, if I configure it to match the max number of connections in the database, will the thread have to wait until there is a free connection? The documentation isnt very clear (at least not for me).

任何指导都表示赞赏!

推荐答案

暂不考虑运行应用程序的位置以及是否将数据库暴露给互联网,我不认为添加连接池可以解决您的问题,但它可以改善您的应用程序。

Putting aside the questions of where your application is running and whether you have your database exposed to the internet, I don't think adding a connection pool will fix your problem, but it could improve your application.

我猜你在使用数据库连接时发生了虚假错误。我不认识你的特定错误,但这听起来像是某种类型的连接失败,如果你和数据库之间的链接不可靠或很慢,就会发生这种情况。游泳池在这里没有帮助,因为它是一个连接池。一旦你获得连接,你不知道它是否会因为同样的原因而失败。

I'm guessing that your spurious errors are happening when you are using your database connection. I don't recognize your particular error, but it sounds like a connection failure of some sort, which could happen if you had unreliable or slow links between you and the database. The pool wouldn't help here because it is a pool of connections. Once you obtain the connection, you don't know whether it will then fail or not for the same reasons.

但是,如果你确实使用了游泳池,那么你就不要不得不长时间保持连接打开。使用池时,您要求建立连接,如果没有,则会创建一个连接。返回连接后,如果连接暂时没有使用,它可能会(断开连接)并处理掉。除非您的应用程序使用每个连接都是常量,否则这对您的应用程序和服务器都有好处。

However, if you do use a pool, then you don't have to keep the connection open for extended periods. With a pool, you ask for a connection, and one will be created if none is available. After you return the connection, it might be (disconnected) and disposed if it hasn't been used for a while. Unless your application is constant using every connection, then this would be good for both your app and the server.

即使在这里,您还需要做一些额外的事情来处理故障。假设您已经从池中获取了连接,并且随后失败了。您可以关闭它,并向池请求新连接(池中应该有一些API来摆脱该连接。)新连接可能处于更好的状态。

Even here, you have to do something extra to handle the failure. Let's say you have taken a connection from the pool, and it has subsequently failed. You could close it, and ask the pool for a new connection (there should be some API in the pool to get rid of that connection.) A new connection might be in a better state.

最后,考虑一下也许不要在互联网上使用JDBC。正如其他人可能指出的那样,这会让自己面临不必要的风险。也许使用某种Web服务来通过安全的https和更受限制的界面读取和写入数据。

Finally, consider perhaps not using JDBC over the internet. As other people are likely to point out, this is exposing yourself to unnecessary risk. Perhaps use a webservice of some kind to read and write data over secure https and a more restricted interface.

这篇关于连接池是否有益于多线程Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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