Reputation: 563
I am looking using @Transactional
on one of the Service methods. However when an exception occurs, the transaction is not getting rolled back. I tried the same with @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
. I need a solution that works for any kind of Exception
. Some one suggested me to write own annotation, where as others suggested of having a TransactionManager
as part of applicationContext.xml
file. A detailed solution will definitely help me.
By the way I am using 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. I am trying to raise an Exception
by adding the same User
in the above scenario.
My applicationContext.xml is as follows:-
<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>
Upvotes: 0
Views: 169
Reputation: 4158
The ease-of-use afforded by the use of the @Transactional
annotation is best illustrated in this link
you have to add :
<tx:annotation-driven transaction-manager="transactionManager" />
Upvotes: 1