I have a Spring data
repository:
@Repository
interface SomeRepository extends CrudRepository<Entity, Long> {
Stream<Entity> streamBySmth(String userId);
}
I am calling that method in some Spring bean:
@Scheduled(fixedRate = 10000)
private void someMethod(){
someRepository.streamBySmth("smth").forEach(this::callSomeMethod);
}
I am using MySQL database. And when I am running application after some successful method invocations it throws an exception:
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
It seems, that connection was not closed properly by Spring. If I have changed method return value to List
from Stream
it works correctly.
UPDATE: Spring Boot version is 1.4.1.RELEASE
close
on the stream? – AlfonzoalfordStream
manually. – UnrepairStream
from a repository, but aStream
is lazy, so how would Spring otherwise know when to close the connection?Stream
is anAutoClosable
as well, so you could use it in a try-with-resources clause. – AlfonzoalfordStream
s has ending operations, that means that you can not do anything else with Stream after them.forEach
is one of such operations. – Unrepair