0

When I say "soft" transaction I mean something which doesn't involve persistence in some DB but just logical object modifications. Here's the story.

Plain JAVA application, no frameworks, possibly lots of threads.

  1. Object Ob, in his separate thread, starts doSomething(). Within doSomething(), this object members and (likely) other objects are modified.
  2. Ob's Thread is interrupted by some event handler. Someone wants Ob to doSomethingElse() instead of doSomething()!
  3. Ob completes doSomething() and checks if his thread was interrupted. If yes, I'd like him to:
    a) completely undo (rollback) doSomething(),
    b) doSomethingElse()

without having to trace any specific change made in doSomething(), also because I could have a lot of different doSomething methods for each object. I'm using no specific frameworks and I'd like to keep it "light", say.

Is there a way to achieve such a behavior?
Thanks a lot in advance!

Adrian Heine
  • 4,051
  • 2
  • 30
  • 43
Ema
  • 104
  • 11
  • I am not sure, but that might require having defensive copies of the objects, to save the states before edit. – Himanshu Bhardwaj Apr 09 '13 at 12:20
  • But one thing just clicked me, environment is heavily multi-threaded. So if you try to restore object to a previous state and while other thread is successfully changed a state without exception, what to do in that case? – Himanshu Bhardwaj Apr 09 '13 at 12:28

3 Answers3

4

You could have a look at the Memento Pattern:

The memento pattern is a software design pattern that provides the ability to restore an object to its previous state (undo via rollback).

pgras
  • 12,614
  • 4
  • 38
  • 46
2

Yes, such functionality is possible and often used in e.g. ORM frameworks or in servlet containers. ORM frameworks mostly offer some kind of entity cache, where pre-populated Java objects are stored. Modifications to these objects must be synchronized with the DB transaction. Some servlet containers also offer transactional access to the HTTP session. E.g. modifications during a request are applied to a copy of the session and only permanently applied if the request succeeds and otherwise discarded.

Generally, such functionality is based on making a copy of the state when starting the transaction, working on the copy during the transaction and replace the permanent state with the modified copy when committing the transaction, respectively discarding the copy if the transaction is rolled back. You may also need additional functionality to check for or even disallow concurrent modifications.

Since there is no "standard" way in Java to make an object copy, you have to do some work yourself and there are several possible approaches:

  • Implement Cloneable, but make sure that the clone method returns a deep copy of your object instead of the default shallow copy (outbound references are simply "moved" to the new object instead of making a real copy of the referenced object).
  • Use serialization. If your objects are serializable, you can serialize e.g. to a ByteArrayOutputStream and read the object back to obtain a copy.
  • Implement your own reflection based copy function.
jarnbjo
  • 33,923
  • 7
  • 70
  • 94
0

There is no built-in facility in Java to do this. So you have to do this manually. Mainly, this is about copying the affected objects states and restoring them back on rollback.

Related question:

Transaction for POJOs

Community
  • 1
  • 1
Vitaly
  • 2,760
  • 2
  • 19
  • 26