5

我的交易有点问题。我将 Spring 3.1.1.RELEASE、Spring Data 1.0.3.RELEASE JPA 与 Hibernate 提供程序一起使用。当我开始一个 junit 测试时,在哪里用@Transactional它注释的方法似乎很好,但是当我启动整个应用程序时,没有错误,但事务不起作用。这是我的配置和示例代码:

应用程序上下文.xml

<context:annotation-config />
    <context:component-scan base-package="com.sheedo.upload" />
    <jpa:repositories base-package="com.sheedo.upload.repository" />
    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath*:messages/*.properties</value>
                <value>classpath*:*.properties</value>
            </list>
        </property>
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="persistenceUnit" />
        <property name="dataSource" ref="dataSource" />
    </bean>

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

    <bean id="dataSource" class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource">
        <property name="url" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

持久性.xml

<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
        <property name="hibernate.connection.charSet" value="UTF-8" />
        <property name="hibernate.show_sql" value="true" />
    </properties>
</persistence-unit>

用户存储库.java

公共接口 UserRepository 扩展 CrudRepository<User, Long>{ }

UserServiceImpl.java

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    @Transactional
    public void addUser(String name, String surname) {
        User u = new User(name, surname);
        userRepository.save(u);
        throw new RuntimeException(); // to invoke a rollback
    }
}

用户服务测试.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/META-INF/spring/root-context.xml" })
public class UserServiceTest {

    Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private UserService userService;

    @Test
    public void testUserAdd() {
        userService.addUser("John", "Doe");
    }

}

在这种 JUnit 测试的情况下,尽管服务方法使用@Transactional. 当我将此注释添加到testUserAdd()方法时,我会在控制台中得到它:

2012-05-17 11:17:54,208 INFO [org.springframework.test.context.transaction.TransactionalTestExecutionListener] - Rolled back transaction after test execution for test context [[TestContext@23ae2a testClass = UserRepositoryTest, testInstance = com.sheedo.upload.repository.UserRepositoryTest@7f52c1, testMethod = testUserAdd@UserRepositoryTest, testException = java.lang.RuntimeException, mergedContextConfiguration = [MergedContextConfiguration@111fd28 testClass = UserRepositoryTest, locations = '{classpath:/META-INF/spring/root-context.xml}', classes = '{}', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader']]]

我想这是正确的。那么,注释怎么可能@Transactional只在 Junit 测试类中有效,而在其他 spring bean 中无效呢?

我的理论是,它SpringJUnit4ClassRunner以某种方式提供了这种交易。我的 Spring 配置中是否有问题,即事务在我的应用程序中不起作用,而仅在 Junit 测试类中起作用?appContext 中缺少什么?

编辑: 日志:

2012-05-17 12:46:10,770 DEBUG [org.springframework.orm.jpa.JpaTransactionManager] - Creating new transaction with name [org.springframework.data.jpa.repository.support.SimpleJpaRepository.save]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
2012-05-17 12:46:10,770 DEBUG [org.springframework.orm.jpa.JpaTransactionManager] - Opened new EntityManager [org.hibernate.ejb.EntityManagerImpl@e4080] for JPA transaction
2012-05-17 12:46:10,979 DEBUG [org.springframework.orm.jpa.JpaTransactionManager] - Not exposing JPA transaction [org.hibernate.ejb.EntityManagerImpl@e4080] as JDBC transaction because JpaDialect [org.springframework.orm.jpa.DefaultJpaDialect@1f87491] does not support JDBC Connection retrieval
Hibernate: insert into user (name, surname) values (?, ?)
2012-05-17 12:46:11,062 DEBUG [org.springframework.orm.jpa.JpaTransactionManager] - Initiating transaction commit
2012-05-17 12:46:11,062 DEBUG [org.springframework.orm.jpa.JpaTransactionManager] - Committing JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@e4080]
2012-05-17 12:46:11,142 DEBUG [org.springframework.orm.jpa.JpaTransactionManager] - Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@e4080] after transaction
2012-05-17 12:46:11,142 DEBUG [org.springframework.orm.jpa.EntityManagerFactoryUtils] - Closing JPA EntityManager
4

1 回答 1

2

我有完全相同的问题。另外,请阅读<tx:annotation-driven/>在我的 Web 配置中添加标签的解决方案(spring-servlet.xml,而不是applicationContext.xml),并为我工作。

但我不认为这是一个好的解决方案,所以我试图理解为什么会发生这种情况......

好吧,事实证明,<context:component-scan>我在我的标签spring-servlet.xml中也包含了@Service扫描中的类(base-package规范太笼统了)。这很奇怪,因为我有一个include-filter就地引用@Controller注释......但无论如何,似乎 Web 层的应用程序上下文是创建@Service实例的上下文,而不是创建的应用程序上下文 -applicationContext.xml这是定义业务层——因为前者没有启用事务性……我没有任何事务。

解决方案(好一个):更好(更具体)component-scan的配置spring-servlet.xml

于 2012-06-03T10:04:07.933 回答