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. <br>
<div class="gmail_quote">
<br>I'm not using anything special where i initialize EntityManagerFactory or EntityManager as you can see:<br><br>EntityManager entityManager;<br>EntityManagerFactory factory;<br>if (factory == null || !factory.isOpen()) {<br>
factory = Persistence.createEntityManagerFactory("GTBancaPU");<br> }<br> entityManager = factory.createEntityManager();<br><br> try {<br> if(!entityManager.isOpen()) {<br>
factory = Persistence.createEntityManagerFactory("GTBancaPU");<br> entityManager = factory.createEntityManager();<br> }<br> entityManager.createQuery("FROM WbkTiposTransaccion").getResultList();<br>
} catch(Exception ex) {<br> factory = Persistence.createEntityManagerFactory("GTBancaPU");<br> entityManager = factory.createEntityManager();<br> }<br> ((BaseAction)invokedAction.getAction()).setEntityMngr(entityManager);<br>
<br> transaction = entityManager.getTransaction();<br><br>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:<br>
<br>if (isBaseAction && transaction.isActive()) {<br> entityManager.getTransaction().commit();<br> }<br><br>if (entityManager != null && entityManager.isOpen()) {<br> entityManager.close();<br>
}<br><br>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:<br><br>select a.sid, a.serial#, b.sql_text from v$session a, v$sqlarea b<br>
where a.sql_address=b.address and a.MACHINE IN( 'MyMachine' );<br><br>10 1229 SELECT VALUE FROM NLS_INSTANCE_PARAMETERS WHERE PARAMETER ='NLS_DATE_FORMAT'<br>13 601 SELECT VALUE FROM NLS_INSTANCE_PARAMETERS WHERE PARAMETER ='NLS_DATE_FORMAT'<br clear="all">
<br>Hibernate queries NLS_INSTANCE_PARAMETERS and the result of that query is null.<br><br><br>Regards.<br><br clear="all"></div><br>-- <br>Oscar Calderón<br>SCJP 6 <a href="http://javahowto.net" target="_blank"></a><br>