views:

32

answers:

2

want declarative transactional management example in spring aop........

Actually Here

<aop:config>
    <aop:advisor advice-ref="addAdvice" pointcut="execution(* com.DAO.*.*(..))"/>
</aop:config>
<tx:advice id="addAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="add*" propagation="REQUIRED" rollback-for="" />
    </tx:attributes>
</tx:advice>

So here what i want to write actually rollback-for="" , is there any method or any else? and if method then where will it that method put?

+3  A: 

in rollback-for you specify the name of the exception. For example if you you want to rollback for your.pkg.NoProductInStockException, you write

rollback-for="your.pkg.NoProductInStockException"

This will make the transaction be rolled back if it encounters an exception that matches the specified. If an exception is thrown that does not match, it is propagated to the caller of the service or wrapped into a TransactionRolledBackException

The transaction documentation explains:

The recommended way to indicate to the Spring Framework's transaction infrastructure that a transaction's work is to be rolled back is to throw an Exception from code that is currently executing in the context of a transaction. The Spring Framework's transaction infrastructure code will catch any unhandled Exception as it bubbles up the call stack, and make a determination whether to mark the transaction for rollback.

In its default configuration, the Spring Framework's transaction infrastructure code only marks a transaction for rollback in the case of runtime, unchecked exceptions; that is, when the thrown exception is an instance or subclass of RuntimeException. (Errors will also - by default - result in a rollback). Checked exceptions that are thrown from a transactional method do not result in rollback in the default configuration.

You can configure exactly which Exception types mark a transaction for rollback, including checked exceptions.

Bozho
+1 (11 more to go)
seanizer
@axtavt - I added an excerpt from the docs.
Bozho
Thanks bozho for reply, but what exactly we have to write in our "NoProductInStockException" ? we have to write our rollback method or exception handling? can u give me an example for this?
Sanju
A: 

By default, there should be no need to do this for unchecked exceptions. If you throw an unchecked exception inside the method then you have to use the rollback-for attribute. You can use regular expression style, e.g.: *InStockException

virgium03