Java – Spring 3 problems with TransactionManager and annotations

javaspringspring-transactionstransactions

I'm trying to setup a TransactionManager in my Web Application (powered by Spring MVC 3), as I need to have one method of a component annotated as @Transactional.

This is my situation:

  • web.xml: load 2 xml files for the Spring ContextLoaderListener (applicationContext.xml and database.xml)
  • applicationContext.xml: contains some beans which I can't define via annotation, plus the tags for annotation, plus the usual context:annotation-config and context:component-scan (this component-scan includes the package that contains the @Transactional method)
  • database.xml: contains the datasource (I'm using BasicDataSource from commons-dbcp), the transaction manager definition and tx:annotation-driven.

I've got a @Component (DeleteComponent) which has an interface and an implementation (DeleteComponentImpl). The implementation class is annotated with @Component and has one public method annotated with @Transactional (I annotated the concrete class and not the interface, as Spring documentation states). For @Transactional I didn't put any arguments, as defaults are fine. This class has some DAOs (annotated with @Repository) being injected via @Autowired. I'm using only plain JDBC (no Hibernate or other ORM). This @Component is injected into a @Controller (which is defined in spring-servlet.xml).

If the method annotated as @Transactional throws an exception (unchecked, as RuntimeException), however, nothing is rolled back. The database retains the changes did before the exception. I'm using Jetty web server for testing locally my application. The thing I noticed is actually that seems like no transaction manager is being set up. Actually, my transaction manager is named transactionManager. The xml line to set up the annotation-driven transaction is

<tx:annotation-driven transaction-manager="transactionManager"/>

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

If I change it to use a non-existing bean name like

<tx:annotation-driven transaction-manager="fake"/>

The application still deploys correctly and doesn't complain.

Any tips on what should I check to make it working?

Thanks.

Best Answer

I solved by using @Transactional(rollbackFor = RuntimeException.class) and switching from a BasicDataSource to a ComboPooled that's present in c3p0 library. Thanks for the suggestions though.