在 Spring Boot 控制器中使用 Stream 返回类型会导致“java.sql.SQLException: Closed Resultset: next”

发布于 2025-01-20 03:16:05 字数 1837 浏览 0 评论 0原文

当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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苄①跕圉湢 2025-01-27 03:16:05

我通过使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文