-->
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.  [ 8 posts ] 
Author Message
 Post subject: need input on transaction handling
PostPosted: Wed May 09, 2007 11:11 pm 
Newbie

Joined: Sat Nov 25, 2006 6:57 am
Posts: 12
Location: india
Hi,

I am using hibernate with Tomcat5.0. Here i wanted to use JTA so added jotm related jars. Changed the hibernate.cfg.xml for including the transaction.

<property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="jta.UserTransaction">java:comp/UserTransaction</property>

So after adding this in my DAO can i use directly

sessionFactory.getSession().beginTransaction()
// perform some logic
sessionFactory.getSession().getTransaction().commit();
//if exception
rollback
finally
close session.

Will this do?
Or if i want to seperate my transaction handling to seperate file. What is the best way. So my DAO can just take of Data access logic.

Thanks,
Naresh


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 12:24 am 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
Here's one way using AOP

http://www.hibernate.org/391.html

Basically the general way is to use a proxy to intercept calls to your DAO methods. Look at java's Proxy class.

You'd basically need to implement the invoke() method.

So inside the invoke() method you'd have something like

try {
begin transaction

call DAO method

commit transaction

} catch (Exception e) {
rollback
} finally {
close session
}

Application servers such as JBOSS do this automatically for you. If you are outside of an application server, you kinda have to implement the proxy urself.

_________________
Don't forget to rate the reply if it helps..:)

Budyanto


Top
 Profile  
 
 Post subject: don't want to use AOP
PostPosted: Thu May 10, 2007 1:15 am 
Newbie

Joined: Sat Nov 25, 2006 6:57 am
Posts: 12
Location: india
Thanks for your reply.

But here i don't want to use AOP and implement on my own.

Then i need to write an interceptor as u have mentioned. So in this case i need to pass the session instance to the DAO method right.

But will the session.beginTransaction() use JTA with only the declaration in configuration itself? if yes then what is the difference between using this way and using initialContext to load the UserTransaction.

Thanks,
Naresh


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 1:39 am 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
The advantage with using the proxy or interceptor is that your DAO code only deals with, for example, session.save(), session.delete()..etc.

All the transaction handling stuff such as begin, commit, rollback and session close is done in the interceptor. It keeps your DAO clean of transaction management.

I'm not sure about your other JTA question. I would think you'd need to get the transaction using InitialContext for JTA but I may be wrong on this. I have never used JTA.

The example here seems to suggest using of InitialContext().

_________________
Don't forget to rate the reply if it helps..:)

Budyanto


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 2:04 am 
Newbie

Joined: Sat Nov 25, 2006 6:57 am
Posts: 12
Location: india
Yes, i am looking for the same type of implementation.

Meanwhile i checked through this site and found some code examples at

http://salto-db.sourceforge.net/salto-d ... tedao.html

which is exactly what i am looking for. But here, i have a doubt in HibernateUtil there is getSession() method which opens transaction and there are methods for closing session, commit and roll back. But i could not find any call in the code for commit and rollback. Only i could find the call for getSession().

So here in the final DAO, if any exception occurs while in save, or saveorUpdate etc... how will the transaction being taken care?

Once this is clear i think i just need to change in single place for handling JTA.

Thanks,
Naresh


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 2:46 am 
Beginner
Beginner

Joined: Mon Apr 23, 2007 8:30 am
Posts: 27
Location: India
This might help http://www.hibernate.org/42.html.

My two cents here are to implement transactions as aspects to avoid scaterring the begin() - rollback() code all over. Also, I would prefer not to use try-catch in my code; instead use an AOP based exception logger to report exceptions.

Thanks,
Nikhil


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 1:28 pm 
Newbie

Joined: Sat Nov 25, 2006 6:57 am
Posts: 12
Location: india
I had already gone through that page before posting this thread.
So i could see 2 ways of implementation for seperating the DAO and transaction handling layer. One is using AOP and other is interceptor. So here i want to go with interceptor not AOP.

In this case say i have multiple DAO's with save, delete, findAll.... which internally just executes on the session object.

Now how the inceptor should be written to call multiple DAO's depending on for which it is called, from the single interceptor's method.

Will it be something like this shown below. Say i have to invoke method with class name, methodname and object as parameter. Then in the invoke method it will be something as below

//beginTransaction
//getClass and create instance of the DAO class name passed
//setSession on the DAO instance
//perform the method passed with the object passed as parameter
//commit / rollback

There might be changes, but i think it would give u a clear picture of what i am looking for.

Thanks,
Naresh


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 10:38 pm 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
Look at the example section here.

http://java.sun.com/j2se/1.3/docs/guide ... proxy.html

This InvocationHandler described in there would be your interceptor.

Your DAOs would need to have an interface which you can pass to the newProxyInstance() method of the Proxy class. You'd implement an InvocationHandler. It's invoke method will have the transaction handling class. You might want, just like in the example to make your InvocationHandler have a constructor that takes in the DAO. This is how you use the same InvocationHandler with different DAOs. You are passing the DAO into it.

_________________
Don't forget to rate the reply if it helps..:)

Budyanto


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