简要:
有没有办法确保将数据库连接返回到池?
不会-简要:
数据流:
问题是,在长时间运行的任务中一直记录提交后,连接池使用“活动”连接。如果爆发足够大,这可能会占用我所拥有的任何大小的池。
我正在使用具有以下结构的弹簧靴:
当从控制器调用每个第一个服务方法时,它会打开一个活动连接,并且在控制器方法返回之前不会释放它。
那么,有没有办法在每个服务方法之后将连接返回到池?
以下是服务类:
@Service
@Slf4j
class SubmissionService {
@Autowired
CompanyRepository companyRepository;
@Autowired
SubmissionRepository submissionRepository;
@Autowired
FailureRepository failureRepository;
@Autowired
DataSource dataSource
@Transactional(readOnly = true)
public Long getCompany(String apiToken){
if(!apiToken){
return null
}
return companyRepository.findByApiToken(apiToken)?.id
}
@Transactional
public void successSubmission(Long id) {
log.debug("updating submission ${id} to success")
def submissionInstance = submissionRepository.findOne(id)
submissionInstance.message = "successfully analyzed."
submissionInstance.success = true
submissionRepository.save(submissionInstance)
}
@Transactional
public long createSubmission(Map properties) {
log.debug("creating submission ${properties}")
dataSource.pool.logPoolState()
def submissionInstance = new Submission()
for (key in properties.keySet()) {
if(submissionInstance.hasProperty(key)){
submissionInstance."${key}" = properties.get(key)
}
}
submissionInstance.company = companyRepository.findOne(properties.companyId)
submissionRepository.save(submissionInstance)
return submissionInstance.id
}
@Transactional
public Long failureSubmission(Exception e, Object analysis, Long submissionId){
//Track the failures
log.debug("updating submission ${submissionId} to failure")
def submissionInstance
if (submissionId) {
submissionInstance = submissionRepository.findOne(submissionId)
submissionRepository.save(submissionInstance)
}
def failureInstance = new Failure(submission: submissionInstance, submittedJson: JsonOutput.toJson(analysis), errorMessage: e.message)
failureRepository.save(failureInstance)
return failureInstance.id
}
}
答案 0 :(得分:0)
事实证明@ M.Deinum正走在正确的轨道上。 Spring Boot JPA自动打开" OpenEntityManagerInViewFilter"如果应用程序属性spring.jpa.open_in_view
设置为true,则默认情况下为true。我在JPA Configuration Source中找到了这个。
将此设置为false后,数据库会话未被保留,我的问题就消失了。