我有以下服务类:
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包下。
我在春季使用休眠作为我的 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 的create和select语句,但看不到insert)。
为什么在调用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