Search This Blog

Showing posts with label jdbc. Show all posts
Showing posts with label jdbc. Show all posts

Saturday, May 19, 2012

Enable Spring Jdbc Transaction with Annotation

Spring 3.1 introduced a new annotation @EnableTransactionManagement.  With it, all xml configuration can be got rid of now.  Here is the example on how to use it:

@Configuration
@EnableTransactionManagement
public class JdbcConfig {

    @Bean
    public DataSource dataSource() {
    //config datasource      
    }

    @Bean
    public PlatformTransactionManager txManager() {
        return new DataSourceTransactionManager(dataSource());
    }

    @Bean
    public MyDao myDao() {
        MyDaoJdbc dao = new MyDaoJdbc();
        dao.setDataSource(dataSource());
        return dao;
    }
}
Next you need to annote your dao with @Transactional.  The source code can be found at github:

https://github.com/zhentao/spring-jdbc-transaction-example