gpt4 book ai didi

java - 未提交声明性 Spring 交易

转载 作者:行者123 更新时间:2023-11-29 09:14:55 28 4
gpt4 key购买 nike

我有以下服务类:

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);
}
}

我的服务接口(interface)位于 com.mysystem.services 包下,我的服务实现位于 com.mysystem.services.impl 包下。

我在 Spring 使用 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 不会刷新方法调用之间的 session 。我可以通过控制台输出验证此表单(我可以看到 hibernate 的 createselect 语句,但看不到 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

最佳答案

你能试试这个吗。

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

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

关于java - 未提交声明性 Spring 交易,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10070378/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com