[英]@Transactional for any Exceptions in Spring
I am looking using @Transactional
on one of the Service methods. 我正在寻找在Service方法之一上使用
@Transactional
。 However when an exception occurs, the transaction is not getting rolled back. 但是,发生异常时,事务不会回滚。 I tried the same with
@Transactional(rollbackFor=Exception.class)
. 我尝试了
@Transactional(rollbackFor=Exception.class)
。 My code as follows:- 我的代码如下:
@Override
@Transactional(rollbackFor=Throwable.class)
public boolean addUser(User user) throws Exception{
boolean userAdded = userDao.addUser(user);
boolean userRegistrationRecorded = userDao.recordUserRegistraionDetails(user);
return true;
}
I read lot of posts and every one says that Spring handles only RuntimeException
s and not checked Exception
s other than RmiException
. 我看了很多帖子,每一个说,春节仅处理
RuntimeException
S和未检查Exception
其他s比RmiException
。 I need a solution that works for any kind of Exception
. 我需要一种适用于任何
Exception
的解决方案。 Some one suggested me to write own annotation, where as others suggested of having a TransactionManager
as part of applicationContext.xml
file. 有人建议我写自己的注释,而另一些人建议我将
TransactionManager
作为applicationContext.xml
文件的一部分。 A detailed solution will definitely help me. 详细的解决方案一定会对我有帮助。
By the way I am using Spring JdbcTemplate
. 顺便说一下,我正在使用Spring
JdbcTemplate
。 The strange thing I observe is though the Exception
s raised by Spring are RuntimeException
s the transaction is not getting rolled back. 我观察到的奇怪的事情是,尽管Spring引发的
Exception
是RuntimeException
但是事务没有回滚。 I am trying to raise an Exception
by adding the same User
in the above scenario. 我试图通过在上述情况下添加同一
User
来引发Exception
。
My applicationContext.xml is as follows:- 我的applicationContext.xml如下:
<context:component-scan base-package="org.chaperone.services.security.*" />
<context:annotation-config />
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="searchSystemEnvironment" value="true" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${DATABASE_URL}" />
<property name="username" value="${DATABASE_USER_NAME}" />
<property name="password" value="${DATABASE_PASSWORD}" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.