如何手动处理Spring 4交易?

时间:2016-05-20 13:28:26

标签: java spring hibernate transactions spring-test

如何在单@Test方法中以编程方式控制事务边界? Spring 4.x文档有一些clues但我认为自从测试抛出错误后我遗漏了一些东西:

java.lang.IllegalStateException: 
Cannot start a new transaction without ending the existing transaction first.

测试

import com.hibernate.query.performance.config.ApplicationConfig;
import com.hibernate.query.performance.config.CachingConfig;
import com.hibernate.query.performance.persistence.model.LanguageEntity;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.test.context.transaction.TestTransaction;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.PersistenceContext;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ApplicationConfig.class, CachingConfig.class }, loader = AnnotationConfigContextLoader.class)
@PersistenceContext
@Transactional(transactionManager = "hibernateTransactionManager")
@TestExecutionListeners({})
public class EHCacheTest extends AbstractTransactionalJUnit4SpringContextTests {

    private static Logger logger = LoggerFactory.getLogger(EHCacheTest.class);

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        logger.info("setUpBeforeClass()");
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        logger.info("tearDownAfterClass()");
    }

    @Autowired
    private SessionFactory sessionFactory;

    @Test
    public void testTransactionCaching(){
        TestTransaction.start();
        Session session = sessionFactory.getCurrentSession();
        System.out.println(session.get(LanguageEntity.class, 1));
        Query query = session.createQuery("from LanguageEntity le where le.languageId < 10").setCacheable(true).setCacheRegion("language");
        @SuppressWarnings("unchecked")
        List<LanguageEntity> customerEntities = query.list();
        System.out.println(customerEntities);
        session.getTransaction().commit();

        TestTransaction.flagForCommit();
        TestTransaction.end();

        // Second Transaction
        TestTransaction.start();

        Session sessionNew =  sessionFactory.getCurrentSession();
        System.out.println(sessionNew.get(LanguageEntity.class, 1));
        Query anotherQuery = sessionNew.createQuery("from LanguageEntity le where le.languageId < 10");
        anotherQuery.setCacheable(true).setCacheRegion("language");
        @SuppressWarnings("unchecked")
        List<LanguageEntity> languagesFromCache = anotherQuery.list();
        System.out.println(languagesFromCache);
        sessionNew.getTransaction().commit();

        TestTransaction.flagForCommit();
        TestTransaction.end();
    }
}

更新

还有一个细节:

所有出现的session.getTransaction().commit(); 必须,因为它们会中断交易工作流程。

1 个答案:

答案 0 :(得分:2)

TL; DR

为了避免这个问题,只需删除测试方法的第一行并使用已经可用的事务:

@Test
public void testTransactionCaching() {
    // Remove this => TestTransaction.start(); 
    // Same as before
}

详细答案

使用@Transactional或扩展AbstractTransactionalJUnit4SpringContextTests注释测试类时:

// Other annotations
@Transactional(transactionManager = "hibernateTransactionManager")
public class EHCacheTest extends AbstractTransactionalJUnit4SpringContextTests { ... }

该类中的每个测试方法都将在事务中运行。更准确地说, Spring Test Context (通过使用TransactionalTestExecutionListener)将在每个测试方法的开头打开一个事务,并在测试完成后将其回滚。

那么,你的testTransactionCaching测试方法:

@Test
public void testTransactionCaching() { ... }

在开始时会有一个开放的交易,并且您尝试通过以下方式手动打开另一个:

TestTransaction.start();

因此错误:

  

在不结束现有交易的情况下无法启动新交易   第一

为了避免此问题,只需删除测试方法的第一行并使用已经可用的事务。其他TestTransaction.*方法调用都可以,但只需删除第一个。