transaction timeout not working on hibernate with oracle

会有一股神秘感。 提交于 2019-12-12 19:06:01

问题


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.

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){}
            }
        }
    }

}

回答1:


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();



回答2:


You can see that at: http://community.jboss.org/wiki/TransactionTimeout.




回答3:


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.

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



来源:https://stackoverflow.com/questions/635047/transaction-timeout-not-working-on-hibernate-with-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!