Pratik Singhal
Pratik Singhal

Reputation: 6492

Jooq not working with spring transactions

I tried setting up using Jooq with Spring JDBC, everything is working properly except transactions.

This is my current setup:

@Configuration
public class DALConfig {

    @Value("${jdbcUrl}")
    String jdbcUrl;

    @Value("${username}")
    String username;

    @Value("${password}")
    String password;

    @Bean(destroyMethod = "close")
    DataSource getDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setUrl(jdbcUrl);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        return dataSource;
    }

    @Bean(name="transactionManager")
    DataSourceTransactionManager getDataSourceTransactionManager() {
        return new DataSourceTransactionManager(getDataSource());
    }

    @Bean(name="transactionAwareDataSource")
    TransactionAwareDataSourceProxy getTransactionAwareDataSourceProxy() {
        return new TransactionAwareDataSourceProxy(getDataSource());
    }

    @Bean(name="connectionProvider")
    DataSourceConnectionProvider getDataSourceConnectionProvider() {
        return new DataSourceConnectionProvider(getTransactionAwareDataSourceProxy());
    }

    @Bean
    DefaultDSLContext getDefaultDSLContext() {
        return new DefaultDSLContext(getConfiguration());
    }

    @Bean
    DefaultConfiguration getConfiguration() {
        DefaultConfiguration config = new DefaultConfiguration();
        config.set(SQLDialect.MYSQL);
        config.setConnectionProvider(getDataSourceConnectionProvider());
        return config;
    }

    @Bean
    CourseDao getCourseDao() {
        return new CourseDao(getConfiguration());
    }
}

I am using @Transactional(propagation = Propagation.MANDATORY) annotation on the method which inserts a new Course, but I am getting the following exception org.springframework.transaction.IllegalTransactionStateException: No existing transaction found for transaction marked with propagation 'mandatory'. I have read the docs for spring and jooq but I have not been able to figure out what is missing and what to do to resolve this. Can someone point out what am I missing here.

Upvotes: 0

Views: 1585

Answers (1)

Pratik Singhal
Pratik Singhal

Reputation: 6492

OK, I got the problem the exception which was supposed to rollback the transaction was happening outside the scope of the transaction. If I added @Transactional in the scope which includes the exception, the rollback works properly.
Also the propagation should be Propagation.MANDATORY should be changed to Propagation.REQUIRED (which is the default).

Upvotes: 1

Related Questions