在我的 hibernate 应用程序中,我编写了以下用于将 EmployeeRegistration 对象保存到 oracle 数据库中的代码。
public Integer submitDetails(EmployeeRegistration es)
{
Session session = factory.openSession();
Transaction tx = null;
Integer employeeID = null;
try
{
tx = session.beginTransaction();
employeeID = (Integer)session.save(es);
session.flush();
tx.commit();
}
catch(HibernateException e)
{
if(tx != null)
{
tx.rollback();
}
e.printStackTrace();
}
finally
{
if(session.isOpen()) {
session.close();
}
}
return employeeID;
}
关闭 session 后,它会在 oracle 数据库中保留非 Activity session 。我已经使用 oracle 中的以下查询检查了非 Activity session 。
SQL> select USERNAME,COUNT(*) FROM V$SESSION WHERE STATUS='INACTIVE' GROUP BY USERNAME ;
如何通过 hibernate 终止所有非 Activity session 。谁能帮我解决这个问题。
最佳答案
确保您没有多次创建 sessionFactory
。
在您的 finally
代码中尝试以下操作:
if (session != null && session.isOpen()) {
session.flush();
session.close();
}
每次获得 session 时,都会根据需要创建新的数据库连接(即:事务已启动)。最好使用连接池来克服这种行为。
编辑 来自 docs
Hibernate's own connection pooling algorithm is however quite rudimentary. It is intended to help you get started and is not intended for use in a production system or even for performance testing.
我建议您在处理库创建者定义的基本问题之前使用连接池...
关于java - 即使在关闭 session 后,Hibernate 也会在 oracle db 中保持非 Activity session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31024919/