Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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. I have checked the inactive session using the below query in oracle.

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

How to kill all the inactive session through hibernate. Can anyone help me in resolving this issue.

like image 978
Badal Avatar asked Jun 24 '15 11:06

Badal


People also ask

What happens to INACTIVE sessions in Oracle?

Just before the oracle executable executes a read to get the next "command" that it should execute for its session, it will set its session's state to INACTIVE . After the read completes, it will set it to ACTIVE . It will remain in that state until it is done executing the requested work.

What happens if Hibernate session is not closed?

When you don't close your Hibernate sessions and therefore do not release JDBC connections, you have what is typically called Connection leak. So, after a number of requests (depending on the size of your connection pool) the server will not be able to acquire a connection to respond your request.

Why there are INACTIVE sessions in Oracle?

It just means that someone is logged in but not executing SQL right at that instant. Your very own session is "INACTIVE" everytime you are in the process of typing in a query via sqlplus. It is a normal state to be in.


1 Answers

make sure that you are not creating the sessionFactory several times.

In your finally code try the following:

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. 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...

like image 193
Paizo Avatar answered Oct 04 '22 18:10

Paizo