晨钟暮鼓 安之若素:在Spring+hibernate中不能释放tomcat连接池中连接的解决办法
来源:百度文库 编辑:中财网 时间:2024/05/08 15:51:53
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)
……………………
开始弄了半天也不知道怎么回事,后来发现是我自己写的分页代码有问题……原来的代码如下:
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自动事务代理……配置文件如下……
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
By:残梦追月