Reputation: 1051
My Hibernate config:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:XE</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.default_schema">gg</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="show_sql">false</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<mapping class="com.greengrass.house.Obj"></mapping>
<mapping class="com.greengrass.house.ObjxProp"></mapping>
<mapping class="com.greengrass.house.Property"></mapping>
<mapping class="com.greengrass.house.EvQue"></mapping>
<mapping class="com.greengrass.house.EvxObj"></mapping>
</session-factory>
</hibernate-configuration>
Part of my main procedure:
Session sess = HibernateUtil.getSessionFactory().openSession();
List<Obj> lobj = sess.createQuery(
"from Obj").list();
sess.beginTransaction();
Obj o2=lobj.get(1);
o2.crEvent("test", 3600, 1); //calling method
sess.getTransaction().commit();
One method of my persisting object:
@Transient
public void crEvent(String speech, int timeout, int maxCnt) {
Session s = HibernateUtil.getSessionFactory().getCurrentSession();
if (!s.getTransaction().isActive()) {
System.out.println("No transaction!!!");
} else {
System.out.println("Exist transaction!!!");
}
Query query = s.createSQLQuery(
"CALL p_event.cr_event(:p_speech, :p_tm, :p_max_cnt)")
.setParameter("p_speech", speech)
.setParameter("p_tm", timeout)
.setParameter("p_max_cnt", maxCnt);
int exRows = query.executeUpdate();
}
So i get this result:
No transaction!!! <--Message from the my method
Exception in thread "main" org.hibernate.HibernateException: createSQLQuery is not valid without active transaction
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:348)
at com.sun.proxy.$Proxy16.createSQLQuery(Unknown Source)
at com.greengrass.house.Obj.crEvent(Obj.java:273)
at com.greengrass.evhandler.GreenEvHandler.main(GreenEvHandler.java:36)
So i don't understand why is HibernateUtil.getSessionFactory().getCurrentSession(); in method crEvent, can't continue active transaction from main procedure?
Upvotes: 1
Views: 1210
Reputation: 19171
Whenever you want to deal with a contextual session in Hibernate (3.0.1+) you should use getCurrentSession()
. You do not need to (nor should you) call openSession()
first. From the manual (v4.3, §3.9.3):
Using the "jta" session context, if there is no Hibernate
Session
associated with the current JTA transaction, one will be started and associated with that JTA transaction the first time you callsessionFactory.getCurrentSession()
.
In contrast openSession
always opens a new session, which you should then close (e.g. with try-with-resources) before it goes out of scope. The session opened by openSession
is not a hibernate contextual session.
You can (usually) see if two session instances are the same by comparing hashCodes
:
Session s1 = HibernateUtil.getSessionFactory().getCurrentSession();
Session s2 = HibernateUtil.getSessionFactory().openSession();
Session s3 = HibernateUtil.getSessionFactory().getCurrentSession();
System.out.println("getCurrent is " + s1.hashCode());
System.out.println("openSession is " + s2.hashCode());
System.out.println("getCurrent is " + s3.hashCode());
s2.close();
outputs:
getCurrent is 392918519
openSession is 1499840045
getCurrent is 392918519
Upvotes: 1