JdbcTemplate 在使用连接池的情况下,数据库连接仍超出最大回话数可能的原因
2017-06-23 10:13
309 查看
在使用spring框架的时候,会经常用到jdbcTemplate,而可能习惯性的每次创建都是通过
ApplicationContext act = new ClassPathXmlApplicationContext("application-context.xml");
JdbcTemplate jdbcTemplate = (JdbcTemplate) act.getBean("jdbcTemplate");
这样来创建的。
这种方式在连接数据库次数小时,好像并不会出现问题,但是在经常操作库的时候就会发现一会 数据库连接就超出了最大回话数。
解决方式:对ApplicationContext使用单列
/**
*
* @ClassName: ApplicationBean
* @Description: 获取ApplicationContext 实例
* @author wxd
* @date 2017年6月23日 上午1:36:06
*
*/
public class ApplicationBean {
static private ApplicationContext ac = null;
static {
ac = new ClassPathXmlApplicationContext("application-context.xml");
}
private ApplicationBean() {
}
/**
*
* @Title: getAc
* @Description: 获取ApplicationContext对象
* @param @return
* @return ApplicationContext
* @date 2017年6月23日 上午1:37:58
* @author wxd
* @throws
*/
public static ApplicationContext getAc() {
return ac;
}
}然后在使用时只需要通过
ApplicationContext act = ApplicationBean.getAc();
JdbcTemplate jdbcTemplate = (JdbcTemplate) act.getBean("jdbcTemplate");这样就解决了以上问题
ApplicationContext act = new ClassPathXmlApplicationContext("application-context.xml");
JdbcTemplate jdbcTemplate = (JdbcTemplate) act.getBean("jdbcTemplate");
这样来创建的。
这种方式在连接数据库次数小时,好像并不会出现问题,但是在经常操作库的时候就会发现一会 数据库连接就超出了最大回话数。
解决方式:对ApplicationContext使用单列
/**
*
* @ClassName: ApplicationBean
* @Description: 获取ApplicationContext 实例
* @author wxd
* @date 2017年6月23日 上午1:36:06
*
*/
public class ApplicationBean {
static private ApplicationContext ac = null;
static {
ac = new ClassPathXmlApplicationContext("application-context.xml");
}
private ApplicationBean() {
}
/**
*
* @Title: getAc
* @Description: 获取ApplicationContext对象
* @param @return
* @return ApplicationContext
* @date 2017年6月23日 上午1:37:58
* @author wxd
* @throws
*/
public static ApplicationContext getAc() {
return ac;
}
}然后在使用时只需要通过
ApplicationContext act = ApplicationBean.getAc();
JdbcTemplate jdbcTemplate = (JdbcTemplate) act.getBean("jdbcTemplate");这样就解决了以上问题
相关文章推荐
- 超时时间已到。在从池中获取连接之前超时时间已过。出现这种情况可能是因为所有池连接都已被使用并已达到最大池大小
- 超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
- 超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
- [转]超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小
- 超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大
- 超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小
- 超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
- 超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
- 超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大
- 解决办法之“超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小 ”
- 超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
- 出现这种情况可能是因为所有池连接都已被使用并已达到池的最大值
- 超时时间已到。在从池中获取连接之前超时时间已过。出现这种情况可能是因为所有池连接都已被使用并已达到最大池大小。
- 超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
- 超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
- 出现这种情况可能是因为所有池连接都已被使用并已达到池的最大值
- 超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
- 超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
- 超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小
- 超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小