Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Question about declaring roll back method in non-database transaction

  Asked By: Joel    Date: Oct 07    Category: Java    Views: 1424

I have some problems about non-database transactional operation by using Spring Framework 2.0.x

I have a Queue Class that has two methods send and receive.

When you call the save method it puts it at the end of a LinkedList as well as save (insert/update) it

in the database.

It is evident that if you define this class transactional (by using @Transactional annotation or tx:advice tags) the insert/updating operation can be rollback successfully in the case of an error. But how I can reverse the LinkedList adding (I mean how the added object can be removed from LinkedList automatically when a rollback occurred)? I searched through the internet about defining some rollback methods and declaring them to the spring to call them when a rollback operation occurred, but unfortunately I could not find any solution.

Please Help me and show me a clear approach and example that solves it.



5 Answers Found

Answer #1    Answered By: Chaths Massri     Answered On: Oct 07

Apache common transactions has an implementation of transactional maps. Check if that puts you in any direction. It is also possible to configure that with spring transaction  management so that commits and rollbacks are respected. In general what you need is to have thread local variables contain modifications done in scope of a transaction and then you need to hook into transaction call  backs so that you get notified when is appropriate to put changes back  in the main stack or just discard them.

Answer #2    Answered By: Tarron Thompson     Answered On: Oct 07

We all know that (in JTA) two resources cannot participate in one
transaction unless they follow Open/XA distributed transaction  protocol
(or a simulation of it using algorithms such as LLR, LRC) which is a 2PC
negotiation between resource and TX manager.

A LinkedList is not an XA resource unless you yourself write a wrapper
around it. The good news is that such a wrapper is already developed and
is very familiar to all of us: JMS ;)

The other approach  is to forget about standards and write some callback
methods to receive  rollback/commit events and perform relate logic. This
is also supported in both Spring and EJB.

So, as I completely accept Farzad's comments I would like to proposed
these two alternatives too:

1. Put the list contents in a JMS and enjoy full XA support of it.
2. Write some methods  and bind them to TX events.

Considering fast and POJO based JMS implementation we already have, I
myself would prefer the first alternative.

Answer #3    Answered By: Vid Fischer     Answered On: Oct 07

Thanks for your help, I read a little about it but I should find  some samples to know how to use it specially in Spring framework. Mean while I don't know that Is it possible to have both this transaction  Manager and Hibernate or JTA Transaction Manger together? This is necessary for me to have database  rollback beside logical rollback.

Answer #4    Answered By: Daniel Costa     Answered On: Oct 07

JTA is OK. Full XA support in most of implementation.

Forget about Hibernate TM. All it has in common with a real transaction
manager is its name!

Answer #5    Answered By: Grace Ellis     Answered On: Oct 07

Consider what Mike says....