问题描述
现在我正在使用Micronaut数据JPA-以完全阻塞的方式在我的应用程序中使用hibernate和postgresql,即:
@Repository
public class PetRepository extends CrudRepository<Pet,String> {}
我认为我可以将rxjava引入应用程序,以充分利用非阻塞Netty服务器的潜力。
现在我唯一需要担心的是,如何为响应流管理事务?
我的一些阻止代码如下:
@Singleton
@RequiredArgsConstructor
public class PetService {
private final PetRepository repository;
private final SomeOtherService someOtherService;
@Transactional
public void doSomethingWithAPet(String petId,String newName) {
Pet pet = repository.findById(petId);
if (pet.something()) {
someOtherService.doStuff();
pet.setName(newName);
}
repository.save(pet);
}
}
很明显,整个方法都包装在一个事务中。
现在让我说我将以被动方式进行此操作,例如:
@Transactional
public Completable doSomethingWithAPet(String petId,String newName) {
return repository
.findById(petId)
.flatMapCompletable(pet->{
if(pet.something()){
someOtherService.doStuff();
pet.setName(newName);
}
return repository.save(pet)
.ignoreElement();
}
}
文档说:
对于JPA,每个操作都将以其自己的事务和会话运行,因此需要注意获取正确的数据并避免分离对象。
我是否正确理解反应式版本将产生两个事务? -一个用于findById
,另一个用于save
?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)