有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java spring事务超时不起作用

我正在尝试停止/回滚运行时间过长的事务。但是,通过配置spring事务管理器的timeout属性,它似乎不起作用。 我的环境:

  1. Spring2.5.6+JPA+Hibernate3.2.6
  2. 甲骨文10g
  3. jdk 1.6.0_17

由于spring有助于管理我的事务,其配置如下:

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <property name="dataSource" ref="dataSource" />
</bean>

<!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean 
    below) -->
<tx:advice id="defaultTxAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <!-- Keep SequenceService in a isolation transaction -->
        <tx:method name="get*" read-only="true" />
        <!-- By default, A runtime exception will rollback transaction. -->
        <tx:method name="*" timeout="10" rollback-for="ApplicationException" />
    </tx:attributes>
</tx:advice>

我有一个票务服务,它将向数据库插入一些记录,只需让它多睡15秒

public class DefaultTicketService implements TicketService{
    public void sell() {
        // checking and insert some records to underlying database
        ....
        // sleep to reach the transaction deadline
        try {Thread.sleep(15 * 1000);} catch(Exception e){}
    }
}

我还修改了spring的组织。springframework。奥姆。jpa。JpaTransactionManager以输出更多调试信息

protected void doBegin(Object transaction, TransactionDefinition definition) {
    ... ...        
    // Register transaction timeout.
    int timeout = determineTimeout(definition);
    if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
        if (logger.isDebugEnabled()) {
            logger.debug("****setTimeoutinSeconds(" + timeout
                    + " seconds) to EntityManager(" + txObject.getEntityManagerHolder()
                    + "), the transaction begin time:"
                    + new Date(System.currentTimeMillis()));
        }
        txObject.getEntityManagerHolder().setTimeoutInSeconds(timeout);
    }
    ... ... 
}

protected void doCommit(DefaultTransactionStatus status) {
    JpaTransactionObject txObject = (JpaTransactionObject) status.getTransaction();
    if (status.isDebug()) {
        logger.debug("Committing JPA transaction on EntityManager ["
                + txObject.getEntityManagerHolder().getEntityManager() + "]");
    }
    try {
        if (status.isDebug()) {
            logger.debug("The deadline of entityManager("
                    + txObject.getEntityManagerHolder().getEntityManager() + "):"
                    + txObject.getEntityManagerHolder().getDeadline() + ", and current time:"
                    + new Date(System.currentTimeMillis()));
        }
        EntityTransaction tx = txObject.getEntityManagerHolder().getEntityManager()
                .getTransaction();
        tx.commit();
    ... ...
}

在完成测试后,结果超出了我的预期,事务最终被提交。以下是测试的输出:

[JpaTransactionManager] Opened new EntityManager [org.hibernate.ejb.EntityManagerImpl@350225] for JPA transaction
[JpaTransactionManager] ****[Begin]timeout:10 seconds,The deadline of entityManager(org.hibernate.ejb.EntityManagerImpl@350225):null, and current time:Tue Sep 06 15:05:42 CST 2011
[JpaTransactionManager] Exposing JPA transaction as JDBC transaction [SimpleConnectionHandle: com.mchange.v2.c3p0.impl.NewProxyConnection@1eb41d6]
[JpaTransactionManager] Found thread-bound EntityManager [org.hibernate.ejb.EntityManagerImpl@350225] for JPA transaction
... ...
[JpaTransactionManager] Initiating transaction commit
[JpaTransactionManager] Committing JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@350225]
[JpaTransactionManager] ****[Commit]The deadline of entityManager(org.hibernate.ejb.EntityManagerImpl@350225):Tue Sep 06 15:05:52 CST 2011, and current time:Tue Sep 06 15:05:58 CST 2011
[JpaTransactionManager] Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@350225] after transaction
[EntityManagerFactoryUtils] Closing JPA EntityManager

从调试信息来看,很明显当前时间已经超过了截止日期,那么为什么spring不回滚事务呢??在我的理解中,如果我设置了超时,比如10秒,Spring将在启动新事务时启动计时器,如果计时器达到时间限制,它将回滚事务。你能告诉我为什么吗

更新>&燃气轮机

当浏览JavaEE7教程时,发现似乎是JPA2。1提供了对锁超时的支持(一般事务超时是由获取锁超时引起的)

http://docs.oracle.com/javaee/7/tutorial/doc/persistence-locking002.htm

42.2.2.1悲观锁定超时


共 (2) 个答案

  1. # 2 楼答案

    经过一些研究,我发现问题在于TimerTask线程没有使用通常会启动和提交事务的spring代理。从理论上讲,您可以手动将支持编码到TimerTask中,以使用代理,但这对我来说似乎很麻烦

    Spring有自己的调度框架,您可以使用它代替TimerTask来执行可运行文件,并且不需要太多代码更改

    以下是文件:

    http://static.springsource.org/spring/docs/3.0.5.RELEASE/reference/scheduling.html