Hibernate:设置默认查询超时?

我正在使用Hibernate对我的数据库进行一些大的查询,我有时会遇到超时。 我想避免在每个QueryCriteria上手动设置超时。

我可以为AnnotationConfiguration提供任何属性,为我运行的所有查询设置一个可接受的默认值吗?

如果没有,我如何在Hibernate查询上设置默认超时值?

JPA 2定义了javax.persistence.query.timeout提示以指定默认超时(以毫秒为单位)。 Hibernate 3.5(目前仍处于测试阶段)将支持此提示。

另见http://opensource.atlassian.com/projects/hibernate/browse/HHH-4662

JDBC有这个名为Query Timeout的机制,你可以调用java.sql.Statement对象的setQueryTime方法来启用这个设置。

Hibernate无法以统一的方式执行此操作。

如果您的应用程序检索JDBC连接vi java.sql.DataSource,则可以轻松解决该问题。

我们可以创建一个DateSourceWrapper来代理Connnection,它为它创建的每个Statement执行setQueryTimeout。

示例代码很容易阅读,我使用一些spring util类来帮助它。

 public class QueryTimeoutConfiguredDataSource extends DelegatingDataSource { private int queryTimeout; public QueryTimeoutConfiguredDataSource(DataSource dataSource) { super(dataSource); } // override this method to proxy created connection @Override public Connection getConnection() throws SQLException { return proxyWithQueryTimeout(super.getConnection()); } // override this method to proxy created connection @Override public Connection getConnection(String username, String password) throws SQLException { return proxyWithQueryTimeout(super.getConnection(username, password)); } private Connection proxyWithQueryTimeout(final Connection connection) { return proxy(connection, new InvocationHandler() { //All the Statement instances are created here, we can do something //If the return is instance of Statement object, we set query timeout to it @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object object = method.invoke(connection, args); if (object instanceof Statement) { ((Statement) object).setQueryTimeout(queryTimeout); } return object; }); } private Connection proxy(Connection connection, InvocationHandler invocationHandler) { return (Connection) Proxy.newProxyInstance( connection.getClass().getClassLoader(), ClassUtils.getAllInterfaces(connection), invocationHandler); } public void setQueryTimeout(int queryTimeout) { this.queryTimeout = queryTimeout; } 

}

现在我们可以使用这个QueryTimeoutConfiguredDataSource包装您存在的DataSource,以透明地为每个Statement设置Query Timeout!

Spring配置文件:

         

以下是几种方法:

  • 使用工厂或基类方法创建所有查询并在返回Query对象之前设置超时
  • 创建自己的org.hibernate.loader.Loader版本并在doQuery中设置超时
  • 使用AOP(例如Spring)返回Session的代理; 向它添加包含createQuery方法的建议,并在返回之前设置Query对象的超时

用于在查询级别设置全局超时值 – 将以下内容添加到配置文件。

     

在事务(INSERT / UPDATE)级别设置全局超时值 – 将以下内容添加到配置文件。