-->
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.  [ 6 posts ] 
Author Message
 Post subject: Transaction Handling
PostPosted: Fri Jun 13, 2008 5:30 am 
Newbie

Joined: Tue Oct 02, 2007 3:49 am
Posts: 17
Hibernate version: 3.2

Name and version of the database you are using:MS SQLSERVER 7.0

Dear All,
I wrote a generic controller to handle the update action for each entity. It's fine. Below is my code.

public <T> T save(Serializable obj) {
ORMHelper.openSession(IAppConstants.DEFAULT_SESSION);
T mergedObj;

try {
ORMHelper.beginTransaction();
mergedObj = (T) ORMHelper.update(obj);
ORMHelper.commitTransaction();
return mergedObj;
} catch (RuntimeException ex) {
try {
ORMHelper.rollbackTransaction();
} catch (RuntimeException rbEx) {
logger.error(ROLLBACK_ERR_MSG, rbEx);
}
throw ex;

}
}


But if now I want to do is on update 2 or more entities in a transaction, it will rollback if one of any entity update fail or it can only commit if all entity successfully update. Is there any way like Spring in Hibernate that using @Transactional to control the commit action? Since I don't want to use the Spring in my project, thus hoping to find any alternative way to handle it.

Best Rdgs
Ellis


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 13, 2008 10:38 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

Does this not all depend on how you call your save method? What I mean is that you current code can only persist one single entity anyway, so I assume that you have somewhere a method which has eg a collection of entities and you want to persist all of them. Even though it might be dangerous to partly commit changes you could just iterate over your entities, call your save method for each entity and catch a potential exception. You could keep count on how many entities failed to update and even keep the raised exceptions. Not sure if this is a good idea though. In most cases it actually makes sense to all or nothing.

Since you don't want to use any additional frameworks (like eg Spring) to handle transactions, you will have to write your own 'framework' depending on your usecase.

In my experience, however, declarative transaction management is superior to programmatic transaction handling.

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 15, 2008 9:40 pm 
Newbie

Joined: Tue Oct 02, 2007 3:49 am
Posts: 17
Thanks for your prompt reply. Actually, I started my project using hibernate + spring + eclipse at the beginning. But I almost used 2 weeks to try putting them together and failed at the end. Thus I gave up using spring now. In my case, would you kindly give me some more hints or some sample codes as reference? Bcos it's too hard for me to build a framework :-(


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 16, 2008 3:30 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

It all really depends on what type of application you are trying to build. Is it a webapp? If so, maybe this will help - http://hibernate.org/441.html. This page shows all the configuration files you need. On top of this you have to of course annotate your entities and write your db access code. Using this setup you can use inject the entity manager via @PersitenceContext and you can mark any method as @Transactional. I used this setup successfully for several web applications.

When working with Spring and transaction it is also very useful to understand what's actually happening. This - http://static.springframework.org/spring/docs/2.0.x/reference/transaction.html - might look quite dry, but will give you a deeper understanding on what transaction management is all about.

Hope it helps :)

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 16, 2008 4:33 am 
Newbie

Joined: Tue Oct 02, 2007 3:49 am
Posts: 17
Thanks Hardy, How about doing in database application? Can I apply it on it also?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 16, 2008 5:13 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

What do you mean with database application?

What I meant with type was - standalone Java application (simple jar file), a web application (war file, JBoss). Depending on the answer the setup changes, mainly how to bootstrap the frameworks. Using Hibernate all of these application will access a database.

--Hardy


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