非数据库应用程序事务
如何将非数据库操作封装到事务中?
例如,假设我按顺序调用了一些方法/函数,一些更新数据库,一些更新文件系统上的文件,一些更新 HttpSession 中的参数,一些更新缓存等。如果后一个失败,就像更新会话一样,如何回滚文件系统、数据库等中的所有其他更改?
环境是一个 Java Servlet 容器(例如 Tomcat),带有 Struts2 之类的东西、任何 RDBMS、持久层(例如 Hibernate 或 Ibatis 等)。
How can an encapsulate non-database actions into transactions?
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.?
The environment is a Java Servlet container like Tomcat with something like Struts2, any RDBMS, a persistence layer like Hibernate or Ibatis, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所有这些都必须是事务性资源,能够参与两阶段提交协议,以便执行您想要的操作。
除非代码中有错误,否则会话更新永远不会失败。因此,您可以在其他更新成功后执行此操作。如果您需要事务性的东西,应该避免使用文件系统。另一种方法是首先保存到文件系统,然后进行数据库更新,并在数据库更新失败时接受文件系统中存在无用文件。
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.