在 Spring Boot 控制器中使用 Stream 返回类型会导致“java.sql.SQLException: Closed Resultset: next”
当jParepository流到REST API时,我有一个例外“ Java.sql.sqlexception:封闭结果集:下一个”
我正在使用Spring Boot,Spring Data JPA和Oracle Database/jdbc
存储库
@Repository
public interface EntityRepository extends JpaRepository<Entity, Long> {
@Transactional(readOnly = true)
public Stream<Entity> findAllByFilter(String filter);
}
服务
@Service
public class EntityService {
@Autowired private EntityRepository repository;
@Transactional(readOnly = true)
public Stream<Entity> getEntityByFilter(String filter) {
return repository.findAllByFilter(filter);
}
}
控制器
@RestController
@RequestMapping("/entities")
public class EntityController {
@Autowired private EntityService service;
@GetMapping(value = "/filter/{filter}")
@Transactional(readOnly = true)
public Stream<Entity> getEntitiesByFilter(@PathVariable("filter") String filter) {
return service.getEntityByFilter(filter);
}
}
消耗控制器中的流中的流,但是通过HTTP原因将其归还
引起的:java.sql.sqlexception:封闭结果集: oracle.jdbc.driver.insensitivesCrollablerEsultset.ensureopen(insensitivesCrollablerElesultset.java:201) 〜[ojdbc11-21.3.0.0.0.jar:21.3.0.0.0] at oracle.jdbc.driver.insensitivesCrollablerEsultSet.Next(insensitivesCrollablerEultSet.java:496) 〜[ojdbc11-21.3.0.0.0.jar:21.3.0.0.0] at com.zaxxer.hikari.pool.hikariproxyresultset.next(hikariproxyresultset.java) 〜[hikaricp-4.0.3.jar :?] at org.hibernate.internal.scrollableresultsimpl.next(scrollablereSultsimpl.java:101) 〜[Hibernate-Core-5.6.5.final.jar:5.6.5.final] at org.hibernate.query.internal.scrollableresultsiterator.hasnext(scrollablereSultSiterator.java:33) 〜[Hibernate-Core-5.6.5.final.jar:5.6.5.final] at java.util.iterator.foreachremaining(iterator.java:132)〜[?:?]
When streaming result from JpaRepository to Rest API, I have an exception "java.sql.SQLException: Closed Resultset: next"
I am Using Spring boot, Spring Data JPA and Oracle Database/JDBC
Repository
@Repository
public interface EntityRepository extends JpaRepository<Entity, Long> {
@Transactional(readOnly = true)
public Stream<Entity> findAllByFilter(String filter);
}
Service
@Service
public class EntityService {
@Autowired private EntityRepository repository;
@Transactional(readOnly = true)
public Stream<Entity> getEntityByFilter(String filter) {
return repository.findAllByFilter(filter);
}
}
Controller
@RestController
@RequestMapping("/entities")
public class EntityController {
@Autowired private EntityService service;
@GetMapping(value = "/filter/{filter}")
@Transactional(readOnly = true)
public Stream<Entity> getEntitiesByFilter(@PathVariable("filter") String filter) {
return service.getEntityByFilter(filter);
}
}
Consuming the stream in the controller works, but returning it through HTTP cause
Caused by: java.sql.SQLException: Closed Resultset: next at
oracle.jdbc.driver.InsensitiveScrollableResultSet.ensureOpen(InsensitiveScrollableResultSet.java:201)
~[ojdbc11-21.3.0.0.jar:21.3.0.0.0] at
oracle.jdbc.driver.InsensitiveScrollableResultSet.next(InsensitiveScrollableResultSet.java:496)
~[ojdbc11-21.3.0.0.jar:21.3.0.0.0] at
com.zaxxer.hikari.pool.HikariProxyResultSet.next(HikariProxyResultSet.java)
~[HikariCP-4.0.3.jar:?] at
org.hibernate.internal.ScrollableResultsImpl.next(ScrollableResultsImpl.java:101)
~[hibernate-core-5.6.5.Final.jar:5.6.5.Final] at
org.hibernate.query.internal.ScrollableResultsIterator.hasNext(ScrollableResultsIterator.java:33)
~[hibernate-core-5.6.5.Final.jar:5.6.5.Final] at
java.util.Iterator.forEachRemaining(Iterator.java:132) ~[?:?]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过使用 Query 类的 getResultStream 并使用谓词(而不是 JpaRepository)应用过滤器解决了我的问题,到 HTTP 客户端的流工作得很好
I resolved my issue by using getResultStream of Query class and applying my filters with Predicates (instead of JpaRepository), the Stream to a HTTP client work well