Spring数据JPA闭合连接
有一个函数,它在数据库中插入一条开始状态为“Running”的记录,然后执行一些长时间的处理,最后将状态更新为“Success”或“failed” 我在最后更新状态时出错,因为处理花费了很长时间(将数据上传到第三方应用程序需要4小时) 我如何处理这种情况 下面是我的代码片段:Spring数据JPA闭合连接,spring,hibernate,spring-boot,spring-data-jpa,spring-transactions,Spring,Hibernate,Spring Boot,Spring Data Jpa,Spring Transactions,有一个函数,它在数据库中插入一条开始状态为“Running”的记录,然后执行一些长时间的处理,最后将状态更新为“Success”或“failed” 我在最后更新状态时出错,因为处理花费了很长时间(将数据上传到第三方应用程序需要4小时) 我如何处理这种情况 下面是我的代码片段: public void upload() { entity.setStatus("RUNNING"); repository.save(entity);
public void upload() {
entity.setStatus("RUNNING");
repository.save(entity);
try {
//Uploads data to thrid party;
callingThridPartyApp();
log.info("Upload successfull.");
entity.setStatus("SUCCESS");
repository.save(entity);
} catch (Exception e) {
log.error("Upload failed.", e);
entity.setStatus("FAILED");
repository.save(entity);
}
}
我解决了这个问题,在更新数据库状态的漫长过程结束时声明了一个新事务
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void updateStatus(Entity entity) {
log.info("Upload successfull.");
entity.setStatus("SUCCESS");
repository.save(entity);
} catch (Exception e) {
log.error("Upload failed.", e);
entity.setStatus("FAILED");
repository.save(entity);
}
}
我解决了这个问题,在更新数据库状态的漫长过程结束时声明了一个新事务
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void updateStatus(Entity entity) {
log.info("Upload successfull.");
entity.setStatus("SUCCESS");
repository.save(entity);
} catch (Exception e) {
log.error("Upload failed.", e);
entity.setStatus("FAILED");
repository.save(entity);
}
}
您正在使用连接池吗?请将
空闲时间
设置为更高的值或无限制
来更改配置文件。有没有办法刷新连接并再次执行事务?因为我无法设置此应用程序的空闲时间。请尝试在方法的顶部使用@Transactional注释。@Thirumaran会重置连接吗?我正在使用Hikari数据库池?您正在使用连接池吗?将IDLE\u TIME
设置为更高的值或UNLIMITED
更改配置文件。有没有办法刷新连接并再次执行事务?因为我无法设置此应用程序的空闲时间。请尝试在方法的顶部使用@Transactional注释。@Thirumaran会重置连接吗?我正在使用Hikari数据库池?