简体   繁体   English

Spring Boot-从非事务性原因更新中校准事务性方法

[英]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: 如果我的推论是正确的,则会发生以下步骤:

  1. When a request comes and hits the controller, an entity manager (hibernate session) is attached to the current thread. 当请求到达并命中控制器时,实体管理器(休眠会话)将附加到当前线程。 (?) (?)
  2. A controller method which handles the request, calls organizationService.addUser method 处理请求的控制器方法,调用organizationService.addUser方法
  3. Organization is fetched by findById method which is by default transactional and readonly (according to SimpleJpaRepository implementation). 组织是通过findById方法获取的, 方法默认为事务性和只读的(根据SimpleJpaRepository实现)。 This transaction closes after this, but the entity manager bound to it is still running and organization entity remains managed. 此后该事务关闭,但是绑定到它的实体管理器仍在运行,并且组织实体仍处于管理状态。
  4. The updates occur 更新发生
  5. invitationService.deleteByUserEmail(user.getEmail()); InvitationService.deleteByUserEmail(user.getEmail()); is called. 叫做。 Because it's transactional method a transaction is started. 因为它是事务方法,所以开始事务。 Deletion occurs, and a transaction is committed and flushes all changes into the database, even the changes made in organization entity. 发生删除,并且提交事务并将所有更改(包括组织实体中的更改)刷新到数据库中。

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.

相关问题 在另一个实例中从事务方法调用 spring 非事务方法时,事务是否得到传播? - When calling a spring Non-Transactional method from a Transactional Method in another instance, does the transaction get propagated? 如何从事务方法调用非事务方法 - How to call non-transactional methods from a Transactional method 例外-非交易 - Exception - non-transactional Java Spring:mongodb的事务版本和非事务版本的兼容性 - Java Spring: Compatibility for both transactional and non-transactional versions of mongodb 是否可以将整个@Transactional类的一个方法标记为非事务性的 - Is it possible to mark one method of an entire @Transactional class as non-transactional 在Spring中是否可以从非事务方法中调用事务方法? - Is it possible to invoke transactional method from non transactional method in Spring? Spring 3 MVC Hibernate 3.5.4 hibernateTemplate没有关闭连接(非事务性) - Spring 3 MVC Hibernate 3.5.4 hibernateTemplate not closing connections (non-transactional) 如何在Spring中创建非事务性JUnit集成测试? - How to create non-transactional JUnit integration tests in Spring? 延迟加载在Hibernate的非事务类/方法中工作 - Lazy loading working in non-transactional class/method in Hibernate 事务回滚后调用非事务方法 - Calling non-transactional method after transaction is rolled back
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM