Spring之防止连接泄露的处理办法
来源:互联网 发布:深圳青年旅舍 知乎 编辑:程序博客网 时间:2024/05/22 14:31
连接泄漏
在开发过程中直接连接数据库获取连接,使用完之后不进行及时的关闭连接,连接会一直处于激活状态,就会造成连接泄露,对系统和数据库都会带来一定的压力和负担。
我们写了一个方法让主线程睡眠的形式来模拟多线程环境下的模拟
package com.zhu.service;import java.sql.Connection;import java.sql.SQLException;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.datasource.DataSourceUtils;import org.springframework.stereotype.Service;import com.alibaba.druid.pool.DruidDataSource;@Servicepublic class TestJdbcService { @Autowired private JdbcTemplate jdbcTemplate; public static class UserServiceRunner extends Thread{ private TestJdbcService testJdbcService; private String name; public UserServiceRunner(TestJdbcService testJdbcService,String name) { this.testJdbcService = testJdbcService; this.name = name; } @Override public void run() { testJdbcService.logon(name); } public long getThreadID() { return Thread.currentThread().getId(); } } public void logon(String name){ try { Connection connect = jdbcTemplate.getDataSource().getConnection(); String sql = "update zhu_test set last_time = ? where name = ?"; jdbcTemplate.update(sql, System.currentTimeMillis(),name); Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } } public static void sleep(long time){ try { Thread.sleep(time); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void reportConn(DruidDataSource dataSource){ System.out.println("连接数[active]-[" + dataSource.getActiveCount()+"]"); } public static void asynclogin(TestJdbcService testJdbcService,String name){ UserServiceRunner runner = new UserServiceRunner(testJdbcService,name); System.err.println(Thread.currentThread().getName()+"ID:"+runner.getThreadID()); runner.start(); } public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("file:src/main/resources/applicationContext.xml"); TestJdbcService testJdbcService = (TestJdbcService) ctx.getBean("testJdbcService"); DruidDataSource druidDataSource = (DruidDataSource) ctx.getBean("dataSource"); testJdbcService.reportConn(druidDataSource); testJdbcService.asynclogin(testJdbcService,"tom"); testJdbcService.sleep(500); testJdbcService.reportConn(druidDataSource); testJdbcService.sleep(2000); testJdbcService.reportConn(druidDataSource); testJdbcService.asynclogin(testJdbcService,"jack"); testJdbcService.sleep(500); testJdbcService.reportConn(druidDataSource); testJdbcService.sleep(2000); testJdbcService.reportConn(druidDataSource); }}
运行结果:
由于我使用的是阿里的数据源,没有找到获取空闲连接的方法
分析如下(引自网络)
解决办法:
①使用DataSourceUtils
将上述获取连接的方式改为DataSourceUtils的形式来获取连接。
public void logon(String name){ try { //Connection connect = jdbcTemplate.getDataSource().getConnection(); Connection connect = DataSourceUtils.getConnection(jdbcTemplate.getDataSource()); String sql = "update zhu_test set last_time = ? where name = ?"; jdbcTemplate.update(sql, System.currentTimeMillis(),name); Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } }
然后查看运行结果:
并没有数据连接泄露。
但是这样仅仅是在配置事务的时候不会出现连接泄露,如果将事务注释之后,还是会出现一定的连接泄漏。
将配置文件中的事务注释之后,执行结果
分析如下(引自网络)
杜绝上述情况连接泄露的方式就是手动关闭连接。
②使用TransactionAwareDataSourceProxy
如果不得已要显式获取数据连接,除了使用 DataSourceUtils 获取事务上下文绑定的连接外,还可以通过 TransactionAwareDataSourceProxy 对数据源进行代理。数据源对象被代理后就具有了事务上下文感知的能力,通过代理数据源的 getConnection() 方法获取的连接和使用 DataSourceUtils.getConnection() 获取连接的效果是一样的。
将配置文件中的配置修改一下
<!-- 数据源默认将autoCommit设置为true --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://172.16.14.40:3306/zhu" p:username="zhu" p:password="zhu" /><!-- ①对数据源进行代理--><bean id="dataSourceProxy" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy" p:targetDataSource-ref="dataSource"/><!-- ②直接使用数据源的代理对象--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSourceProxy"/>
打开事务,运行结果
没有连接泄露!
在了解数据连接泄露原理之后,触类旁通,我们可以推及其他的框架。
0 0
- Spring之防止连接泄露的处理办法
- Spring配置防止内存泄露
- spring中防止内存泄露的监听器—IntrospectorCleanupListener
- 宽带连接错误的处理办法
- STL之防止内存泄露
- c# 防止内存泄露 内存溢出 之using的用法
- 内存泄露的查找办法
- Spring 刷新Introspector防止内存泄露
- Spring 刷新Introspector防止内存泄露
- spring 监听器 IntrospectorCleanupListener 防止内存泄露
- Spring刷新Introspector防止内存泄露
- DDR2的ECC处理办法之二
- 防止sql注入的办法
- 防止卡帧的办法
- 防止查看源代码的办法
- tcp连接数 处理办法
- "无法删除当前已连接的用户"处理办法
- 无法删除当前已连接的用户处理办法
- JAVA使用ZeroMQ优秀的使用(从0到1)
- 51nod 1714 B君的游戏
- 四种第三方登录
- Missing letters
- opengl投影透视矩阵的个人总结
- Spring之防止连接泄露的处理办法
- JS高级技巧学习小结
- 在Win2008下配置iis7绑定域名后出现500错误的解决办法
- 如何自己制作身份证扫描件?
- struts2.0经典标签---级联select--
- 数据库水平切分的实现原理解析——分库,分表,主从,集群,负载均衡器
- 彻底理解webservice SOAP WSDL
- 在IDEA里创建Spring Boot项目
- impala 导出查询结果到文件