This discussion is archived
4 Replies Latest reply: Oct 29, 2012 9:01 AM by rp0428 RSS

how to implement rollback on my application?

971020 Newbie
Currently Being Moderated
hello!

I'm new to this forum and to Java.

I need to implement atomic transactions in my application. For instance, it's a webservice that gets an instruction like "set firewall rules A, B and C", then it request another webservice (or iptables) to set each of the rules individually.
Now assume I can set rule A and B, but rule C fails. This means I must unset rules A and B (and possibly check if C wasn't really setted before the other service failed to respond).

This means it would be nice to "execute a rollback script" or something like that when the operation fails (and, btw, this script can also fail at some point and should be retried or go to an error state depending on the kind of failure).

As you can see, this can get very complex. I was reading a bit about reflections and maybe it can be used to solve this problem. But I wonder if anyone can give me some point, some code that do that already, article/book that talks about this problem?

thanks very much
  • 1. Re: how to implement rollback on my application?
    800170 Newbie
    Currently Being Moderated
    Man, I haven't been on these forums in quite some time.

    What you're asking ins't really java-dependent. In fact, I'd say that, if what you're describing is all that your webservice is doing, java ins't necessarily the best tool for this job.
    What you need to do is focus on the steps that you'll have to do in order to accomplish this. For the example that you gave below with iptables, you could do something simple like:
    1) Before you apply rules A, B and C, run iptables-save and store the output to a file.
    2) Execute rule A
    a) If rule fails, goto 5
    b) If rule succeeds, goto 3
    3) Execute rule B
    a) If rule fails, goto 5
    b) If rule succeeds, goto 4
    4) Execute rule C
    a) If rule fails, goto 5
    b) If rule succeeds, goto 6
    5) Execute iptables-load from the file stored in Rule #1
    6) You're done.
  • 2. Re: how to implement rollback on my application?
    971020 Newbie
    Currently Being Moderated
    hello
    thanks for your answer
    I agree with you that this isn't exactly a Java-specific question, but it may have a Java-specific solution (ie, some kind of property of the language - like reflection as mentioned before - that might help with it).

    That said, your solution certainly solves the problem, but not only it's "cheating" (ie, it's depending on iptables "own rollback", so it's very specific for this case), it also wouldn't work with concurrency.

    Besides, the iptables example is really one problem I'm trying to solve, but it's not the only one. My application has a very complex flow, and some parts of it requires rollback operations eventually, so I'm interested on how to store this rollback information using Java.

    My first idea is to store it on a database, so even if the application crashes (or even if the computer where it's running is rebooted) it can rollback. (That's NOT a requirement, since this should be very rare, but it seems to me that relying on a database to store this information might even make things easier)
    But I don't know exactly WHAT to store. Maybe a json-encoded array like:
    ["UNDO operation A", "UNDO operation B"]
    and then I'd have a parser to figure out what the program really would need to do... I don't like that solution though.
    So maybe storing real calls to functions and then trying to use reflection... well, I really don't know

    any suggestion is very welcomed :)
  • 3. Re: how to implement rollback on my application?
    gimbal2 Guru
    Currently Being Moderated
    JEE containers have built in application scoped transactions - the design you're looking for is basically what JEE already solves. Check out Jboss Transactions for example:

    http://www.jboss.org/jbosstm
  • 4. Re: how to implement rollback on my application?
    rp0428 Guru
    Currently Being Moderated
    >
    My application has a very complex flow, and some parts of it requires rollback operations eventually, so I'm interested on how to store this rollback information using Java.
    >
    Whether you can even do a 'rollback' of an operation depends on what the operation itself does. There are many operations that cannot be 'undone' except by restoring the previous state from a backup.

    Consider a Java array that you load from a data source. Your code now iterates the array and replaces every entry that has a value of 2 with a 3. There is no way to 'rollback' that code and restore the array to the values that existed before the code executed except by reloading the data again. And the data source might not be available anymore or could have been modified by another user or process.

    So before you start trying to design a rollback architecture you need to first design and finalize your state processes and architecture.

    And before you embark on that I would suggest you search for a product (any product) made by another company that you think supports the type of 'rollback' that you are envisioning. Because if you can't find one your chances of success are slim to none.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points