数据库重启导致的连接中断问题

一个对接银行的项目,发现突然没有数据了
1.异常:

org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler.handleUncaughtException(SimpleAsyncUncaughtExceptionHandler.java:37) - Unexpected error occurred invoking async method 'public void org.zhd.bank.adaptor.service.ScheduleService.getHxErpYYTDetails()'.
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: java.sql.SQLException: 关闭的连接

2.代码:

@Async
@Scheduled(cron = "0 0/25 8-18,23 * * ?")
@Transactional(rollbackOn = { RuntimeException.class, Exception.class })
public void getHxErpYYTDetails() {
	try {
		getHxDetails(hxCorpNoYYT);
	} catch (Exception e) {
		bkLog.error(1, hxCorpNoYYT + ">>>>华夏账号异常:" + e.getMessage(), e);
		xyMsgService.batchMsg("erp", 0, hxCorpNoYYT, 1);  //异常时发送短信通知
	}
}

application.properties

# datasource xy
spring.xy.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.xy.datasource.url=jdbc:oracle:thin:@***:1521/ora11
spring.xy.datasource.username=username
spring.xy.datasource.password=password

项目是多数据源的(A,B),其中A数据库在前一天重启过,然后项目并不能自动重连,导致每次走定时任务时,检查连接发现是关闭的连接。

3.解决:
在配置文件中增加连接池以及重连配置

# 增加连接池配置,实现断线自动重连
spring.xy.datasource.initial-size=6
spring.xy.datasource.min-idle=5
spring.xy.datasource.max-idle=10
spring.xy.datasource.max-active=15
spring.xy.datasource.log-abandoned=true
spring.xy.datasource.remove-abandoned=true
spring.xy.datasource.remove-abandoned-timeout=180
spring.xy.datasource.max-wait=5000
spring.xy.datasource.test-while-idle=true
spring.xy.datasource.validation-query=select 1 from dual
spring.xy.datasource.test-on-borrow=true
spring.xy.datasource.min-evictable-idle-time-millis=600000
spring.xy.datasource.time-between-eviction-runs-millis=300000

具体配置意思请看(转载)

4.遗留问题
应该是@Transactional注解的原因,导致还没走进getHxDetails方法就检查到连接关闭了。这样getHxErpYYTDetails中的try…catch就不能捕捉到这个异常,导致无法发送短信通知。注解去掉是可以的,但是就没有事务管理了。

另外这边的是javax.transaction中的注解
------------------------------------------------------------5.6 ------------------------------------------------------------
发现因为是内部调用,@Transactional一直都没有生效,所以还是去掉吧。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值