hibernate settimeout不工作mysql

时间:2014-09-03 16:41:28

标签: java mysql hibernate

我在java hibernate和mysql中工作。我想使用事务settimeout来实现应用程序的支付功能。我只是测试下面的代码,以便settimeout工作。

Transaction tx = (Transaction) threadTransaction.get();
try {
    if (tx == null) {
        Session session = (Session) threadSession.get();
        session.getTransaction().setTimeout(5);
        tx=session.beginTransaction();

        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if(session.getTransaction().isActive()) {
            System.out.println("session active");
        }
        else {
            System.out.println("session inactive");
        }

        threadTransaction.set(tx);
    }
}
catch (HibernateException e) {
    throw new HibernateException("", e);
}

但是打印会话处于活动状态,意味着超时不起作用。是什么原因?请帮忙 !

1 个答案:

答案 0 :(得分:0)

只要不需要做任何事情(效率等),Hibernate就无所事事。我认为这是你的测试所显示的,而不是超时不适用intended to do

“...确保数据库级别的死锁和具有巨大结果集的查询受到定义的超时限制。” 另请注意,无法在CMT bean中调用“setTimeout()...”

使用一些执行事务超时的代码测试事务超时,我认为您会发现它正常工作。