Jpa 没有@Transactional的StoredProcedureQuery的连接未释放

Jpa 没有@Transactional的StoredProcedureQuery的连接未释放,jpa,spring-data,eclipselink,Jpa,Spring Data,Eclipselink,我有以下服务,它只是将错误消息记录到数据库中。当方法上没有@Transactional时,连接将保持活动状态,直到所有连接都用完。所有返回结果的StoredProcedureQuery调用都没有此问题 为什么我需要将该方法标记为@Transactional以使其释放连接 @Service public class SSOErrorLogServiceImpl implements SSOErrorLogService { @PersistenceContext private

我有以下服务,它只是将错误消息记录到数据库中。当方法上没有
@Transactional
时,连接将保持活动状态,直到所有连接都用完。所有返回结果的
StoredProcedureQuery
调用都没有此问题

为什么我需要将该方法标记为
@Transactional
以使其释放连接

@Service
public class SSOErrorLogServiceImpl implements SSOErrorLogService {

    @PersistenceContext
    private EntityManager em;

    @Override
    @Transactional
    public void logError(String errorMessage, String request){
        StoredProcedureQuery proc =   em.createNamedStoredProcedureQuery("dbo.spSSOLogError");
        proc.setParameter("errorMessage", errorMessage);
        proc.setParameter("request", request);
        proc.execute();
    }
}
数据配置

public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean entityManagerFactory
            = new LocalContainerEntityManagerFactoryBean();

    entityManagerFactory.setDataSource(reflexDataSource());
    entityManagerFactory.setPackagesToScan("com.company.platform.jpa");
    entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter());

    Properties additionalProperties = new Properties();
    additionalProperties.put("eclipselink.weaving", "false");

    entityManagerFactory.setJpaProperties(additionalProperties);

    return entityManagerFactory;
}

@Bean
public HikariDataSource reflexDataSource() {
    HikariDataSource dataSource = new HikariDataSource();

    dataSource.setLeakDetectionThreshold(20000);
    dataSource.setMaximumPoolSize(20);
    dataSource.setConnectionTimeout(5000);
    dataSource.setRegisterMbeans(false);
    dataSource.setInitializationFailFast(false);
    dataSource.setDriverClassName(driver);
    dataSource.setJdbcUrl(url);
    dataSource.setUsername(user);
    dataSource.setPassword(password);
    return dataSource;
}
没有事务的日志(摘录)

使用@Transaction记录日志

[EL Finer]: transaction: 2016-09-30 14:30:50.747--UnitOfWork(1448667590)--Thread(Thread[Test worker,5,main])--begin unit of work flush
[EL Finer]: transaction: 2016-09-30 14:30:50.747--UnitOfWork(1448667590)--Thread(Thread[Test worker,5,main])--end unit of work flush
[EL Finest]: query: 2016-09-30 14:30:50.747--UnitOfWork(1448667590)--Thread(Thread[Test worker,5,main])--Execute query ResultSetMappingQuery(name="dbo.spSSOLogError" )
[EL Finest]: connection: 2016-09-30 14:30:50.748--ServerSession(1432568628)--Connection(708660831)--Thread(Thread[Test worker,5,main])--Connection acquired from connection pool [default].
[EL Finer]: transaction: 2016-09-30 14:30:50.748--ClientSession(287210054)--Connection(708660831)--Thread(Thread[Test worker,5,main])--begin transaction
[EL Finest]: connection: 2016-09-30 14:30:50.748--ClientSession(287210054)--Thread(Thread[Test worker,5,main])--reconnecting to external connection pool
[EL Fine]: sql: 2016-09-30 14:30:50.75--ClientSession(287210054)--Connection(29007067)--Thread(Thread[Test worker,5,main])--EXECUTE dbo.spSSOLogError @errorMessage = ?, @request = ?
    bind => [Message, Request]
