[英]Spring Boot - caling transactional method from non-transactional cause update
I'm wondering about a case, which I encountered recently. 我想知道最近遇到的一个案例。 Suppose we have a service method as follows:
假设我们有一个如下的服务方法:
@Override
public User addUser(UUID organizationId, User user) {
Organization organization = organizationRepository.findById(organizationId).orElseThrow(IllegalArgumentException::new);
user.setOrganization(organization);
organization.getUsers().add(user);
invitationService.deleteByUserEmail(user.getEmail());
return user;
}
Organization is provided by a repository method: 组织是通过存储库方法提供的:
@Repository
public interface OrganizationRepository extends JpaRepository<Organization, UUID> {
Optional<Organization> findById(UUID id);
}
Adding of users works only because invitationService.deleteByUserEmail(user.getEmail());
添加用户的工作仅是因为
invitationService.deleteByUserEmail(user.getEmail());
is present. 存在。 The entire method belongs to another service and looks as follows:
整个方法属于另一个服务,其外观如下:
@Override
@Transactional
public void deleteByUserEmail(String userEmail) {
invitationRepository.deleteByUserEmail(userEmail);
}
I am not sure if I correctly understand why adding a user works. 我不确定我是否正确理解为什么添加用户有效。 According to Hibernate Docs, automatic updates occur only, when an entity is in managed state and the update takes place in a single transaction.
根据Hibernate Docs的说法,仅当实体处于受管状态并且更新在单个事务中进行时,才会发生自动更新。 If my reasoning is correct there are the following steps which occur:
如果我的推论是正确的,则会发生以下步骤:
Is it possible despite of addUser method is non-transactional ? 尽管addUser方法是非事务性的,是否有可能?
On one of you configuration classes you may have special filters registered which basically keep the session / entitymanager opened for the life of an entire request: 在您的一个配置类中,您可能注册了特殊的过滤器,这些过滤器基本上在整个请求的生命周期内都打开会话/实体管理器:
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
or 要么
org.springframework.orm.hibernate5.support.OpenSessionInViewFilter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.