非数据库应用程序事务 [英] Non-database application transactions

查看:160
本文介绍了非数据库应用程序事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将非数据库动作封装到事务中?

How can an encapsulate non-database actions into transactions?

例如,假设我有一些顺序调用的方法/函数,一些更新数据库,一些文件系统上的更新文件,HttpSession中的一些更新参数,一些更新缓存等。如果后者之一失败,像更新会话,我如何回滚文件系统上,在数据库中的所有其他更改等?

For example, say I have some number of methods/functions called in sequence, some update the database, some update files on the filesystem, some update parameters in the HttpSession, some update the cache, etc. If one of the latter ones fails, like updating the session, how can I roll back all the other changes on the file system, in the DB, etc.?

环境是一个类似Tomcat的Java Servlet容器,类似于Struts2,任何RDBMS,像Hibernate或Ibatis等持久层等。

The environment is a Java Servlet container like Tomcat with something like Struts2, any RDBMS, a persistence layer like Hibernate or Ibatis, etc.

推荐答案

所有这些都必须是事务资源,能够参与一个两阶段提交协议,以便做你想要的。

All these would have to be transactional resources, able to participate in a 2-phase commit protocol, in order to do what you want.

除非您的代码中有错误,否则会话的更新决不会失败。因此,您可以在其他更新成功后执行此操作。如果你需要事务处理,应该避免文件系统。备选方案可以是通过保存到文件系统开始,然后进行数据库更新,如果数据库更新失败,则接受在文件系统中有无用的文件。

Unless you have a bug in your code, the update to the session should never fail. You could thus do it after the other updates are successful. The file system should just be avoided if you need something transactional. The alternative could be to start by saving to the file system, then do the database updates, and accept to have useless files in the file system if the DB update fails.

这篇关于非数据库应用程序事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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