Spring DataSourceTransactionManager 不提交
Spring DataSourceTransactionManager does not commit
我认为我的事务管理器根本不起作用。在插入数据库时自动增量值增加,但没有添加新数据。
我以编程方式定义事务管理器、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
我做错了什么?
您的 Spring 配置似乎缺少 @EnableTransactionManagement class
我认为我的事务管理器根本不起作用。在插入数据库时自动增量值增加,但没有添加新数据。 我以编程方式定义事务管理器、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
我做错了什么?
您的 Spring 配置似乎缺少 @EnableTransactionManagement class