我在为 Oracle 上的 hibernate 设置事务超时时遇到问题。它不起作用。任何人都可以帮忙吗? “SaveOrUpdate”不会在规定的 10 秒内返回。它会挂很长时间。 我使用的是 Oracle 10r2。
hibernate 配置文件
<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 类
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){}
}
}
}
}
最佳答案
需要在事务开始之前设置超时。
而不是
tran = dbSession.beginTransaction();
tran.setTimeout(10);// 10 seconds
尝试
tran = dbSession.getTransaction();
tran.setTimeout(10);
tran.begin();
关于oracle - 事务超时在 Oracle 的 Hibernate 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/635047/