JPA(和/或 Hibernate)-如何设置连接和/或查询的超时阈值?

发布于 2024-09-26 12:38:22 字数 449 浏览 1 评论 0原文

我试图弄清楚如何配置我的项目,以便 JPA 在配置的时间后超时并抛出异常。我希望在两种情况下发生这种情况:

  • 当 JPA 甚至无法连接到数据库
  • 时 当 JPA 查询花费的时间超过超时阈值才能返回结果集时

我不确定是否可以单独配置这两种情况(每个都有不同的超时阈值),或者如果两者都使用一个阈值。

我的项目当前设置如下:

  • 按照 JPA 2.0 规范进行编码
  • 使用 Hibernate 3.5.6 作为 JPA 实现
  • 将 c3p0 连接池与 Hibernate 一起
  • 使用 使用 persistence.xml 配置文件(使用 Hibernate 特定的属性值)仅在必要时)
  • 使用任何 Hibernate 特定的配置文件

I'm trying to figure out how to configure my project such that JPA will timeout and throw an exception after a configured amount of time. There are two situations where I would like this to happen:

  • When JPA is unable to even connect to the database
  • When a JPA query takes longer than the timeout threshold to return a result set

I'm not sure if these two scenarios can be configured separately (a different timeout threshold for each), or if one threshold is used for both.

My project is currently set up as follows:

  • Coding to the JPA 2.0 specification
  • Using Hibernate 3.5.6 as the JPA implementation
  • Using c3p0 connection pooling with Hibernate
  • Using persistence.xml configuration file (using Hibernate-specific property values only when necessary)
  • NOT using any Hibernate-specific configuration files

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

只怪假的太真实 2024-10-03 12:38:22

JPA2 持久性属性“javax.persistence.query.timeout”允许您设置查询的超时(假设底层 JDBC 驱动程序支持它)。

JPA2 persistence property "javax.persistence.query.timeout" allows you to set the timeout for a query (assuming the underlying JDBC driver supports it).

别把无礼当个性 2024-10-03 12:38:22

您应该设置 Java 客户端和数据库服务器默认超时:
如果您使用的是 Spring,请使用 @Transactional(timeout=10) ,它最终会设置 preparedStatement.setQueryTimeout() 提示以在 10 秒内关闭事务。
另外,使服务器超时时间略高于客户端超时时间,以便“java 客户端用户”超时,例如 15 秒:
https://mariadb.com/kb/en/mariadb/query-限制和超时/

You should be setting both Java client as well as database server default timeouts:
If you were using Spring, use @Transactional(timeout=10) which eventually sets the preparedStatement.setQueryTimeout() hint to close the transaction within 10 seconds.
Also, enable your server to timeout slightly above the client timeout so "java client user" to timeout, say, in 15 s:
https://mariadb.com/kb/en/mariadb/query-limits-and-timeouts/

人事已非 2024-10-03 12:38:22

Hibernate wiki 上的此页面详细介绍了如何配置 c3p0 连接池,包括超时设置。

请注意,这些类型的详细信息实际上与 JPA 或 Hibernate 没有太大关系,而是您在数据源/数据库连接(本例中为 c3p0)本身上设置的设置。

This page on the Hibernate wiki details how to configure the c3p0 connection pool, including the timeout settings.

Note that these type of details really don't have much to do with JPA or Hibernate, but are settings that you set on the DataSource / database connection (c3p0 in this case) itself.

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