我有一个
Spring数据存储库:
@Repository interface SomeRepository extends CrudRepository<Entity,Long> { Stream<Entity> streamBySmth(String userId); }
@Scheduled(fixedrate = 10000) private void someMethod(){ someRepository.streamBySmth("smth").forEach(this::callSomeMethod); }
我正在使用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
看来,Spring没有正确关闭连接.如果我已将方法返回值更改为Stream from Stream,则它可以正常工作.
更新:
Spring Boot版本是1.4.1.RELEASE
解决方法
作为
reference documentation clearly states,Streams需要与try-with-resources块一起使用.
此外,通过使用@Transactional注释周围的方法,确保在消耗流时保持(只读)事务处于打开状态.否则,将应用默认设置,并尝试在存储库方法返回时释放资源.
@Transactional public void someMethod() { try (Stream<User> stream = repository.findAllByCustomQueryAndStream()) { stream.forEach(…); } }
原文地址:https://www.jb51.cc/java/120627.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。