mysql持久连接与飞持久连接,与JDBC到MySQL的持久连接
发布日期:2022-02-18 13:20:04 浏览次数:8 分类:技术文章

本文共 2348 字,大约阅读时间需要 7 分钟。

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?

解决方案

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.

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.

There are several connection pools, like Apache DBCP which is singlethreaded and thus poor in performance, C3P0 which is multithreaded and performs better, and Tomcat JDBC for the case that you're using Tomcat and wouldn't like to use the builtin DBCP due to bad performance.

You can create connection pools programmatically, here's an example with 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:

DataSource dataSource = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/YourDataSourceName");

转载地址:https://blog.csdn.net/weixin_28926205/article/details/118843818 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:指令格式实验计算机组成原理,计算机组成原理实验第五讲.ppt
下一篇:江苏开放计算机绘图作业1,江苏开放大学计算机绘图形考作业2.doc

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年03月27日 09时00分40秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

redis持久化, RBD(Redis Database)和AOF(Append Only File) 2019-04-26
boost::bad_function_call用法的测试程序 2019-04-26
boost::function_types::is_callable_builtin用法的测试程序 2019-04-26
boost::function_types::is_member_function_pointer的用法测试程序 2019-04-26
boost::function_types::is_function_pointer用法的测试程序 2019-04-26
SpringMVC框架学习总结 2019-04-26
boost::function_types::is_function_reference的测试程序 2019-04-26
boost::function_types::is_function用法的测试程序 2019-04-26
boost::function_types::is_member_function_pointer用法的测试程序 2019-04-26
boost::geometry::clear用法的测试程序 2019-04-26
asp 指定读取前几条记录 2019-04-26
大数据_Hbase-API访问_Java操作Hbase_MR-数据迁移-代码测试---Hbase工作笔记0017 2019-04-26
大数据_Hbase-内容回顾和补充---Hbase工作笔记0018 2019-04-26
大数据_Hbase-内容回顾_知识点补充_线程安全与wait的区别---Hbase工作笔记0019 2019-04-26
大数据_Hbase-Filter & 索引(优化)_根据column查询---Hbase工作笔记0020 2019-04-26
大数据_MapperReduce_从CSV文件中读取数据到Hbase_自己动手实现Mapper和Reducer---Hbase工作笔记0021 2019-04-26
大数据_MapperReduce_协处理器_类似Mysql的触发器---Hbase工作笔记0024 2019-04-26
大数据_MapperReduce_Hbase的优化_存数据_自动计算分区号 & 自动计算分区键---Hbase工作笔记0027 2019-04-26
大数据_MapperReduce_Hbase的优化_RowKey设计原则---Hbase工作笔记0028 2019-04-26
大数据_MapperReduce_Hbase的优化和Hbase相关面试题_以及hbase的javaapi的一部分源码---Hbase工作笔记0029 2019-04-26