0

We have a service class that goes like this

@Service
@Transactional
public class DoSomeServiceImpl1 implements IDoSomeService {

@Override
public void doSomething() throws SomeException {
    //dao calls (database insert)
    //ldap dao call   (ldap insert)
}

When an exception happens in the ldap dao calls, the data inserted in the database remains there and does not rollback.

The ldap call method is something like the one below

public void insertLDAPRecord() throws SomeException {
      try {
         //ldap insert logic
      } catch (LDAPException e) {
          throw new SomeException("ldapexception",e);
       }
}

Can anyone help me on this? Why wouldn't the database insert rollback? by the way for the dao call we are using spring-tx version 4.2.4.RELEASE and our app is running on Websphere 8.5

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • Post the actual dao impl and service. Configuration of your app and explain which database you are using. – M. Deinum Sep 30 '16 at 06:23

2 Answers2

3

Spring transactions will by default rollback when any RuntimeException occurs. If your SomeException is a checked exception, then you'll have to explicitly tell spring to roll back when it occurs, see @saurav-saha's answer. The usual approach is to not use checked exceptions though, Spring's philosophy is to always use RuntimeExceptions and not make any assumptions about the client of a method.

If on the other hand your SomeException actually is a RuntimeException, then you most likely haven't setup a TransactionManager. To tell if this is the case, set a breakpoint in your method, and look for org.springframework.transaction.interceptor.TransactionInterceptor in the call stack. If it's not there, then there's no transaction active and you'll need to configure a transaction manager.

gogstad
  • 3,607
  • 1
  • 29
  • 32
1

If you want the transaction to rollback the @Transactional annotation should have the rollbackFor property. You can use the @Transactional annotation over method rather than over class.

@Override
@Transactional(rollbackFor = Exception.class, readOnly = false)
public void doSomething() throws SomeException {

    //dao calls (database insert)
    //ldap dao call   (ldap insert)
}