简体   繁体   English

即使在关闭会话之后,Hibernate也会在oracle db中保持非活动会话

[英]Hibernate keeping inactive session in oracle db even after closing the session

In my hibernate application I have written below code for saving EmployeeRegistration object into oracle db. 在我的hibernate应用程序中,我编写了下面的代码,用于将EmployeeRegistration对象保存到oracle db中。

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;
}

After closing session, it keeps inactive sessions in oracle db. 关闭会话后,它会在oracle db中保持非活动会话。 I have checked the inactive session using the below query in oracle. 我在oracle中使用以下查询检查了非活动会话。

SQL> select USERNAME,COUNT(*) FROM V$SESSION WHERE STATUS='INACTIVE' GROUP BY USERNAME ;

How to kill all the inactive session through hibernate. 如何通过hibernate杀死所有非活动会话。 Can anyone help me in resolving this issue. 任何人都可以帮我解决这个问题。

make sure that you are not creating the sessionFactory several times. 确保您没有多次创建sessionFactory

In your finally code try the following: 在您的finally代码中尝试以下操作:

if (session != null && session.isOpen()) {
    session.flush();
    session.close();
}

Every time you get a Session a new database connection is created if needed (es: transaction started). 每次获得会话时,如果需要,都会创建一个新的数据库连接(例如:事务已启动)。 It is better to use a connection pool to overcome this behavior. 最好使用连接池来克服此行为。

Edit from the docs 文档 编辑

Hibernate's own connection pooling algorithm is however quite rudimentary. 然而,Hibernate自己的连接池算法非常简陋。 It is intended to help you get started and is not intended for use in a production system or even for performance testing. 它旨在帮助您入门,不打算在生产系统中使用,甚至不用于性能测试。

I would suggest you to use a connection pooling before dealing with issues from something defined rudimentary from the creators of the library... 我建议你在处理来自图书馆创作者的基本问题之前使用连接池...

How many inactive sessions do you notice in the Oracle database for this user? 您在Oracle数据库中为此用户注意了多少个非活动会话?

If you see one, then it might be related to the SQL terminal session that you have open with the database. 如果您看到一个,那么它可能与您使用数据库打开的SQL终端会话有关。

If you see it to be more than one and the count keeps increasing every time you execute your code, then you are not releasing the connections properly. 如果您看到它不止一个,并且每次执行代码时计数都会不断增加,那么您就不会正确释放连接。 This could be a connection leak. 这可能是连接泄漏。

Or it could be that you are indeed using a connection pool and the inactive connections are related to the connections it manages. 或者可能是您确实使用了连接池,并且非活动连接与它管理的连接相关。 Inactive here means that they are currently not executing an action. 此处不活动意味着它们当前未执行操作。 It does not mean there is a connection leak. 这并不意味着存在连接泄漏。 Posting your config will help shed some light. 发布您的配置将有助于解决问题。 You can validate if you are using a connection pool by either generating a stack trace or debugging through the code. 您可以通过生成堆栈跟踪或通过代码进行调试来验证您是否正在使用连接池。

Also, make sure that there is no other application that is accessing the database with the same credentials. 此外,请确保没有其他应用程序使用相同的凭据访问数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM