当绑定到weblogic JTA时,Hibernate不会释放weblogic数据源的连接
在应用程序中,我们试图将weblogic JTA绑定到hibernate会话。我们的应用程序托管在Weblogic 12C中,我们在Weblogic中配置了一个数据源,最小连接数为1,最大连接数为150。这是我们的hibernate.cfg.xml配置。我们正在使用hibernate 5.4.10当绑定到weblogic JTA时,Hibernate不会释放weblogic数据源的连接,hibernate,transactions,connection-pooling,weblogic12c,Hibernate,Transactions,Connection Pooling,Weblogic12c,在应用程序中,我们试图将weblogic JTA绑定到hibernate会话。我们的应用程序托管在Weblogic 12C中,我们在Weblogic中配置了一个数据源,最小连接数为1,最大连接数为150。这是我们的hibernate.cfg.xml配置。我们正在使用hibernate 5.4.10 <property name="connection.datasource">myDataSource</property> <property
<property name="connection.datasource">myDataSource</property>
<property name="jndi.class">weblogic.jndi.WLInitialContextFactory</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.transaction.jta.platform">org.hibernate.engine.transaction.jta.platform.internal.WeblogicJtaPlatform</property>
<property name="hibernate.current_session_context_class">jta</property>
<property name="hibernate.transaction.coordinator_class">jta</property>
这里我们使用的是AutoconnectionClose
istrue
然后我们进行手术
public Entity importEntity(Entity entity) throws CreateException, UpdateException {
if(logger.isDebugEnabled()) logger.debug("Importing entity : " + entity.getClassName());
SessionImpl session = null;
try{
session = (SessionImpl) getSession();
beginTransaction(session);
session.setCacheMode(CacheMode.IGNORE);
//Save the entity values to the hibernate cache
session.saveOrUpdate(entity);
// Syncs the hibernate cache with the database
session.flush();
return entity;
}catch (HibernateException hbe){
throw new CreateException(hbe,"sys.entity.create", new Object[]{entity.getClassName()} ,
"The records could not be created.");
} finally{
cleanUp(session);
}
}
然后进行清理
private void cleanUp(Session session){
if(isAutoConnectionClose){
try{
if(session != null){
SessionImpl sessionImpl=(SessionImpl)session;
Connection conn=sessionImpl.connection();
conn.close();
session.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
我们已从日志中验证打开和关闭会话的数量是否匹配,即我们正在关闭所有打开的连接。
谢谢“活动连接当前计数”显示从池到数据库建立的连接数,而不是从应用程序到池或数据库建立的连接数。当应用程序关闭连接时,连接将被释放并返回到连接池。连接池是唯一决定是否关闭与数据库建立的物理连接的池。它可以减少未使用的连接数,以减少初始连接数。您最多可以打开150个连接,这是一个巨大的数目。很抱歉,回复太晚。我们发现了问题,这是多种因素综合作用的结果。连接池计数非常高,缺少应用程序的提交,这导致连接未被释放并保持活动状态。通过减少最大连接数并添加提交,连接被释放,我们能够以最佳方式使用。谢谢@EmmanuelCollin的建议。
private void cleanUp(Session session){
if(isAutoConnectionClose){
try{
if(session != null){
SessionImpl sessionImpl=(SessionImpl)session;
Connection conn=sessionImpl.connection();
conn.close();
session.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}