-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: Create a SubTransaction using Hibernate in EJB Container
PostPosted: Wed Jul 27, 2005 12:56 pm 
Newbie

Joined: Tue Jul 26, 2005 9:12 pm
Posts: 7
Location: California
Hi,
I am working on a project converting EJB CMPs + JDBC code to Hibernate +JDBC codes. And wondering how to create a sub-transaction with Hibernate in CMT env. In EJB CMP this can be achieved by declare the transaction=requiresNew. Is there anything equivallent to this ?

I have read the Hibernate in Action and the documentation, and did not have find any related explainations. I need this as the code is still a mixture of both JDBC SQL code and Hibnerate Code, unless Hibernate code is commited in an new sub-transaction, the JDBC SQL Code can not see the new record/or changes, which will cause the SQL exception in the JDBC Update.

I have tried calling session.flush() and it does not work for me. I have changed the flushmode to COMMIT at the runtime before the Hibernate.save() is called, it still does not work. The following is what I have done:
Code:
   
                session.setFushMode(FlushMode.COMMIT);
                session.save(entity);
                session.flush();
   



The entity here uses all the default mapping, basically a class-name, a table name and properties.

I can work around this by create EJB Stateless session bean and mark the transaction as RequiresNew and use hibernate inside this session bean. I would prefer using Hibernate to do this without Stateless session bean.

Anyone has an inside on this ? I did not find any related posting on the forum.


The hibernate cfg mapping file is like this:



Code:


<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory name="hibernate.session_factory">

<!-- properties -->
<property name="connection.datasource">ZG-DATA-SOURCE</property>
<property name="show_sql">true</property>
      <property name="use_outer_join">false</property>
      <property
name="transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup</property>
         <property
name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>

<!-- mapping files -->
<mapping
resource="com/clearstorysystems/ems/persist/ActionBean.hbm.xml"/>

</session-factory>



Noticed that i did not specify the Dialect. As Data source already provides such information.

And the session factory is obtained via JNDI lookat on Session Factory name.




My Env.

OS: LINUX RED HAT Fedora Core 1
App Server: Weblogic 8.1 SP3
Hibernate 3.0.5
Ant 1.6.1
XDoclet 1.2.3

I use XDoclet to generate Hibernate Mapping file


Thanks a lot


Chester Chen


Top
 Profile  
 
 Post subject: Create a SubTransaction using Hibernate in EJB Container
PostPosted: Wed Jul 27, 2005 2:18 pm 
Newbie

Joined: Tue Jul 26, 2005 9:12 pm
Posts: 7
Location: California
I forgot to mention that the database is Oracle 9.2.0.5

Thanks

Chester


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 7:10 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Hibernate does not manage Tx, its your JTA environment.
So what you could do is to wrap your call in a SLSB method marked as RequiresNew

_________________
Emmanuel


Top
 Profile  
 
 Post subject: new transaction in CMT env.
PostPosted: Wed Jul 27, 2005 9:39 pm 
Newbie

Joined: Tue Jul 26, 2005 9:12 pm
Posts: 7
Location: California
That's what I am doing now. I was hoping Hibernate will leverage JTA and allows me to start a new transaction like CMP does.

Thanks for the reply.


Chester


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 3:06 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You can always suspend the Tx, start a new one, commit it and then resune the original tx, but once again, it"s plain JTA, and has nothing to do with Hibernate

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 3:12 pm 
Newbie

Joined: Tue Jul 26, 2005 9:12 pm
Posts: 7
Location: California
Thanks for your reply,

Code:
You can always suspend the Tx, start a new one, commit it and then resune the original tx, but once again, it"s plain JTA, and has nothing to do with Hibernate



But I am not sure how could you do it in the CMT env. ? Could you show me an example ? I know this may not be hibenate any more. But this could me to understand it better.


In JTA, you can start UserTransaction with begin, commit or rollback. But in CMT env. such as Weblogic EJB container, you don't have control tx any more, so I am wondering what do you mean by suspend the Tx in this case. Could you elaborate ?


Thanks a lot


Chester


Top
 Profile  
 
 Post subject: Transaction roll back hehaviour
PostPosted: Thu Jul 28, 2005 3:30 pm 
Newbie

Joined: Tue Jul 26, 2005 9:12 pm
Posts: 7
Location: California
Also, a follow-up question, I have notied that following behaviour which is a bit surprise to me.


I have transaction T1, which in turn create transanction T2 (via SLSB).

In T2, I use Hibernate to persist an Object to databse, session.save(entity); session.flush(); then back in T1, I encounter an error, throw an Exception, which cause T1 roll back.

I noticed, that the object persisted in T2 did NOT commited to database.

In the same use case with EJB CMP, the changes in T2 would have commited even T1 has the exception.

I don't know if this is something I did wrong or I would have to do differently.

I have tried to SessionFactory.openSesison(connection) instead of just
SessionFactory.oepnSession(); where Connection is obtained directly from DataSource (DataSource is found via JNDI Lookup). It does not make a difference.

The question is what the behaviour should be ? is there something I need to do differently ?


Thanks a lot

Chester


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 3:40 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Have you considered talking a step back and reading up on some basic "transaction management in EJB" material?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 3:57 pm 
Newbie

Joined: Tue Jul 26, 2005 9:12 pm
Posts: 7
Location: California
Code:
Have you considered talking a step back and reading up on some basic "transaction management in EJB" material?



Thanks for the suggestion. And I did, I have re-read some of the books and materials I have read before.

I believe Transaction behaviour in CMP is correct. That's why I am puzzled by the behavouir when changing to Hibernate.

I hope that just some-thing I did wrong. I was hoping some one give me a pointer as what's the right way to do it for case like this.

Regards,

Chester


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 8:03 pm 
Newbie

Joined: Tue Jul 26, 2005 9:12 pm
Posts: 7
Location: California
Just one more update.

If I change the Hibernate code to plain JDBC SQL code to insert in transaction T2, as expected, the record committed in transaction T2 persist in database, even the exception in transaction T1 caused rollback.

This is the same as EJB CMP. But it's not the same for Hibernate code.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.