Hi to all, this is my first post. I'm working in a project using Struts 2.1.8 and Hibernate JPA . My application works fine, i'm using c3po also. The problem is after a while because it seems that hibernate leaves a lot of open sessions in the database (Oracle 9i) that are inactive, so suddenly i'm navigating in the application and it appears the exception IllegalStateException: Session is closed! or EntityManager is closed and it looks like it can't get an active connection because there are so many open sessions in the database, so the only way to solve the situation is to restart database server or kill the inactive sessions in Oracle.

I'm not using anything special where i initialize EntityManagerFactory or EntityManager as you can see:

EntityManager entityManager;
EntityManagerFactory factory;
if (factory == null || !factory.isOpen()) {
                factory = Persistence.createEntityManagerFactory("GTBancaPU");
            }
            entityManager = factory.createEntityManager();

            try {
                if(!entityManager.isOpen()) {
                    factory = Persistence.createEntityManagerFactory("GTBancaPU");
                    entityManager = factory.createEntityManager();
                }
                entityManager.createQuery("FROM WbkTiposTransaccion").getResultList();
            } catch(Exception ex) {
                factory = Persistence.createEntityManagerFactory("GTBancaPU");
                entityManager = factory.createEntityManager();
            }
            ((BaseAction)invokedAction.getAction()).setEntityMngr(entityManager);
           
            transaction = entityManager.getTransaction();

So i initialize EntityManagerFactory just once in my entire application and EntityManager is initialized on each request (at the beginning) and closed at the end of the request with the next code:

if (isBaseAction && transaction.isActive()) {
                entityManager.getTransaction().commit();
            }

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

Do you know why hibernate leaves open sessions in my database? Also when i query the database to see the query that inactive sessions executed i get that:

select a.sid, a.serial#, b.sql_text from v$session a, v$sqlarea b
    where a.sql_address=b.address and a.MACHINE IN( 'MyMachine'  );

10    1229    SELECT VALUE FROM NLS_INSTANCE_PARAMETERS WHERE PARAMETER ='NLS_DATE_FORMAT'
13    601    SELECT VALUE FROM NLS_INSTANCE_PARAMETERS WHERE PARAMETER ='NLS_DATE_FORMAT'

Hibernate queries NLS_INSTANCE_PARAMETERS and the result of that query is null.


Regards.


--
Oscar Calderón
SCJP 6