【问题标题】:Handling exception resulting from transaction timeout when using Spring / Hibernate / Postgres使用 Spring / Hibernate / Postgres 处理事务超时导致的异常
【发布时间】:2021-07-08 15:19:51
【问题描述】:

我正在尝试向系统用户提供有用的错误消息,其中某些查询可能需要很长时间才能执行。我使用 @Transactional(timeout = 5) 使用 Spring 设置了事务超时。

这按预期工作,但带注释的方法的调用者收到JpaSystemException: could not extract ResultSet 异常(由GenericJDBCException: could not extract ResultSet 引起,又由PSQLException: ERROR: canceling statement due to user request 引起)。

据我所知,异常是由于一旦超过超时,Hibernate 和 JDBC 驱动程序就会取消语句。

有什么方法可以确定异常是事务超时的结果,以便我可以向用户提供有关查询失败原因的错误消息?

应用程序使用 Spring 框架 4.2.9、Hibernate 4.3.11 和 Postgres JDBC 驱动程序 9.4.1209。我意识到这些已经很老了。如果较新的版本可以更轻松地处理这种情况,我很想知道。

【问题讨论】:

    标签: java spring postgresql hibernate


    【解决方案1】:

    您是否可以根据某些正则表达式模式检查原因原因的异常消息,或者仅检查消息是否包含这样的字符串?

    exception.getCause().getCause().getMessage().equals("ERROR: canceling statement due to user request")
    

    【讨论】:

      猜你喜欢
      • 2015-02-03
      • 2011-11-04
      • 1970-01-01
      • 2017-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-02
      • 2019-09-30
      相关资源
      最近更新 更多