private static CascadingTx createTransaction(final Object obj) throws RecoverablePersistenceException, RuntimeException { final String ctx = lookatPersistenceContext(obj); final CascadingTx ret = new CascadingTx(ctx); try { ret.begin(); if (txRootThreadLocal.get() == null) { final String txId = makeTxRootName(ret); LOG.trace( "Creating root entry for transaction tree: " + txId + " at: \n" + Threads.currentStackString()); txRootThreadLocal.set(txId); } txStateThreadLocal.get().put(ctx, ret); return ret; } catch (RuntimeException ex) { try { ret.rollback(); } catch (RuntimeException ex1) { throw ex1; } throw ex; } }
public static EntityTransaction get(final Object obj) { if (hasTransaction(obj)) { final CascadingTx tx = getTransaction(obj); final EntityTransaction etx = tx.join(); return etx; } else { return createTransaction(obj); } }
private static void cleanStrandedTx(final CascadingTx txValue) { LOG.error( "Found stranded transaction: " + txValue.getRecord().getPersistenceContext() + " started at: " + txValue.getRecord().getStack()); try { txValue.rollback(); } catch (final Exception ex) { LOG.trace(ex, ex); } }
public static void removeTransaction(final CascadingTx tx) { final String txId = makeTxRootName(tx); txLog.remove(txStateThreadLocal.toString() + tx.getRecord().getPersistenceContext()); txStateThreadLocal.get().remove(tx.getRecord().getPersistenceContext()); if (txId.equals(txStateThreadLocal.get())) { for (final Entry<String, CascadingTx> e : txStateThreadLocal.get().entrySet()) { cleanStrandedTx(e.getValue()); } txStateThreadLocal.get().clear(); txStateThreadLocal.remove(); } }
public static boolean hasTransaction(final Object obj) { final String ctx = lookatPersistenceContext(obj); final CascadingTx tx = txStateThreadLocal.get().get(ctx); if (tx == null) { return false; } else if (tx.isActive()) { return true; } else { cleanStrandedTx(tx); return false; } }
private boolean clearStale(final PersistenceContext persistenceContext) { final CascadingTx tx = this.get().get(persistenceContext.name()); if (!tx.isActive()) { try { tx.getTxState().doCleanup(); } catch (final Exception ex) { LOG.error(ex, ex); } finally { this.get().remove(persistenceContext.name()); } return true; } else { return false; } }
private static String makeTxRootName(final CascadingTx tx) { return txStateThreadLocal.toString() + tx.getRecord().getPersistenceContext(); }