Boost logo

Boost :

Subject: [boost] [transact] Handling with user exceptions
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2010-02-28 13:22:24


Hi,

I would like to initiate a discussion on how to manage with user exceptions. Here follows some thougth I'm implementing in Boost.STM.

There are two main ways to manage with user exceptions:
* exceptions commit transactions - corresponds to the current C++ behavior
* exceptions abort on exit - corresponds to rollback

The behavior associated to the curent language-like macros is to throw the catched exception. In this case, the exception shouldn't contain
references to transaction specific objects.

I was wondering if we cannot add a second family of macros that before a user exception leaves the transaction block the transaction is commited.
BOOST_TRANSACT_A_ family will abort the transaction on user exception
BOOST_TRANSACT_C_ family will commit the transaction on user exception

This will allows to explore which both approaches, and see which approach is more usable, or if there is an approach that is better adapted to some particuler contexts.
 
* Exceptions Commit Transactions - How to get this?

While trying to commit on destruction we have catched any exception to release the commit_on_destruction variable.
commit_on_destruction __comm( __txn ) ;

t ry {
  i f ( false ) ; else / / to for ce a sentence
  / / t r a n s a c t i o n sentence
} catch ( . . . ) { __comm. release ( ) ; throw ; }

To make exception commit transactions by default, we should not release the committer. However, we need to avoid exception throw by commit when the transaction has been set as forced to abort or during unwinding exception.

commit_on_destruction __comm( __txn ) ;
t ry {
  <sentence >
} catch ( . . . ) {
  i f ( __txn_.forced_to_abort ( ) ) __comm.release ( ) ;
  else __comm.commit ( ) ;
  throw ;
}

We need how to override the default behavior.
If we have Exceptions Commit Transactions - we can abort the transaction in two ways:
* Explicit try-block to Abort Transactions
* Explicitly abort before throw

- Explicit try-block to Abort Transactions

The user may want the exception abort the transaction while leaving the transaction block. The user must explicitly state that this is the desired behavior by adding a try-catch block as shown below:

BOOST_STM_C_TRANSACTION
t ry {
  / / t r a n s a c t i o n block
} catch (Ex&) {
  BOOST_STM_ABORT( ) ;
  throw ;
}
BOOST_STM_C_END_TRANSACTION;

- Explicitly abort before throw

The user may want the transaction to be aborted before throwing an exception.
The user may explicitly state that this is the desired either by setting the current transaction as forced to abort,

BOOST_STM_ABORT( ) ;
throw except ( ) ;

or by throwing the exception using the macro :

/ / . . .
BOOST_STM_ABORT_AND_THROW( except ( ) ) ;
/ / . . .

If we have Exceptions Abort on Exit we can commit the transaction as follows:

- Explicit try-block to Commit Transactions
The user must explicitly state that this is the desired behavior by adding a try-catch block as shown below:

BOOST_STM_TRANSACTION
t ry {
  / / t r a n s a c t i o n block
} catch (Ex &ex ) {
  BOOST_STM_COMMIT( ) ;
  throw ;
}
BOOST_STM_END_TRANSACTION;

boost::exception_ptr error;
BOOST_STM_TRANSACTION
t ry {
  / / t r a n s a c t i o n block
} catch (Ex &) {
  error=boost::current_exception();
}
BOOST_STM_END_TRANSACTION;
if( error)
        boost::rethrow_exception(error);
}

Please let me know what do you think? Do you find this useful? Should we provide two families of macros?

Best regards,
_____________________
Vicente Juan Botet Escribá


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk