Spring DataSourceTransactionManager不提交

时间:2018-12-07 08:42:24

标签: java database spring transactions transactionmanager

我认为我的交易经理根本不工作。在插入数据库时​​,自动增量值会增加,但不会添加新数据。 我以编程方式定义了事务管理器,jdbc模板和数据源。 交易经理:

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

JDBC模板:

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

数据源:

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

服务:

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

这是调试日志:

[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

我做错了什么?

1 个答案:

答案 0 :(得分:2)

看来您在Spring配置类上缺少@EnableTransactionManagement