在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自动事务代理……配置文件如下……
1     2         class="org.springframework.jndi.JndiObjectFactoryBean">
3         
4             java:comp/env/SqlServer
5         
6         
7             true
8         
9     
10
11     
12     13         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
14         
15             
16         
17         
18             
19                 
20                     org.hibernate.dialect.SQLServerDialect
21                 
22                 
23                 true
24             

25         
26         
27             
28                 ./Message.hbm.xml
29                 ./Setting.hbm.xml
30                 ./Admin.hbm.xml
31             

32         
33     
34
35     
36     37         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
38         
39             
40         
41     
42
43     
44
45     46         class="org.springframework.transaction.interceptor.TransactionInterceptor">
47         
48             
49         
50         
51         
52             
53                 PROPAGATION_REQUIRED
54                 PROPAGATION_REQUIRED
55                 PROPAGATION_REQUIRED
56                 PROPAGATION_REQUIRED
57                 PROPAGATION_REQUIRED
58                 PROPAGATION_REQUIRED
59             

60         
61     
62
63     
64     65         class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
66         
67             
68                 *DAO
69                 gotoPage
70                 find
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:残梦追月