Hibernate超时在PostgreSQL 10中不起作用

时间:2019-02-22 16:25:49

标签: postgresql hibernate timeout settimeout query-timeout

persistence.xml:

      <properties>
        <property name="showSql" value="true"/>
        <property name="hibernate.dialect" value="${hibernate.dialect:org.hibernate.dialect.Oracle10gDialect}"/>
        <propertyname="hibernate.connection.datasource"value="java:/alarmmgr/alarmMonitorDB"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="false"/>
        <property name="hibernate.use_sql_comments" value="false"/>
        <property name="hibernate.jdbc.wrap_result_sets" value="false"/>
        <property name="hibernate.hibernate.cache.use_query_cache" value="true"/>
        <property name="javax.persistence.query.timeout" value="1" />

    </properties>

查询:

em.createQuery(query).setHint("javax.persistence.query.timeout", 1)
                                    .setFirstResult(payload.getStart())
                                    .setMaxResults(payload.getSize() > MAX_FETCH ? MAX_FETCH : payload.getSize())
                                    .getResultList()

查询返回了7,000行...但是超时不起作用。

1 个答案:

答案 0 :(得分:0)

JDBC Postgresql驱动程序不适用于属性javax.persistence.query.timeout, 所以我做了一个解决方法。

em.unwrap(Session.class).doWork(connection -> {
            long maxTimeOutMil = TimeUnit.SECONDS.toMillis(MAX_TIMEOUT);
            connection.createStatement().execute("set statement_timeout = " + maxTimeOutMil);
        });