刚过完年,正式上班的头一天就碰到个麻烦的问题...
JBoss错误日志:
Caused by: javax.resource.ResourceException: IJ000453: Unable to get managed connection for java:/abc/ds/cba
at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.getManagedConnection(AbstractConnectionManager.java:390)
at org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.getManagedConnection(TxConnectionManagerImpl.java:368)
at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.allocateConnection(AbstractConnectionManager.java:464)
at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:139)
... 91 more
Caused by: javax.resource.ResourceException: IJ000655: No managed connections available within configured blocking timeout (30000 [ms])
at org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreArrayListManagedConnectionPool.getConnection(SemaphoreArrayListManagedConnectionPool.java:383)
at org.jboss.jca.core.connectionmanager.pool.AbstractPool.getSimpleConnection(AbstractPool.java:397)
at org.jboss.jca.core.connectionmanager.pool.AbstractPool.getConnection(AbstractPool.java:365)
at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.getManagedConnection(AbstractConnectionManager.java:329)
... 94 more
导致以上错误的是下面这个方法:
/**
* 根据HQL查询实体List, 注意是STATELESS的, Hibernate不会维护其状态
* @param hql
* @param params
* @return
*/
public List<T> getEntityListByHqlStateless(final String hql,final Map<String, Object> params) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
@Override
public Object doInHibernate(Session session) throws HibernateException, SQLException {
StatelessSession statelessSession = session.getSessionFactory().openStatelessSession();
Query query = statelessSession.createQuery(hql);
if (params != null) {
Iterator<Entry<String, Object>> iterator = params.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, Object> entry = iterator.next();
String key = entry.getKey();
Object value = entry.getValue();
if(value instanceof Collection) {
query.setParameterList(key, (Collection)value);
}else {
query.setParameter(key, value);
}
}
}
return query.list();
}
});
}
保存业务日志时,需要修改前的业务对象状态,所以使用了StatelessSession
来避免Hiberante维护实体对象的状态。
出现IJ000453, IJ000655
错误,是由于数据库连接池中的connection
被耗尽,无法获取到新的连接导致的。
怀疑是session.getSessionFactory().openStatelessSession()
该方法的问题,难道他每次都去取新的数据库连接?而且还没有正常释放?
尝试将该行代码修改成:
StatelessSession statelessSession = session.getSessionFactory().openStatelessSession(session.connection());
使用当前session的数据库连接,测试通过。
- Hibernate 3.6.10
- Spring 3.2.8
DAO继承的是HibernateDaoSupport
,该方法已不推荐....