-
1. Re: how app thread should clear tx with status STATUS_ROLLEDBACK
tomjenkinson May 23, 2013 10:47 AM (in response to smarlow)tm.suspend does what you need, it is safe too as all it can do is return null if there is no transaction associated with the thread or the transaction object if there is one.
tm.suspend would work with Narayana as it is a JTA spec thing.
Hope that helps,
Tom
-
2. Re: how app thread should clear tx with status STATUS_ROLLEDBACK
jesper.pedersen May 23, 2013 11:01 AM (in response to smarlow)No, I said that JTA 1.1 section 3.2.2 states that rollback() disassociates the transaction with the current thread. Hence having a status of ROLLEDBACK is a getStatus() on a cached transaction handle in your code.
As Tom says tm.suspend() does a disassociate too, with a possible null return value - so that will work.
However, that isn't the point - you shouldn't cache the transaction handle in the first place. And you need to look into why there is a transaction still associated with the thread.
-
3. Re: how app thread should clear tx with status STATUS_ROLLEDBACK
smarlow May 23, 2013 11:10 AM (in response to jesper.pedersen)However, that isn't the point - you shouldn't cache the transaction handle in the first place. And you need to look into why there is a transaction still associated with the thread.
Jesper, are you saying that for Narayana, when the reaper thread cancels the transaction, that the application thread should avoid keeping a strong reference to the canceled tx because that is the only thing keeping it in the application thread memory? Currently, I see tm.getTransaction() in the application thread, returning the canceled transaction. We also have the same tx as a local parameter, which we use to check if it matches what tm.getTransaction() returns.
-
4. Re: how app thread should clear tx with status STATUS_ROLLEDBACK
jhalliday May 23, 2013 11:13 AM (in response to jesper.pedersen)tm.rollback() disassociates per the spec. tx.rollback() is not required to and doesn't.
-
5. Re: how app thread should clear tx with status STATUS_ROLLEDBACK
smarlow May 23, 2013 11:14 AM (in response to jesper.pedersen)As Tom says tm.suspend() does a disassociate too, with a possible null return value - so that will work.
For an already canceled transaction (e.g. background reaper thread canceled it for my current case), why would tm.suspend() do a disassociate also when tx.status == Status.STATUS_ROLLEDBACK?
-
6. Re: how app thread should clear tx with status STATUS_ROLLEDBACK
jesper.pedersen May 23, 2013 11:16 AM (in response to smarlow)I wouldn't cache the transaction handle - if you need to check if a transaction has changed I would use a identity hash (System.identityHashCode(tx)).
However, if the reaper canceled the transaction then tm.getTransaction() should return null according to 3.2.2.
-
7. Re: how app thread should clear tx with status STATUS_ROLLEDBACK
jhalliday May 23, 2013 11:20 AM (in response to jesper.pedersen)> However, if the reaper canceled the transaction then tm.getTransaction() should return null according to 3.2.2.
Nonsense. If the tx is rolledbackvia tm.rollback() then yes. Otherwise it's outside the scope of that section. The reaper does not use tm.rollback, because the tx is not associated to the reaper thread. It uses basically the internal equivalent of tx.rollback() - which takes us back to my prior post.
-
8. Re: how app thread should clear tx with status STATUS_ROLLEDBACK
jesper.pedersen May 23, 2013 11:24 AM (in response to jhalliday)k, I was assuming that the reaper was using the same call path as tm.rollback().
So there are two cases then, reaper => tm.getTransaction() != null, and tm.rollback() => tm.getTransaction() == null. So the tm.suspend() call is needed if != null && ROLLBACKED.
-
9. Re: how app thread should clear tx with status STATUS_ROLLEDBACK
jhalliday May 23, 2013 11:33 AM (in response to jesper.pedersen)the requirement to keep the tx context on the thread until explicitly removed actually stems from the way the JCA works
begin();
// the following sql is under the control of the JTA tx commit
doSomeSQL();
// the following sql will autocommit independent of the jta tx, which is probably not what the user wants
doSomeMoreSQL();
commit();
why? because the tx timed out and rolled back between the two SQL statements, causing the latter to revert to the connection's default auto-commit mode. This is a Bad Thing. Hence we don't work that way - the rolled back tx sticks around and causes the second SQL statement to fail, hopefully dropping us into an exception handler that calls rollback or suspend. Having to clean up the context by hand is a small price to pay to being able to write code that will behave safely. On the other hand it causes hibernate lazy loading to fail too, which causes no end of fun. You just can't win.
-
10. Re: how app thread should clear tx with status STATUS_ROLLEDBACK
jesper.pedersen May 23, 2013 11:35 AM (in response to jhalliday)This is an EJB3 case
-
11. Re: how app thread should clear tx with status STATUS_ROLLEDBACK
tomjenkinson Feb 15, 2016 6:40 AM (in response to smarlow)Discussion is followed up over here: how EJB3 CMT should handle different tx status flags