Reputation: 2070
I use spring 3.2 and have some transactions. When i get an exception, the rollback don't seem to be done.
My code
public class x{
@Transactional
public createX(){
try{
...
y.createY();
...
}
catch(Exception e){
....
}
}
}
public class y{
@Transactional
public createY(){
...
callYY();
...
}
@Transactional(propagation = Propagation.REQUIRED)
public void callYY(){
...
throw new Exception();
}
}
@Configuration
@EnableTransactionManagement
public class Configuration {
}
Basicaly, i have a class X, createX method start a transaction. It call createY who call callYY. In this method an exception happen. I was thinking then all the persistent operation since the createX would be rollbacked but it's not that who happen
I don't see transaction info in the log
any idea
Upvotes: 0
Views: 155
Reputation: 21
It is very simple. You catch exception in method createX. If you want rollback you can't catch exception in transaction. To rollback transaction you have to throw exception without catch.
Upvotes: 1
Reputation: 441
Propagation.REQUIRED
(which is default) means that no transaction opened in case of an open transaction exists.
That means that the transaction actually being opened upon calling x.createX
method and nothing is done (in terms of transaction treatment) upon calling y.callY
and y.callYY
methods.
However you catch the exception and it doesn't reach the Spring transaction interceptor defined on x.createX
method, which should translate it into the rollback.
So if x.createX
don't have to to be transactional, removing @Transactional
from it will make the rollbacks to happen.
Upvotes: 1
Reputation: 53516
You never mention your tranaction manager. You will need to either define a transaction manager in your application context xml...
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
OR as an annotated declaration on your Bean Configuration class...
@Configuration
@EnableTransactionManagement()
public class MyConfiguration {...}
Upvotes: -1
Reputation: 3522
Try define exception which cause rollback, for example:
@Transactional(rollbackFor = {Throwable.class, Exception.class})
Upvotes: 1