相关疑难解决方法(0)

返回Java 8流的Spring存储库方法不会关闭JDBC连接

我有一个Spring data存储库:

@Repository
interface SomeRepository extends CrudRepository<Entity, Long> {
    Stream<Entity> streamBySmth(String userId);
}
Run Code Online (Sandbox Code Playgroud)

我在一些Spring bean中调用该方法:

@Scheduled(fixedRate = 10000)
private void someMethod(){
    someRepository.streamBySmth("smth").forEach(this::callSomeMethod);
}
Run Code Online (Sandbox Code Playgroud)

我正在使用MySQL数据库.当我在一些成功的方法调用后运行应用程序时,它会引发异常:

o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 08001
o.h.engine.jdbc.spi.SqlExceptionHelper   : Could not create connection to database server.
o.s.s.s.TaskUtils$LoggingErrorHandler    : Unexpected error occurred in scheduled task.

org.springframework.dao.DataAccessResourceFailureException: Unable to acquire JDBC Connection; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
Run Code Online (Sandbox Code Playgroud)

看来,Spring没有正确关闭连接.如果我已经将方法返回值更改为ListStream它正常工作.

更新: Spring Boot版本是1.4.1.RELEASE

java spring spring-jdbc spring-data java-stream

8
推荐指数
1
解决办法
6095
查看次数

标签 统计

java ×1

java-stream ×1

spring ×1

spring-data ×1

spring-jdbc ×1