-->
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: Nested transactions / suspending transactions.
PostPosted: Sun Jan 11, 2009 4:09 pm 
Beginner
Beginner

Joined: Sun Jan 11, 2009 2:47 pm
Posts: 21
Hibernate version: 3.3.0 CR1

Name and version of the database you are using: MS SQL Server 2005

I have a web application that consists of various servlets and JSP pages, with Hibernate at the back-end. I am using the "Open Session In View" model, implemented as a servlet filter that opens the transaction and commits/rolls back at the end.

This works great for the servlets and most of the JSP pages. However, I now have a JSP page that needs to open and commit/rollback a separate transaction on the same database without affecting the session opened by the servlet filter (the reason I need to do this relates to software design that I can not change, the details are too gory to get into here). This separate transaction only reads data, it does not write data, if that simplifies things.

I remember reading something about suspending transactions a while ago when I didn't need to do it, and now I can't remember what I read or where I read it. I seem to remember something about being able to suspend the current transaction, start and complete a new one, then resume the old? What's a good way to make this work? Currently, the servlet filter does something like this around the whole request (I've simplified a lot of the code here, left out logging, exception handling on rollback, and I also have the ability to roll back even if an exception is not thrown):

Code:
  Session s = mySessionFactory.getCurrentSession();
  try {
    s.beginTransaction();
    handleRequest(...);
    s.commitTransaction();
  } catch (Throwable t) {
    s.rollbackTransaction();
  }


And the "inner" transaction is opened / closed in a similar way. So, the question is: What's the best way to put that "inner" transaction inside the servlet filter transaction and still have everything work OK?

Thanks,
JC


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2009 5:33 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
Use explicit transaction object for every DB operation and commit() urself in finally block and rollback() in catch block.

*) Don't use implicit transaction. i.e., if u r not specify any Transaction it's taken as implicit transaction by default in DB layer.

_________________
If u feel it will help you, don't forget to rate me....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2009 6:34 am 
Beginner
Beginner

Joined: Sun Jan 11, 2009 2:47 pm
Posts: 21
Madan_Prabhu wrote:
Use explicit transaction object for every DB operation and commit() urself in finally block and rollback() in catch block.

*) Don't use implicit transaction. i.e., if u r not specify any Transaction it's taken as implicit transaction by default in DB layer.


Thanks. The problem is, I don't really have access to the Transaction at the JSP level, because it's all abstracted away behind the DB layer. I don't really have a good way of obtaining the open Transaction and passing it to other pieces of code.

Is there a way to detect if a Transaction already exists? E.g., can I do something like this:
Code:
  if (TransactionAlreadyOpen())
    PerformOperation(ExistingTransaction);
  else {
    try {
      PerformOperation(OpenNewTransaction());
      CommitNewTransaction();
    } catch (Throwable t) {
      RollbackNewTransaction();
      throw;
    }
  }

The idea is to use the existing Transaction if it's open, otherwise open a new one. Is this possible? I have a Session and a SessionFactory to work with at that level.

Thanks!
J


Last edited by mnbv0987 on Mon Jan 12, 2009 6:36 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2009 6:34 am 
Beginner
Beginner

Joined: Sun Jan 11, 2009 2:47 pm
Posts: 21
Oops, double post, sorry.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2009 6:41 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
Transaction trx=session.getTransaction();
if(trx==null)
trx=session.beginTransaction();

use this template for obtaining existing transaction object, if it returns null (i.e., no previous transaction) create ur new transaction.

_________________
If u feel it will help you, don't forget to rate me....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2009 7:26 am 
Beginner
Beginner

Joined: Sun Jan 11, 2009 2:47 pm
Posts: 21
Madan_Prabhu wrote:
use this template for obtaining existing transaction object, if it returns null (i.e., no previous transaction) create ur new transaction.


Cool, thanks a lot for your help!

J


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.