You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Alistair Young <al...@gmail.com> on 2008/04/16 15:18:50 UTC

Pseudo-nesting of transactions

I have a question about nesting iBATIS transactions.  Or, at least,
being able to write methods which will work in an iBATIS transactional
context whether or not one has been imposed externally.

Consider the following bit of sample code:

   sqlMap.startTransaction();
   sqlMap.update("XXX", xxx);
   doProcessing();
   sqlMap.commitTransaction();
   sqlMap.endTransaction();

And suppose that my doProcessing() method looks like this:

   sqlMap.startTransaction();
   sqlMap.update("YYY", yyy);
   sqlMap.commitTransaction();
   sqlMap.endTransaction();

If I execute this code, I'll get an exception because a nested
transaction is being attempted on the sqlMap.

However, what I really want to happen is for the database operations
in my doProcessing() method to join the existing iBATIS transaction,
if one happens to be open (so it is not true nesting).  Something like
this:

   boolean txnAlreadyOpen = sqlMap.isTransactionOpen();
   if (!txnAlreadyOpen) {
      sqlMap.startTransaction();
   }
   sqlMap.update("YYY", yyy);
   if (!txnAlreadyOpen) {
      sqlMap.commitTransaction();
      sqlMap.endTransaction();
   }

Except, of course, the isTransactionOpen() method doesn't actually
exist.  So I find myself pondering the following code...

   boolean txnAlreadyOpen = false;
   try {
      sqlMap.startTransaction();
   } catch (NestedSQLException x) {
      if (x.getCause() instanceof TransactionException) {
         txnAlreadyOpen = true;
      } else {
         throw x;
      }
   }
   // ... as before

(I appreciate that there are subtleties here when it comes to managing
rollback etc., but one thing at a time...!)

Is there a better way of dealing with this?

Cheers,


Alistair.

Re: Pseudo-nesting of transactions

Posted by Koka Kiknadze <22...@gmail.com>.
Sorry, 'it'll save your headache' should read 'it'll save you from your
headache' ;)

On Thu, Apr 17, 2008 at 1:06 AM, Koka Kiknadze <22...@gmail.com> wrote:
> Is there a better way of dealing with this?

I go for Spring framework's declarative transactions. Spring has support for
iBatis and definitely it'll save your headache.

On Wed, Apr 16, 2008 at 5:18 PM, Alistair Young <al...@gmail.com>
wrote:

> I have a question about nesting iBATIS transactions.  Or, at least,
> being able to write methods which will work in an iBATIS transactional
> context whether or not one has been imposed externally.
>
> Consider the following bit of sample code:
>
>   sqlMap.startTransaction();
>   sqlMap.update("XXX", xxx);
>   doProcessing();
>   sqlMap.commitTransaction();
>   sqlMap.endTransaction();
>
> And suppose that my doProcessing() method looks like this:
>
>   sqlMap.startTransaction();
>   sqlMap.update("YYY", yyy);
>   sqlMap.commitTransaction();
>   sqlMap.endTransaction();
>
> If I execute this code, I'll get an exception because a nested
> transaction is being attempted on the sqlMap.
>
> However, what I really want to happen is for the database operations
> in my doProcessing() method to join the existing iBATIS transaction,
> if one happens to be open (so it is not true nesting).  Something like
> this:
>
>   boolean txnAlreadyOpen = sqlMap.isTransactionOpen();
>   if (!txnAlreadyOpen) {
>      sqlMap.startTransaction();
>   }
>   sqlMap.update("YYY", yyy);
>   if (!txnAlreadyOpen) {
>      sqlMap.commitTransaction();
>      sqlMap.endTransaction();
>   }
>
> Except, of course, the isTransactionOpen() method doesn't actually
> exist.  So I find myself pondering the following code...
>
>   boolean txnAlreadyOpen = false;
>   try {
>      sqlMap.startTransaction();
>   } catch (NestedSQLException x) {
>      if (x.getCause() instanceof TransactionException) {
>         txnAlreadyOpen = true;
>      } else {
>         throw x;
>      }
>   }
>   // ... as before
>
> (I appreciate that there are subtleties here when it comes to managing
> rollback etc., but one thing at a time...!)
>
> Is there a better way of dealing with this?
>
> Cheers,
>
>
> Alistair.
>

Re: Pseudo-nesting of transactions

Posted by Koka Kiknadze <22...@gmail.com>.
> Is there a better way of dealing with this?

I go for Spring framework's declarative transactions. Spring has support for
iBatis and definitely it'll save your headache.