javadatabasespringtransactionstransactionmanager

Spring DataSourceTransactionManager does not commit


I think my transaction manager does not work at all. On insert to the database auto-increment value increases, but no new data added. I define transaction manager, jdbc template and data source programmatically. Transaction manager:

@Bean("txManager")
public DataSourceTransactionManager dataSourceTransactionManager(
        BasicDataSource basicDataSource) {
    return new DataSourceTransactionManager(basicDataSource);;
}

JDBC template:

@Bean
public NamedParameterJdbcTemplate namedParameterJdbcTemplate(
        BasicDataSource basicDataSource) {
    return new NamedParameterJdbcTemplate(basicDataSource);
}

DataSource:

@Bean
public BasicDataSource basicDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    // ...
    dataSource.setDefaultAutoCommit(false);
    // ...
    return dataSource;
}

Service:

@Override
@Transactional("txManager")
public User register(RegistrationForm registrationForm) {
    User userToRegister = UserMapper.registrationFormToUser().apply(registrationForm);
    encryptPassword(userToRegister);
    return userDao.insert(userToRegister);
}

Here is debug log:

[DEBUG] [2018-12-07 10:37:35] [http-nio-8080-exec-10] DataSourceUtils - Fetching JDBC Connection from DataSource
[DEBUG] [2018-12-07 10:37:35] [http-nio-8080-exec-10] DataSourceUtils - Returning JDBC Connection to DataSource
[DEBUG] [2018-12-07 10:37:35] [http-nio-8080-exec-10] JdbcTemplate - Executing SQL update and returning generated keys
[DEBUG] [2018-12-07 10:37:35] [http-nio-8080-exec-10] JdbcTemplate - Executing prepared SQL statement [INSERT INTO "user" (first_name, last_name, email, password, role_id) VALUES (?, ?, ?, ?, ?)]
[DEBUG] [2018-12-07 10:37:35] [http-nio-8080-exec-10] DataSourceUtils - Fetching JDBC Connection from DataSource
[DEBUG] [2018-12-07 10:37:35] [http-nio-8080-exec-10] DataSourceUtils - Returning JDBC Connection to DataSource

What I am doing wrong?


Solution

  • It seems you are missing @EnableTransactionManagement on your Spring configuration class