2016-09-30 14:30:50,772 | TRACE | org.springframework.transaction.interceptor.TransactionAspectSupport:519 - Completing transaction for [com.somecompany.platform.jpa.ssoerrorlog.SSOErrorLogServiceImpl.logError]
2016-09-30 14:30:50,772 | TRACE | org.springframework.transaction.support.AbstractPlatformTransactionManager:926 - Triggering beforeCommit synchronization
2016-09-30 14:30:50,772 | TRACE | org.springframework.transaction.support.AbstractPlatformTransactionManager:939 - Triggering beforeCompletion synchronization
2016-09-30 14:30:50,772 | DEBUG | org.springframework.transaction.support.AbstractPlatformTransactionManager:755 - Initiating transaction commit
2016-09-30 14:30:50,773 | DEBUG | org.springframework.orm.jpa.JpaTransactionManager:512 - Committing JPA transaction on EntityManager [org.eclipse.persistence.internal.jpa.EntityManagerImpl@8f90fd5]
[EL Finer]: transaction: 2016-09-30 14:30:50.773--UnitOfWork(1448667590)--Thread(Thread[Test worker,5,main])--begin unit of work commit
[EL Finer]: transaction: 2016-09-30 14:30:50.773--ClientSession(287210054)--Connection(29007067)--Thread(Thread[Test worker,5,main])--commit transaction
[EL Finest]: connection: 2016-09-30 14:30:50.775--ServerSession(1432568628)--Connection(708660831)--Thread(Thread[Test worker,5,main])--Connection released to connection pool [default].
[EL Finer]: transaction: 2016-09-30 14:30:50.775--UnitOfWork(1448667590)--Thread(Thread[Test worker,5,main])--end unit of work commit
[EL Finer]: transaction: 2016-09-30 14:30:50.775--UnitOfWork(1448667590)--Thread(Thread[Test worker,5,main])--resume unit of work
2016-09-30 14:30:50,775 | TRACE | org.springframework.transaction.support.AbstractPlatformTransactionManager:952 - Triggering afterCommit synchronization
2016-09-30 14:30:50,775 | TRACE | org.springframework.transaction.support.AbstractPlatformTransactionManager:968 - Triggering afterCompletion synchronization
2016-09-30 14:30:50,775 | TRACE | org.springframework.transaction.support.TransactionSynchronizationManager:331 - Clearing transaction synchronization
2016-09-30 14:30:50,776 | TRACE | org.springframework.transaction.support.TransactionSynchronizationManager:243 - Removed value [org.springframework.orm.jpa.EntityManagerHolder@4569d6c9] for key [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@33d8b566] from thread [Test worker]
2016-09-30 14:30:50,776 | TRACE | org.springframework.transaction.support.TransactionSynchronizationManager:243 - Removed value [org.springframework.jdbc.datasource.ConnectionHolder@53d1206a] for key [JDBC URL = jdbc:sqlserver://somehost:1433;databaseName=Database, Username = test, partitions = 1, max (per partition) = 10, min (per partition) = 0, idle max age = 60 min, idle test period = 240 min, strategy = DEFAULT] from thread [Test worker]
2016-09-30 14:30:50,776 | DEBUG | org.springframework.orm.jpa.JpaTransactionManager:600 - Closing JPA EntityManager [org.eclipse.persistence.internal.jpa.EntityManagerImpl@8f90fd5] after transaction
2016-09-30 14:30:50,776 | DEBUG | org.springframework.orm.jpa.EntityManagerFactoryUtils:432 - Closing JPA EntityManager
[EL Finer]: transaction: 2016-09-30 14:30:50.776--UnitOfWork(1448667590)--Thread(Thread[Test worker,5,main])--release unit of work
[EL Finer]: connection: 2016-09-30 14:30:50.776--ClientSession(287210054)--Thread(Thread[Test worker,5,main])--client released
2016-09-30 14:30:50,776 | TRACE | org.springframework.test.context.TestContextManager:394 - afterTestMethod(): instance [com.somecompany.platform.jpa.ssoerrorlog.SSOErrorLogServiceTest@314c32e8], method [public void com.somecompany.platform.jpa.ssoerrorlog.SSOErrorLogServiceTest.test_logError()], exception [null]
2016-09-30 14:30:50,777 | DEBUG | org.springframework.test.context.support.DirtiesContextTestExecutionListener:94 - After test method: context [DefaultTestContext@6cce3af3 testClass = SSOErrorLogServiceTest, testInstance = com.somecompany.platform.jpa.ssoerrorlog.SSOErrorLogServiceTest@314c32e8, testMethod = test_logError@SSOErrorLogServiceTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@37770c96 testClass = SSOErrorLogServiceTest, locations = '{}', classes = '{class com.somecompany.platform.jpa.ssoerrorlog.SSOErrorLogServiceTest$Config}', contextInitializerClasses = '[]', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]], class dirties context [false], class mode [null], method dirties context [false].
2016-09-30 14:30:50,777 | TRACE | org.springframework.test.context.TestContextManager:437 - afterTestClass(): class [class com.somecompany.platform.jpa.ssoerrorlog.SSOErrorLogServiceTest]
2016-09-30 14:30:50,778 | DEBUG | org.springframework.test.context.support.DirtiesContextTestExecutionListener:126 - After test class: context [DefaultTestContext@6cce3af3 testClass = SSOErrorLogServiceTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@37770c96 testClass = SSOErrorLogServiceTest, locations = '{}', classes = '{class com.somecompany.platform.jpa.ssoerrorlog.SSOErrorLogServiceTest$Config}', contextInitializerClasses = '[]', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]], dirtiesContext [false].

