Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Transactions not rolling back on Exception (Oracle JNDI datasource)

I am using annotation based transactions in a Spring MVC 3.1 project, and my transactions are not being rolled back when an exception is thrown.

Here is my Service code


@Service
public class ImportService {

    @Autowired
    ImportMapper importMapper;

    @Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED, rollbackFor=Throwable.class)
    public void processImport() throws ServiceException, DatabaseException {
        iImport import = new Import();

        createImport(import);

        throw new ServiceException("");         
    }

    @Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED, rollbackFor=Throwable.class)
    private void createImport(Import import) throws DatabaseException {
        try {
            importMapper.createImport(eventImport);
        } catch (Exception e) {
            throw new DatabaseException(e);
        }
    }

So, hopefully, the createImport method should be rolled back after the exception is thrown. But unfortunately it's not.

I am defining my datasource in the server context.xml

<Resource name="datasource.import" auth="Container" type="javax.sql.DataSource"
           maxActive="100" maxIdle="30" maxWait="10000"
           username="user" password="password" driverClassName="oracle.jdbc.driver.OracleDriver"
           url="jdbc:oracle:thin:@INFO" />

And I'm looking it up with JNDI:

<jee:jndi-lookup id="dataSource" jndi-name="datasource.import"/>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

<tx:annotation-driven />

I'm using an Oracle database, and the JDBC spec says that auto commit is true by default. I thought that if I set it to false explicitly that would help, but I can't figure out how to do that.

Is there any way to get rollbacks working, while looking up your Oracle datasource by JNDI.

like image 248
topher-j Avatar asked Jun 13 '13 22:06

topher-j


People also ask

What types of exception does Spring automatically rollback a transaction?

Note however that the Spring Framework's transaction infrastructure code will, by default, only mark 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.)

Does @transactional throw exception?

@Transactional only rolls back transactions for unchecked exceptions. For checked exceptions and their subclasses, it commits data. So although an exception is raised here, because it's a checked exception, Spring ignores it and commits the data to the database, making the system inconsistent.

Does @transactional rollback?

The @Transactional annotation makes use of the attributes rollbackFor or rollbackForClassName to rollback the transactions, and the attributes noRollbackFor or noRollbackForClassName to avoid rollback on listed exceptions. The default rollback behavior in the declarative approach will rollback on runtime exceptions.

What does @transactional does in Spring?

The @Transactional annotation is metadata that specifies that an interface, class, or method must have transactional semantics; for example, "start a brand new read-only transaction when this method is invoked, suspending any existing transaction".


1 Answers

Please be aware that Spring's transaction management rolls-back transactions only for unchecked exceptions (RuntimeException) by default. If you wish to perform rollback also on checked exceptions, you need to define that.

When using annotations as attribute source, you need to provide rollbackFor attribute with the list of exception classes, which should cause a transaction to be rolled-back (quote from the JavaDoc):

/**
 * Defines zero (0) or more exception {@link Class classes}, which must be a
 * subclass of {@link Throwable}, indicating which exception types must cause
 * a transaction rollback.
 * <p>This is the preferred way to construct a rollback rule, matching the
 * exception class and subclasses.
 * <p>Similar to {@link org.springframework.transaction.interceptor.RollbackRuleAttribute#RollbackRuleAttribute(Class clazz)}
 */
Class<? extends Throwable>[] rollbackFor() default {};
like image 169
Pavel Horal Avatar answered Sep 20 '22 14:09

Pavel Horal