java JDBC 到 MySQL 的持久连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4209454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A persistent connection with JDBC to MySQL
提问by rook
I have an application that connects to MySQL using JDBC. There are cases where the JDBC connection lies idle for hours (maybe even days) and its loosing its connection to MySQL and then excepts when it tries to execute a query. What is the best solution for this?
我有一个使用 JDBC 连接到 MySQL 的应用程序。在某些情况下,JDBC 连接闲置数小时(甚至数天)并失去与 MySQL 的连接,然后在尝试执行查询时除外。什么是最好的解决方案?
回答by BalusC
Keeping the connection open for an undertemined time is a bad practice. The DB will force a close when it's been open for a too long time. You should write your JDBC code so that it always closes the connection (and statement and resultset) in the finally
block of the very same try
block where you've acquired them in order to prevent resource leaking like this.
保持连接打开一段时间是一种不好的做法。当数据库打开时间过长时,它会强制关闭。您应该编写 JDBC 代码,以便它始终在您获取它们的finally
块的块中关闭连接(以及语句和结果集),try
以防止资源像这样泄漏。
However, acquiring the connection on every hiccup is indeed a pretty expensive task, so you'd like to use a connection pool. Decent connection pools will manage the opening, testing, reusing and closing the connections themselves. This does however not imply that you can change your JDBC code to never close them. You still need to close them since that would actually release the underlying connection back to the pool for future reuse.
但是,在每次打嗝时获取连接确实是一项非常昂贵的任务,因此您希望使用连接池。体面的连接池将自行管理连接的打开、测试、重用和关闭。然而,这并不意味着您可以更改 JDBC 代码以永远不关闭它们。您仍然需要关闭它们,因为这实际上会将底层连接释放回池以供将来重用。
There are several connection pools, like Apache DBCPwhich is singlethreaded and thus poor in performance, C3P0which is multithreaded and performs better, and Tomcat JDBCfor the case that you're using Tomcat and wouldn't like to use the builtin DBCP due to bad performance.
有几个连接池,如Apache的DBCP这是单线程,因此在表现不佳,C3P0这是多线程的,并执行好,Tomcat的JDBC的情况下,你使用的是Tomcat,不会像使用内置的DBCP因糟糕的表现。
You can create connection pools programmatically, here's an example with C3P0:
您可以通过编程方式创建连接池,以下是 C3P0 的示例:
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/dbname");
dataSource.setUser("username");
dataSource.setPassword("password");
Do it once during application's startup, then you can use it as follows:
在应用程序启动时执行一次,然后您可以按如下方式使用它:
Connection connection = null;
// ...
try {
connection = dataSource.getConnection();
// ...
} finally {
// ...
if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
}
When you're running inside a JNDI-capable container like a servletcontainer (e.g. Tomcat), then you can also declare it as a java.sql.DataSource
(Tomcat specific manual here). It will then use the servletcontainer-provided connection pooling facilities. You can then acquire the datasource as follows:
当您在支持 JNDI 的容器(如 servletcontainer(例如 Tomcat))中运行时,您还可以将其声明为java.sql.DataSource
(此处为Tomcat 特定手册)。然后它将使用 servletcontainer 提供的连接池工具。然后,您可以按如下方式获取数据源:
DataSource dataSource = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/YourDataSourceName");
回答by MBCook
There are libraries, such as Apache's DBCPwhich can do connection pooling. A part of this is they can be setup to automatically test the connection when you go to use it (such as "SELECT NOW() FROM DUAL", or something else harmless) and automatically re-establish the connection transparently if necessary, allowing your application to pretend that the connection is everlasting.
有一些库,例如Apache 的 DBCP,可以进行连接池。其中一部分是它们可以设置为在您使用它时自动测试连接(例如“SELECT NOW() FROM DUAL”或其他无害的东西)并在必要时透明地自动重新建立连接,允许您应用程序假装连接是永恒的。
回答by icyrock.com
Check here:
检查这里:
Basically, you should use DataSource and always do a getConnection() before using it. DataSource, unless there's something terribly wrong, will reconnect if necessary.
基本上,您应该使用 DataSource 并在使用它之前始终执行 getConnection() 。除非出现严重错误,否则数据源会在必要时重新连接。