-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi,
I get this error message in 2 different ZK events, each running an own java thread, having a session and having non equal transactions.
I use HibernateSessionContextListener, as proposed in Small Talks - Hibernate + ZK.
What am I doing wrong? Or what have i forgotten?
in hibernate cfg file I use...
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.current_session_context_class">thread</property>
best regards,
martin
Hi martin,
You have to open a transaction before executing any sql query.
Or you can add opensessioninview listener in your zk.xml
<!-- Hibernate OpenSessionInView Pattern --> <listener> <description>Hibernate Open Session In View life-cycle</description> <listener-class>org.zkoss.zkplus.hibernate.OpenSessionInViewListener</listener-class> </listener>
/robbie
Hi Robbie,
thats exactly what I'm doing and this is the confusing point...
First I do...
// Start Hibernate
SessionFactory sf = HibernateUtil.getSessionFactory(connectionInfo.getXDB_ConnectionInfo());
Session session = sf.getCurrentSession();
Transaction ta = session.beginTransaction();
Then I do a lot of
sorry, click error..
After beginning a transaction...
I do a lot of
findAll, findById, do some processing of the database objects and write these to an XML structure for output...
and at last I do
ta.commit();
Now, if I call this sequence in ZK twice, both have an active transaction at first. But then I get this Hibernate exception, that the first started transaction is not active :-(
I don't know, how this transaction vanishes, I don't get any log message about this.
What am I doing wrong? Or is there something not possible with Hibernate built in TransactionFactory ...
Do I need a JTA Manager together with Apache Tomcat...
Lots of questions, few answers ...
best regards,
martin
Hi martin,
simply add the following configurations into your zk.xml
<!-- Hibernate SessionFactory lifecycle --> <listener> <description>Hibernate SessionFactory lifecycle</description> <listener-class>org.zkoss.zkplus.hibernate.HibernateSessionFactoryListener</listener-class> </listener> <!-- Hibernate OpenSessionInView Pattern --> <listener> <description>Hibernate Open Session In View life-cycle</description> <listener-class>org.zkoss.zkplus.hibernate.OpenSessionInViewListener</listener-class> </listener> <!-- Hibernate thread session context handler --> <listener> <description>Hibernate thread session context handler</description> <listener-class>org.zkoss.zkplus.hibernate.HibernateSessionContextListener</listener-class> </listener>
And you no longer worry about the problem of transaction, simply invoke org.zkoss.zkplus.hibernate.HibernateUtil to get current session, and do whatever you want as follows:
Session currentSession() { return org.zkoss.zkplus.hibernate.HibernateUtil.currentSession(); } public List findAll() { Session sess = currentSession(); return sess.createQuery("from Event").list(); }
Hi all,
I have a similar problem. I did like in Small Talks - Hibernate + ZK exemple. And it worked well until i add a many-to-one relation in my hibernate mapping. Now i can load my objects with the referenced objects and access it. BUT it seem I can't save a new object (with many-to-one relation) without create a transaction and i don't think it's a good thing to do in zk.
If I do like in the smalltalk exemple: I don’t create a transaction i can see the insert into (…) line in the console BUT its never added to the database.
Why this behavior?
Should I create a transaction then ? When ? Each time ?
My mapping
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="heuresEffectuees.HeuresEffectuees" table="heuresEffectuees"> <composite-id> <key-property name="noBon"/> <key-property name="cipRepondant"/> </composite-id> <property name="nbHeuresEffectuees"/> <many-to-one name="repondant" column="cipRepondant" class="repondants.Repondant" lazy="false" insert="false" update="false"/> </class> </hibernate-mapping>
public class HeuresEffectueesDAO { Session currentSession() { return HibernateUtil.currentSession(); } //!!!!!!!! DON’T WORK !!!!!!!! public void saveOrUpdate(HeuresEffectuees desHeuresEffectuees) { Session sess = currentSession(); sess.saveOrUpdate(desHeuresEffectuees); } //!!!!!!!!!!!! WORK !!!!!!!!!!! public void saveOrUpdate2(HeuresEffectuees desHeuresEffectuees) { Session sess = currentSession(); Transaction tx = sess.beginTransaction(); sess.saveOrUpdate(desHeuresEffectuees); tx.commit(); } public void delete(HeuresEffectuees desHeuresEffectuees) { Session sess = currentSession(); sess.delete(desHeuresEffectuees); } public List findAllByNoBon(String noBon) { Session sess = currentSession(); Criteria criteria = sess.createCriteria(HeuresEffectuees.class); criteria.add(Restrictions.like("noBon", noBon)); return criteria.list(); } public HeuresEffectuees findByNoBonEtRepondant(String noBon, String cipRepondant) { Session sess = currentSession(); return (HeuresEffectuees) sess.get(HeuresEffectuees.class, cipRepondant); } public List findAll() { Session sess = currentSession(); return sess.createQuery("from HeuresEffectuees").list(); } }
@henrichen In this case, the documentation at http://books.zkoss.org/wiki/ZK_Developer%27s_Reference/Integration/Hibernate should be revised to reflect this, either by specifying that the "open session in view" pattern is used in the example, or add the Hibernate beginTransaction and commit method calls to the methods of the EventDAO class.
Asked: 2008-10-28 09:39:36 +0800
Seen: 6,763 times
Last updated: Mar 15 '12