我尝试在代码中为db2和mysql两个数据库的回滚场景使用事务管理如果有人失败,我希望这两个数据库的事务都应该回滚,但在循环中处理了大约70个项目后失败,使用hikaripool-1-连接不可用,请求在30001ms后超时。
主要类别:
public class Test {
@Autowired
@Qualifier("db2transactionmanager")
private DataSourceTransactionManager db2TransactionManager;
@Autowired
@Qualifier("mysqltransactionmanager")
private DataSourceTransactionManager mysqlTransactionManager;
@Autowired
private TransactionDefinition transactionDefinition;
public void mymethod() {
try {
TransactionStatus db2TransactionStatus = null;
TransactionStatus mysqlTransactionStatus = null;
// Some code
for (int i = 0; i <= 100; i++) {
if (//Valid Condition) {
//Getting error on below line
db2TransactionStatus = db2TransactionManager.getTransaction(transactionDefinition);
mysqlTransactionStatus = mysqlTransactionManager.getTransaction(transactionDefinition);
//Some code
db2TransactionStatus.createSavepoint();
// Updating the db2 table
db2TransactionStatus.createSavepoint();
mysqlTransactionStatus.createSavepoint();
// Updating the mysql table
if (//valid condition) {
if (db2TransactionStatus != null) {
db2TransactionManager.getTransaction(null);
db2TransactionManager.commit(db2TransactionStatus);
}
if (mysqlTransactionStatus != null) {
mysqlTransactionManager.getTransaction(null);
mysqlTransactionManager.commit(mysqlTransactionStatus);
}
}
}
}
}
应用程序属性:
//My sql
spring.mysql.datasource.url=${DB_URL}&serverTimezone=EST
spring.mysql.datasource.username=${DB_USER}
spring.mysql.datasource.password=${DB_PASSWORD}
spring.mysql.datasource.hikari.maximum-pool-size=3
spring.mysql.datasource.hikari.minimum-idle=1
spring.mysql.datasource.hikari.idle-timeout=10000
spring.mysql.datasource.hikari.show-sql=true
spring.mysql.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.mysql.datasource.autocommit=false
//DB2
spring.datasource.url=jdbc:db2://${DB2_SERVER}:${DB2_PORT}/${DB2_DSN}:retrieveMessagesFromServerOnGetMessage=true:allowNextOnExhaustedResultSet=1;
spring.datasource.username=${DB2_USER}
spring.datasource.password=${DB2_PASSWORD}
spring.datasource.driver-class-name=com.ibm.db2.jcc.DB2Driver
spring.datasource.autocommit=false
dbconfig类:
@Bean(name = "mysqltransactionmanager")
public DataSourceTransactionManager mysqlTranactionManager(@Qualifier("mysql") DataSource mysql)
{
LOGGER.info("Creating MYSQL JDBC Template with datasouce: {}", mysql);
return new DataSourceTransactionManager(mysql);
}
@Bean(name = "db2transactionmanager")
@Primary
public DataSourceTransactionManager db2TranactionManager(@Qualifier("db2") DataSource db2)
{
LOGGER.info("Creating db2 with datasouce: {}", db2);
return new DataSourceTransactionManager(db2);
}
@Bean(name = "transactionDefinition")
public TransactionDefinition transactionDefinitionNew() {
DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition();
defaultTransactionDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
defaultTransactionDefinition.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
return defaultTransactionDefinition;
}
**Error**:
Could not open JDBC Connection for transaction; nested exception is java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30001ms.
org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30001ms.
at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:305) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.handleExistingTransaction(AbstractPlatformTransactionManager.java:430) ~[spring-tx-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:354) ~[spring-tx-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at com.process(Test.java:151) [classes/:?]
at com.run(MainTest.java:46) [classes/:?]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at com.main(MainTest.java:37) [classes/:?]
Caused by: java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30001ms.
at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:676) ~[HikariCP-3.2.0.jar:?]
at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:190) ~[HikariCP-3.2.0.jar:?]
at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:155) ~[HikariCP-3.2.0.jar:?]
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:128) ~[HikariCP-3.2.0.jar:?]
at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:262) ~[spring-jdbc-5.1.5.RELEASE.jar:5.1.5.RELEASE]
... 10 more
暂无答案!
目前还没有任何答案,快来回答吧!