Spring数据和mongodb - 在@Transactional中用弹簧简单回滚 [英] Spring data and mongodb - simple roll back with spring within @Transactional

查看:1016
本文介绍了Spring数据和mongodb - 在@Transactional中用弹簧简单回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个存储库,一个用于mongodb(DocumentRepository),另一个用于hibernate实体(EntityRepository)

我有一个简单的服务:

  @Transactional 
public doSomePersisting(){
try {
this.entityRepository.save(entity);
this.documentRepository.save(document);

catch(...){
// Rollback mongoDB here
}
}

是否可以在// Rollback mongoDB here这一行中回滚mongoDB?
我已经从实体部分回滚(Transactional annotation)了。

解决方案

MongoDB不支持事务至少不在单个文档的范围之外)。如果您想回滚更改,则需要自己手动进行修改。那里有几个资源描述了在Mongo中实现自己的事务的方法,如果你在某些情况下真的需要它们的话。你可以看看..



http://docs.mongodb.org/manual/tutorial/perform-two-phase-commits/



这是只是对你可以使用的模式的解释。如果您发现您绝对需要应用程序中的交易,则应考虑MongoDB是否适合您的需求。


I have 2 repositories, one for mongodb (DocumentRepository) and the other for hibernate entity (EntityRepository)

I have a simple service:

 @Transactional
 public doSomePersisting() {
     try {
           this.entityRepository.save(entity);
           this.documentRepository.save(document);
     }
     catch(...) {
         //Rollback mongoDB here
     }
 }

Is it possible to rollback the mongoDB on the "//Rollback mongoDB here" line? I already got a rollback from the entity part (Transactional annotation)

解决方案

MongoDB doesn't support transactions (at least not outside the scope of a single document). If you want to roll back changes you will need to handcraft that yourself. There are a few resources out there that describe ways of implementing your own transactions in Mongo if you really need them in certain circumstances. You could take a look at..

http://docs.mongodb.org/manual/tutorial/perform-two-phase-commits/

This is just an explanation of a pattern you could use. If you find that you absolutely need transactions in your application, you should consider whether MongoDB is a good fit for your needs.

这篇关于Spring数据和mongodb - 在@Transactional中用弹簧简单回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