-->
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.  [ 4 posts ] 
Author Message
 Post subject: External Transaction Management in JTA/CMT env
PostPosted: Fri Apr 15, 2005 10:15 am 
Newbie

Joined: Mon Oct 11, 2004 6:44 pm
Posts: 5
Hibernate version: 3.0
Database version: MSSQL2K

Background:
We have a DAO layer that does not expose a transaction API to the client. It must support both a hand-rolled DAO impl (no transaction abstraction on top) as well as Hibernate. Yes we could add the abstraction and expose the transactions but we do not wan the clients exposed to transaction management currently. We are relying on the underlying CMT JTA transaction in the app server for transaction demarcation. Yes -the DAOs will only work in a managed CMT environment. :(

Sample Usage:
Code:
<<session bean CMT>>
public void createUser(User newUser) {
   UserDAO dao = null;
   try {
     dao = (UserDAO) DAOFactory.getDAO(UserDAO.class);
     dao.open();
     dao.create(newUser);
   } catch (DAOException ex) {
      log.error(ex);
      ..
      ..
   } finally {
      if (dao != null) dao.close();
   }
}



Question:
In the above code you wil notice there is no calls to begin/commit the transaction. The container is doing that for us. Will hibernate work properly if the JTA transactions are started/stopped externally (no call made to session.beginTransaction())? The only worry I have is that hibernate may not have a way to "attach" to the current transaction since its started "behind its back". This could effect the flushing of the session on the session.close - how will it know to flush itself? I know that as long as the mods are flushed from the session JTA will handle the transactional aspects correctly.
If the above is true then one workaround I have is when my DAO is closed (dao.close()) I could explicitly flush the session at that point. What are the downsides to this if the client only calls close at the end of a transaction anyways?

Thanks,
Chris


Top
 Profile  
 
 Post subject: Make HIbernate use Datasource
PostPosted: Fri Apr 15, 2005 11:41 am 
Beginner
Beginner

Joined: Wed Sep 17, 2003 10:25 am
Posts: 36
You need to setup Hibernate to use a datasource setup in your app server. The datasource provides connections that are managed by JTA.

Hope that helps.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 15, 2005 11:52 am 
Newbie

Joined: Mon Oct 11, 2004 6:44 pm
Posts: 5
Thanks for the reply but that is not my question. It is already setup and running using the JTA of the appserver.

I am asking whether or not hibernate can handle the "behind the back" transaction demarcation w/o explicitly calling Session.beginTransaction() and Transaction.commit/rollback(). IOW, the container is starting/stopping the transactions and hibernate has not been "told" about it. How will it know when to flush?

Anyways my solution is to have my DAOs tell hibernate about the transaction by calling Session.beginTransaction() in my DAO.open() and calling Transaction.commit() in my DAO.close().


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 15, 2005 12:58 pm 
Beginner
Beginner

Joined: Wed Sep 17, 2003 10:25 am
Posts: 36
Okay. I guess I missed a couple more configurations to clarify the response. And the answer is yes. If I recall correctly, you can still flush as the transaction (started by the container) is runing.

By configuring Hibernate to use a datasource and setting your transaction factory class to the following:

hibernate.transaction.factory_class net.sf.hibernate.transaction.JTATransactionFactory

and telling hibernate where to get the UserTransaction from with something like the following:

jta.UserTransaction jta/usertransaction

Hibernate will work nicely with JTA.

What I really like about this externalization is that I can switch to JDBC Transaciton factory (the default) and test my DAO outside the container.

Hope this helps.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.