我有一个要求,我必须在单个交易中执行多项操作,如下所示:
1. Update
2. Insert
3. Delete
我有简单的查询(例如INSERT / UPDATE / DELETE查询)来执行操作,而我使用的是Spring JdbcTemplate.update()
方法。
以下是我正在使用的配置:
Database: MySQL v8
Web development Framework: Spring Boot: 2.1.1.RELEASE
Connection Pool: BoneCP: 0.8.0.RELEASE
Transaction Management: Programmatic Transaction Management
Class: org.springframework.jdbc.datasource.DataSourceTransactionManager with DefaultTransactionDefinition
任何与INSERT结合使用均失败并尝试回滚。组合如下:
1. INSERT + UPDATE
2. UPDATE + INSERT + DELETE
3. INSERT + DELETE
因此,基本上,每当我尝试同时执行'INSERT'操作和'UPDATE'/'DELETE'操作时,都会出现以下错误:
代码段:
@Autowired private JdbcTemplate jdbcTemplate;
@Autowired private DataSourceTransactionManager transactionManager;
TransactionStatus updateTransactionStatus = transactionManager.getTransaction(new DefaultTransactionDefinition());
try {
for(Map.Entry<String, String> entry : dataMap.entrySet()) {
if("INSERT".equalsIgnoreCase(entry.getKey())) {
System.out.println("Insert operation");
jdbcTemplate.update(entry.getValue());
} else if("DELETE".equalsIgnoreCase(entry.getKey())) {
System.out.println("Delete operation");
jdbcTemplate.update(entry.getValue());
} else {
System.out.println("Update operation");
jdbcTemplate.update(entry.getValue());
}
}
transactionManager.commit(updateTransactionStatus);
} catch(DataAccessException dae) {
System.err.println(dae.printStackTrace());
transactionManager.rollback(updateTransactionStatus);
}
错误堆栈跟踪:
2019-03-05 14:28:37.377 [http-nio-8182-exec-1] ERROR com.jolbox.bonecp.ConnectionHandle.markPossiblyBroken - Database access problem. Killing off this connection and all remaining connections in the connection pool. SQL State = HY000
2019-03-05 14:28:37.494 [http-nio-8182-exec-1] WARN o.s.j.support.SQLErrorCodesFactory.getErrorCodes - Error while extracting database name - falling back to empty error codes
org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is java.sql.SQLException: Connection is closed!
at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:331)
at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:356)
at org.springframework.jdbc.support.SQLErrorCodesFactory.getErrorCodes(SQLErrorCodesFactory.java:214)
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.setDataSource(SQLErrorCodeSQLExceptionTranslator.java:136)
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.<init>(SQLErrorCodeSQLExceptionTranslator.java:99)
at org.springframework.jdbc.support.JdbcAccessor.getExceptionTranslator(JdbcAccessor.java:122)
at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1444)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:388)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:523)
...
Caused by: java.sql.SQLException: Connection is closed!
at com.jolbox.bonecp.ConnectionHandle.checkClosed(ConnectionHandle.java:459)
at com.jolbox.bonecp.ConnectionHandle.getMetaData(ConnectionHandle.java:812)
at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:320)
... 126 common frames omitted