繁体   English   中英

使用 Spring / Hibernate / Postgres 时处理事务超时导致的异常

[英]Handling exception resulting from transaction timeout when using Spring / Hibernate / Postgres

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

这按预期工作,但带注释的方法的调用者收到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。 我意识到这些已经很老了。 如果较新的版本使处理这种情况更容易,我很想知道。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM