一个对接银行的项目,发现突然没有数据了
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一直都没有生效,所以还是去掉吧。