事务超时不适用于 oracle 休眠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/635047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 17:54:44  来源:igfitidea点击:

transaction timeout not working on hibernate with oracle

oraclehibernatejdbctransactionstimeout

提问by chinhw

I am having problem setting the transaction timeout for hibernate on oracle. It does not work.Can anyone help? The "SaveOrUpdate" will not return within the stated 10 seconds. It will hang for a very long time. I am using Oracle 10r2.

我在为 oracle 上的休眠设置事务超时时遇到问题。它不起作用。有人可以帮忙吗?“SaveOrUpdate”不会在规定的 10 秒内返回。它会挂很长时间。我正在使用 Oracle 10r2。

Hibernate Configuration File

休眠配置文件

<hibernate-configuration>
<session-factory>
    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <property name="connection.url">jdbc:oracle:thin:@9.9.9.9:1521:orcl</property>
    <property name="connection.username">foouser</property>
    <property name="connection.password">foopass</property>
    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">20</property>
    <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
    <property name="current_session_context_class">thread</property>
    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">false</property>
    <!-- Mapping files -->
    <mapping resource="foo.hbm.xml"/>
</session-factory>
</hibernate-configuration>

Hibernate class

休眠类

public class foo implements Serializable
{
...
    public void save() throws Exception
    {
        Session     dbSession = null;
        Transaction tran      = null;
        try
        {
            dbSession = PersistenceMgr.getPersistenceMgr().getDbSession();
            tran      = dbSession.beginTransaction();
            tran.setTimeout(10); // 10 seconds
            dbSession.saveOrUpdate(this);
            tran.commit();
        }
        catch (HibernateException e)
        {
            if(tran!=null)
            {
                try{tran.rollback();}
                catch(HibernateException he){}
            }
            ...
        }
        finally
        {
            if( dbSession != null )
            {
                try{dbSession.close();}
                catch(HibernateException e){}
            }
        }
    }

}

回答by JMM

The timeout needs to be set before the transaction is started.

超时需要在事务开始之前设置。

instead of

代替

tran = dbSession.beginTransaction();
tran.setTimeout(10);// 10 seconds

try

尝试

tran = dbSession.getTransaction();
tran.setTimeout(10);
tran.begin();

回答by Steve Ebersole

Is this using JTA? If not, JDBC itself has no API for setting a transaction timeout so instead Hibernate attempts to manage that by tracking how much of the specified timeout period remains when executing a JDBC Statement within the transaction and setting the Statement.setQueryTimeout. It could be that there is either a bug in that logic or that the Oracle JDBC driver has a bug with regard to setting statement timeout.

这是使用 JTA 吗?如果不是,则 JDBC 本身没有用于设置事务超时的 API,因此 Hibernate 尝试通过跟踪在事务中执行 JDBC 语句并设置 Statement.setQueryTimeout 时指定的超时时间剩余多少来管理它。可能是该逻辑存在错误,或者 Oracle JDBC 驱动程序在设置语句超时方面存在错误。

If you find it is a Hibernate bug, https://hibernate.onjira.com

如果您发现它是 Hibernate 错误,请访问 https://hibernate.onjira.com