开发者

example of spring declarative roolback-for?

开发者 https://www.devze.com 2023-01-19 05:53 出处:网络
want declarative transactional management example in spring aop........ Actually Her开发者_运维问答e

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

Actually Her开发者_运维问答e

<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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消