Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-database application transactions

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.

like image 505
Val Schuman Avatar asked Oct 05 '11 21:10

Val Schuman


1 Answers

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.

like image 135
JB Nizet Avatar answered Oct 24 '22 17:10

JB Nizet