在Spring+hibernate中不能释放tomcat连接池中连接的解决办法
来源:百度文库 编辑:神马文学网 时间:2024/04/25 12:31:35
使用jdbc连接数据库所有功能都没问题,发布到tomcat中也没问题,可是如果使用tomcat的数据源,来连接数据库,开始很正常,但是刷新几次就会出现这个异常……
2008-04-26 22:35:40,812 WARN [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 0, SQLState: null
2008-04-26 22:35:40,812 ERROR [org.hibernate.util.JDBCExceptionReporter] - Cannot get a connection, pool error Timeout waiting for idle object
2008-04-26 22:35:40,812 WARN [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 0, SQLState: null
2008-04-26 22:35:40,812 ERROR [org.hibernate.util.JDBCExceptionReporter] - Cannot get a connection, pool error Timeout waiting for idle object
2008-04-26 22:35:40,812 ERROR [com.dao.MessageDAO] - find all failed
org.springframework.jdbc.UncategorizedSQLException: Hibernate operation: Cannot open connection; uncategorized SQLException for SQL [???]; SQL state [null]; error code [0]; Cannot get a connection, pool error Timeout waiting for idle object; nested exception is org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot get a connection, pool error Timeout waiting for idle object
Caused by:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot get a connection, pool error Timeout waiting for idle object
at org.apache.tomcat.dbcp.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:104)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)
……………………
开始弄了半天也不知道怎么回事,后来发现是我自己写的分页代码有问题……原来的代码如下:
1 /**
2 * 自定义的,用属性模糊查询
3 *
4 * */
5 public List find(String propertyName, Object value) {
6
7 log.debug("finding Message instance with property: " + propertyName
8
9 + ", value: " + value);
10
11 try {
12
13 String queryString = "from Message as model where model."
14
15 + propertyName + " like "+value+" order by model.time desc";
16
17 return getHibernateTemplate().find(queryString);
18
19 } catch (RuntimeException re) {
20
21 log.error("find by property name failed", re);
22
23 throw re;
24
25 }
26
27 }
28
29
30 /**
31 *
32 * 自定义的方法,获取指定页的数据
33 *
34 * */
35 public List gotoPage(int page,int pageSize){
36
37
38
39 int totItem = this.findAll().size();//记录总条数
40
41 int pageNum = totItem / pageSize +1;//总页数
42
43 int begin = 0;//当前起始记录数
44
45
46 begin=page*pageSize-pageSize+1; //计算当前起始位置
47
48
49 Session s =this.getSession();
50
51 String hql = "from Message message order by message.time desc";
52
53 Query q =s.createQuery(hql);
54
55 q.setFirstResult(begin);
56
57 q.setMaxResults(pageSize);
58
59 return q.list();
60
61 }
在这句中:
Session s =this.getSession();
String hql = "from Message message order by message.time desc";
Query q =s.createQuery(hql);
查询数据时,Spring并不能够自动管理连接,也就是说,在使用中这几句代码重视不段的获取数据库的连接,每调用一次就申请一个连接……直到 tomcat连接池中的连接耗尽……所以就再也申请不到连接了……出现了这个异常,解决办法是使用事务来管理这段代码,让Spring自动管理这段代码中申请的连接。我使用了Spring AOP自动事务代理……配置文件如下……
12 class="org.springframework.jndi.JndiObjectFactoryBean">
3
4java:comp/env/SqlServer
5
6
7true
8
9
10
11
1213 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
14
15
16
17
18
19
20 org.hibernate.dialect.SQLServerDialect
21
22
23true
24
25
26
27
32
33
34
35
3637 class="org.springframework.orm.hibernate3.HibernateTransactionManager">
38
39
40
41
42
43
44
4546 class="org.springframework.transaction.interceptor.TransactionInterceptor">
47
48
49
50
51
52
53PROPAGATION_REQUIRED
54PROPAGATION_REQUIRED
55PROPAGATION_REQUIRED
56PROPAGATION_REQUIRED
57PROPAGATION_REQUIRED
58PROPAGATION_REQUIRED
59
60
61
62
63
6465 class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
66
67
72
73
74
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
OK,问题成功解决!速度好像还快些!
By:残梦追月
2008-04-26 22:35:40,812 WARN [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 0, SQLState: null
2008-04-26 22:35:40,812 ERROR [org.hibernate.util.JDBCExceptionReporter] - Cannot get a connection, pool error Timeout waiting for idle object
2008-04-26 22:35:40,812 WARN [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 0, SQLState: null
2008-04-26 22:35:40,812 ERROR [org.hibernate.util.JDBCExceptionReporter] - Cannot get a connection, pool error Timeout waiting for idle object
2008-04-26 22:35:40,812 ERROR [com.dao.MessageDAO] - find all failed
org.springframework.jdbc.UncategorizedSQLException: Hibernate operation: Cannot open connection; uncategorized SQLException for SQL [???]; SQL state [null]; error code [0]; Cannot get a connection, pool error Timeout waiting for idle object; nested exception is org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot get a connection, pool error Timeout waiting for idle object
Caused by:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot get a connection, pool error Timeout waiting for idle object
at org.apache.tomcat.dbcp.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:104)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)
……………………
开始弄了半天也不知道怎么回事,后来发现是我自己写的分页代码有问题……原来的代码如下:
1 /**
2 * 自定义的,用属性模糊查询
3 *
4 * */
5 public List find(String propertyName, Object value) {
6
7 log.debug("finding Message instance with property: " + propertyName
8
9 + ", value: " + value);
10
11 try {
12
13 String queryString = "from Message as model where model."
14
15 + propertyName + " like "+value+" order by model.time desc";
16
17 return getHibernateTemplate().find(queryString);
18
19 } catch (RuntimeException re) {
20
21 log.error("find by property name failed", re);
22
23 throw re;
24
25 }
26
27 }
28
29
30 /**
31 *
32 * 自定义的方法,获取指定页的数据
33 *
34 * */
35 public List gotoPage(int page,int pageSize){
36
37
38
39 int totItem = this.findAll().size();//记录总条数
40
41 int pageNum = totItem / pageSize +1;//总页数
42
43 int begin = 0;//当前起始记录数
44
45
46 begin=page*pageSize-pageSize+1; //计算当前起始位置
47
48
49 Session s =this.getSession();
50
51 String hql = "from Message message order by message.time desc";
52
53 Query q =s.createQuery(hql);
54
55 q.setFirstResult(begin);
56
57 q.setMaxResults(pageSize);
58
59 return q.list();
60
61 }
在这句中:
Session s =this.getSession();
String hql = "from Message message order by message.time desc";
Query q =s.createQuery(hql);
查询数据时,Spring并不能够自动管理连接,也就是说,在使用中这几句代码重视不段的获取数据库的连接,每调用一次就申请一个连接……直到 tomcat连接池中的连接耗尽……所以就再也申请不到连接了……出现了这个异常,解决办法是使用事务来管理这段代码,让Spring自动管理这段代码中申请的连接。我使用了Spring AOP自动事务代理……配置文件如下……
1
3
4
5
6
7
8
9
10
11
12
14
15
16
17
18
19
20 org.hibernate.dialect.SQLServerDialect
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
38
39
40
41
42
43
44
45
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
OK,问题成功解决!速度好像还快些!
By:残梦追月
在Spring+hibernate中不能释放tomcat连接池中连接的解决办法
在tomcat中实现https安全连接的方法
网络连接中“本地连接”不见了的解决办法
spring中使用proxool作数据源连接池的配置
在tomcat中实现https安全连接的方法 - 中文JAVA技术网
在Spring中集成Hibernate事务
在Spring中配置Hibernate事务
在Spring中配置Hibernate事务
Hibernate连接池配置
tomcat连接池的配置与使用
转hibernate连接池配置
Hibernate连接池配置1
如何在ASP中连接MySQL数据库
Hibernate从2升级到3不支持Oracle8外连接( )的解决办法
tomcat连接池的配置与使用(mysql)
tomcat连接池泄露的监控和解决
tomcat连接池的配置与使用(mysql)1
tomcat连接池泄露的监控和解决
apache2连接tomcat
IE 被迫连接某网站的解决办法
TCP连接中网络断开后重新连接后的状态 - cyp207的专栏 - CSDNBlog
TCP连接中网络断开后重新连接后的状态
在Spring的项目中配置Log4j
Tomcat最大连接数据设置