您是否尝试将
@PersistenceContext
更改为
@persistenceunit?


通过
@PersistenceContext
获得的EntityManager是容器管理的实体管理器,因为容器将负责管理“实体管理器”,而通过
@PersistenceUnit/EntityManager工厂获得的EntityManager.createEntityManager()
是应用程序管理实体管理器,开发人员必须在代码中管理某些事情(例如释放EntityManager获取的资源)。

您是否尝试将
@PersistenceContext
更改为
@persistenceUnit?

通过
@PersistenceContext
获得的EntityManager是容器管理的实体管理器,因为容器将负责管理“实体管理器”,而通过
@PersistenceUnit/EntityManager工厂获得的EntityManager.createEntityManager()
是应用程序管理的实体管理器,开发人员必须在代码中管理某些事情(例如释放EntityManager获取的资源)。

尝试释放查询

query.unwrap(ProcedureOutputs.class).release();
尝试释放查询

query.unwrap(ProcedureOutputs.class).release();

我们在
execute()
调用中遇到了同样的问题。连接没有关闭。查看代码可以更清楚地看到:


调用函数解决了这个问题。

我们在调用
execute()
时遇到了同样的问题。连接没有关闭。查看代码可以更清楚地看到:


调用已解决的问题。

您如何指定数据源,它是否有连接重用限制?366102提到了类似的东西,但我之前的印象是代码正在关闭finally块中的所有连接。您能否启用打印错误连接从何处获得的堆栈的调试?有些服务器能够处理它,但我不知道这是否是您的设置中的一个选项。我认为这不是重用限制,因为在测试中运行第一个连接时不会释放它。通过在数据源上设置泄漏检测阈值,我可以很快在日志输出中看到哪些连接没有被释放,但无法获得任何可能表明原因的输出。我假设这是因为SP正在插入数据,需要@Transaction才能提交。如果没有外部事务,它应该自动提交并释放。将logging设置为finest并查看可能记录的内容。从日志中可以清楚地看出,如果没有@Transactional,连接不会被释放,但正如您所说,我希望它会自动提交并被释放。您是如何指定数据源的,它会有连接重用限制吗?366102提到了类似的东西,但我之前的印象是代码正在关闭finally块中的所有连接。您能否启用打印错误连接从何处获得的堆栈的调试?有些服务器能够处理它,但我不知道这是否是您的设置中的一个选项。我认为这不是重用限制,因为在测试中运行第一个连接时不会释放它。通过在数据源上设置泄漏检测阈值,我可以很快在日志输出中看到哪些连接没有被释放,但无法获得任何可能表明原因的输出。我假设这是因为SP正在插入数据,需要@Transaction才能提交。如果没有外部事务,它应该自动提交并释放。将logging设置为finest并查看可能记录的内容。从日志中可以清楚地看到,没有@Transactional,连接不会被释放,但正如您所说,我希望它能够自动提交并被释放。