Expand my Community achievements bar.

JTA Transaction and CUrrent Session

Avatar

Level 1
We have the following problem. We configured hibernate with
jta and when we execute a simple code that performs some hibernate
queries we get the following error:



"org.hibernate.LazyInitializationException: could not
initialize proxy - the owning Session was closed"



We discovered that if we leave the session opened the error
disapears, but on the long run this crashes the server throwing a
JDBC connection error.



We tried to use getCurrentSession() instead of opening the
session manualy but in this case we get the following error:



"org.hibernate.HibernateException : Unable to locate current
UserTransaction"



This its quite strange because the current session should be
bound to the current user transaction and its seems to us that our
hibernate cfg file its ok.



I´am posting part of my hibernate cfg file and the code
that I execute.



hibernate.cfg.xml



<hibernate-configuration>

<session-factory name="java:/hibernate/SessionFactory">



<property
name="hibernate.connection.datasource">java:fiap</property>

<property
name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>



<!-- Enable Hibernate's automatic session context
management -->

<property
name="hibernate.current_session_context_class">jta</property>

<property
name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>

<property
name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>

<property
name="jta.UserTransaction">java:comp/UserTransaction</property>



<!-- <property
name="connection.release_mode">after_statement</property>
-->

<!-- <property
name="hibernate.transaction.flush_before_completion">true</property>-->


<property
name="hibernate.transaction.auto_close_session">true</property>



.......

......





and my java Code:

.....

try {

tx = HUtil.getTx(); //JNDI lookup

tx.begin();

session = HUtil.getSessionFactory().getCurrentSession();
//JNDI Lookup

//session = HUtil.getSessionFactory().openSession();

Query query = session.createQuery("from Persona where
username = ?");

query.setString(0, userName);

persona = (Persona) query.uniqueResult();

query = session.createQuery("from Festival where id = ?");

query.setInteger(0, idFestival);

festival = (Festival) query.uniqueResult();



if (persona != null && festival != null) {

query = session.createQuery("from Inscripcion where
inscribeA = :persona " +

"AND inscriptosEn = :festival");

query.setParameter("persona", persona);

query.setParameter("festival", festival);

inscripcion = (Inscripcion) query.uniqueResult();

........

}

.......

return inscripcion;

} catch (Exception e) {

e.printStackTrace();

throw e;

} finally {

tx.commit();

//session.close();

}





thanks
0 Replies