-->
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: How to commit/rollback transaction that spans multiple db ?
PostPosted: Thu Apr 24, 2008 3:36 am 
Beginner
Beginner

Joined: Wed Aug 25, 2004 10:27 pm
Posts: 21
Location: Indonesia
Hibernate version: 3.0
Name and version of the database you are using: SQL Server 2000


Code between sessionFactory.openSession() and session.close():
Code:
//reportSessionFactory points to Report database.
//transactionSessionFactory points to Business database.

reportSession = reportSessionFactory.openSession();
transactionSession = transactionSessionFactory.openSession();

reportSession.beginTransaction();
transactionSession.beginTransaction();

try
{
reportSession.saveOrUpdate(aReportEntity);
transactionSession.update(aTransactionEntity);

reportSession.commit();
transactionSession.commit();
}
catch (Exception)
{
reportSession.rollback();
transactionSession.rollback();
}
finally
{
if (reportSession != null) reportSession.close();
if (transactionSession != null) transactionSession.close();
}



Hi all,

One of our case is to save/update records in tables that reside in more than 1 database, but it also has to be done in transactional manner.

From our snippet above reportSession & transactionSession point to different database.

Although still don't guarantee atomicity, above codes are so far the best we can come up with.

Also, in production we don't run our code in any J2EE environment but in a standalone J2SE.

Any help would be greatly appreciated.

Best Regards,

Setya


Top
 Profile  
 
 Post subject: looks fine
PostPosted: Thu Apr 24, 2008 4:05 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
Hi,
Due to the fact that you don't have the option of using a JEE container to handle transactions for you, it looks like your code is the best approach for simple cases. However, if your REAL code is much more complex, i.e. it spans multiple databases - not just 2, and there are concurrency issues, etc, you should probably look up some 3rd party transaction managers instead of maintaining your own.

Look into JTA
http://java.sun.com/javaee/technologies/jta/index.jsp
Some JTA impls can run without a full-fledged JEE container. for example, I think you can use a few JBoss jars or even Spring in conjunction with your J2SE code to get some application services like JTA.


Top
 Profile  
 
 Post subject: How to commit/rollback transaction that spans multiple db ?
PostPosted: Thu Apr 24, 2008 5:22 am 
Beginner
Beginner

Joined: Wed Aug 25, 2004 10:27 pm
Posts: 21
Location: Indonesia
Hi,

Thanks for your reply.

I've found JOTM (http://jotm.objectweb.org), but their examples doesn't combine their usage with Hibernate, so this is where we're lost.

Besides, I think I can safely leave the possibility of concurrency issue behind since this is only a small utility application that's not meant for our average users.

Best Regards,

Setya


Top
 Profile  
 
 Post subject: final note
PostPosted: Thu Apr 24, 2008 7:33 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
Then you said it: it's just a small utility, not expected to inflate, I assume; use the code you posted, nothing more.

good luck :-)


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.