JPA EntityManager close()不会释放与池的连接吗?

时间:2019-06-24 01:32:46

标签: java spring-data-jpa spring-jdbc spring-transactions

我正在使用JPA存储库,并且在我的代码中还使用了程序化事务管理,如下所示

public class PipelineJobProcessor {

     @Autowired
    private EntityManagerFactory emf;

    @Autowired
    private PipelineJobRepository pipelineJobRepository; //JPA repository

    @Transactional
    public void someMethod () {
        ..
        pipelineJobRepository.save(entity);
    }

    .
    .
    //Manual transaction management
    private void savePipelineJob(PipelineJob pipelineJob) {
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            pipelineJobRepository.save(pipelineJob);
            tx.commit();
        } catch (Exception e) {
            tx.rollback();
            throw e;
        } finally {
            em.close();
        }

    }
}

但是在几次API调用之后,似乎连接池没有任何空闲连接,并且出现以下错误:

关于此的任何提示吗?

  

org.springframework.transaction.CannotCreateTransactionException:   无法打开JPA EntityManager进行交易;嵌套异常为   org.hibernate.exception.JDBCConnectionException:无法获取   JDBC连接位于   org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:446)     在   org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:378)     在   org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:474)     在   org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:289)     在   org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)     在   org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)     在   org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor $ CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:138)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)     在   org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)     在   org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)     在   org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)     在com.sun.proxy。$ Proxy126.save(未知来源)处   com.company.dps.dataplatform.pipeline.generator.PipelineJobProcessor.savePipelineJob(PipelineJobProcessor.java:182)

0 个答案:

没有答案