Defining the transaction rollback (optional), Programmatic transaction management, Using the transactiontemplate – HP Integrity NonStop J-Series User Manual

Page 70

Advertising
background image

Defining the Transaction Rollback (Optional)

This section describes the steps to control the rollback of transactions in a simple declarative fashion.

The recommended way to indicate to the Spring framework transaction infrastructure that a
transaction work must be rolled back is to throw an Exception from the code that is currently
executing in the context of a transaction. The Spring framework transaction infrastructure code
catches any unhandled Exception as it bubbles up the call stack, and marks the transaction for
rollback.

You can configure the Exception type to mark a transaction for rollback. The following example
is a snippet of XML configuration that demonstrates how to configure rollback for a checked,
application-specific Exception type.

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" rollback-for="NoProductInStockException"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>

It is also possible to specify 'no rollback rules', for those times when you do not want a transaction
to be marked for rollback when an exception is thrown. In the example below, the Spring framework
transaction infrastructure commits the attendant transaction even in the face of an unhandled
InstrumentNotFoundException

.

<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="updateStock" no-rollback-for="InstrumentNotFoundException"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>

When the Spring framework transaction infrastructure has caught an exception and is consulting
any configured rollback rules to determine whether or not to mark the transaction for rollback, the
strongest matching rule wins. Therefore, in the case of the following configuration, any exception
other than an InstrumentNotFoundException would result in the attendant transaction being
marked for rollback.

<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="*" rollback-for="Throwable" no-rollback-for="InstrumentNotFoundException"/>
</tx:attributes>
</tx:advice>

This completes the basic transaction handling in Spring using the declarative approach.

Programmatic Transaction Management

The Spring framework provides two means of programmatic transaction management:

“Using the TransactionTemplate” (page 70)

“Using a PlatformTransactionManager” (page 71)

Using the TransactionTemplate

The TransactionTemplate handles the transaction lifecycle and possible exceptions so that
the calling code does not explicitly handle transactions.

The application code that must execute in a transactional context, and that uses
TransactionTemplate

explicitly, can be achieved by completing the following steps:

1.

Write a TransactionCallback implementation that contains all the codes you need to
execute in the context of a transaction.

2.

Pass an instance of your custom TransactionCallback to the execute(..) method
exposed on the TransactionTemplate.

public class SimpleService implements Service {
// single TransactionTemplate shared amongst all methods in this instance
private final TransactionTemplate transactionTemplate;

70

Configuring Spring Applications on NonStop Systems

Advertising
This manual is related to the following products: