声明的spring事务未被提交

时间:2012-04-09 07:45:00

标签: java spring hibernate spring-transactions

我有以下服务类:

public class UserLoginServiceImpl implements UserLoginService {

  private UserDAO dao;

  public UserLoginServiceImpl(UserDAO dao) {this.dao = dao; }

  public User login(String userName, String password) {
     User u = dao.findUserByCredentials(userName, password);
     if (u == null) {
         throw new UserNotFoundException();
     }
     return u;
}

public class UserManagementServiceImpl implements UserManagementService {

  private UserDAO dao;

  public UserManagementServiceImpl(UserDAO dao) { this.dao = dao; }

  public void createUser(User u) {
     dao.save(u);
  }
}

我的服务接口位于 com.mysystem.services 包下,以及 com.mysystem.services.impl 包下的服务实现。

我在春天使用hibernate作为我的jpa实现和声明式事务。我的配置文件如下:

       

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

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="*"/>
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut id="serviceMethod" expression="execution(* com.mysystem.services.impl.*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
</aop:config>

<!--
    Beans
 -->
<bean id="userDao" class="com.mysystem.dao.UserDAOImpl">
    <constructor-arg ref="entityManagerFactory" />
</bean>

<bean id="userLoginService" class="com.mysystem.services.impl.UserLoginServiceImpl">
    <constructor-arg ref="userDao" />
</bean>
<bean id="userManagementService" class="com.mysystem.services.impl.UserManagementServiceImpl">
    <constructor-arg ref="userDao" />
</bean>

最后,在我的代码中的某处,我运行以下内容:

UserManagementService userManagementService = context.getBean(UserManagementService.class);
userManagementService.createUser(new User("test", "test"));

UserLoginService userLogginService = context.getBean(UserLoginService.class);
User user = userLogginService.login("test", "test");

导致 login()方法抛出 UserNotFoundException ,因为它找不到先前由 createUser()插入的用户,这意味着hibernate不会刷新方法调用之间的会话。我可以在控制台输出中验证这个形式(我可以看到hibernate的创建选择语句,但不能插入)。

为什么在调用 createUser()后很快就没有提交事务?我在这做错了什么?

编辑:

使用@Transactional注释而不是声明性事务。然而,使用@Transactional对我来说不是一个选择。我需要实现声明式事务。

下面是spring的调试输出:

13:03:18,174 DEBUG [main] jpa.JpaTransactionManager     - Creating new transaction with name [com.mysystem.services.impl.UserManagementServiceImpl.createUser]: PROPAGATION_REQUIRES_NEW,ISOLATION_DEFAULT
13:03:18,175 DEBUG [main] jpa.JpaTransactionManager     - Opened new EntityManager [org.hibernate.ejb.EntityManagerImpl@1af6a711] for JPA transaction
Hibernate: 
    select
        nextval ('hibernate_sequence')
13:03:18,255 DEBUG [main] jpa.JpaTransactionManager     - Initiating transaction commit
13:03:18,255 DEBUG [main] jpa.JpaTransactionManager     - Committing JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@1af6a711]
13:03:18,256 DEBUG [main] jpa.JpaTransactionManager     - Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@1af6a711] after transaction

    13:03:18,263 DEBUG [main] jpa.JpaTransactionManager     - Creating new transaction with name [com.mysystem.services.impl.UserLoginServiceImpl.login]: PROPAGATION_REQUIRES_NEW,ISOLATION_DEFAULT
    13:03:18,263 DEBUG [main] jpa.JpaTransactionManager     - Opened new EntityManager [org.hibernate.ejb.EntityManagerImpl@1533badd] for JPA transaction
    Hibernate: 
        /* 
    from
        com.mysystem.domain.User u 
    where
        u.userName = :userName 
        and u.password = :password */ select
            user0_.id as id0_,
            user0_.password as password0_,
            user0_.userName as userName0_ 
        from
            Users user0_ 
        where
            user0_.userName=? 
            and user0_.password=? limit ?
    13:03:18,374 TRACE [main] sql.BasicBinder     - binding parameter [1] as [VARCHAR] - test
    13:03:18,374 TRACE [main] sql.BasicBinder     - binding parameter [2] as [VARCHAR] - test
    13:03:18,379 DEBUG [main] jpa.JpaTransactionManager     - Initiating transaction commit
    13:03:18,379 DEBUG [main] jpa.JpaTransactionManager     - Committing JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@1533badd]
    13:03:18,379 DEBUG [main] jpa.JpaTransactionManager     - Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@1533badd] after transaction

1 个答案:

答案 0 :(得分:0)

你能尝试一下。

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>

        <tx:method name="createUser" propagation="REQUIRES_NEW" />
    </tx:attributes>
</tx:advice>