public int getStatus() { Thread thread = Thread.currentThread(); TransactionImpl tx = txThreadMap.get(thread); if (tx != null) { return tx.getStatus(); } return Status.STATUS_NO_TRANSACTION; }
public void resume(Transaction tx) throws IllegalStateException, SystemException { assertTmOk("tx resume"); Thread thread = Thread.currentThread(); if (txThreadMap.get(thread) != null) { throw new IllegalStateException("Transaction already associated"); } if (tx != null) { TransactionImpl txImpl = (TransactionImpl) tx; if (txImpl.getStatus() != Status.STATUS_NO_TRANSACTION) { if (txImpl.isActive()) { throw new IllegalStateException(txImpl + " already active"); } txImpl.markAsActive(); txThreadMap.put(thread, txImpl); } // generate pro-active event resume } }
@Override public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, IllegalStateException, SystemException { assertTmOk("tx commit"); Thread thread = Thread.currentThread(); TransactionImpl tx = txThreadMap.get(thread); if (tx == null) { throw logAndReturn("TM error tx commit", new IllegalStateException("Not in transaction")); } boolean hasAnyLocks = false; boolean successful = false; try { hasAnyLocks = finishHook.hasAnyLocks(tx); if (tx.getStatus() != Status.STATUS_ACTIVE && tx.getStatus() != Status.STATUS_MARKED_ROLLBACK) { throw logAndReturn( "TM error tx commit", new IllegalStateException("Tx status is: " + getTxStatusAsString(tx.getStatus()))); } tx.doBeforeCompletion(); // delist resources? if (tx.getStatus() == Status.STATUS_ACTIVE) { comittedTxCount.incrementAndGet(); commit(thread, tx); } else if (tx.getStatus() == Status.STATUS_MARKED_ROLLBACK) { rolledBackTxCount.incrementAndGet(); rollbackCommit(thread, tx); } else { throw logAndReturn( "TM error tx commit", new IllegalStateException("Tx status is: " + getTxStatusAsString(tx.getStatus()))); } successful = true; } finally { if (hasAnyLocks) { finishHook.finishTransaction(tx.getEventIdentifier(), successful); } } }
public void rollback() throws IllegalStateException, SystemException { assertTmOk("tx rollback"); Thread thread = Thread.currentThread(); TransactionImpl tx = txThreadMap.get(thread); if (tx == null) { throw new IllegalStateException("Not in transaction"); } boolean hasAnyLocks = false; try { hasAnyLocks = finishHook.hasAnyLocks(tx); if (tx.getStatus() == Status.STATUS_ACTIVE || tx.getStatus() == Status.STATUS_MARKED_ROLLBACK || tx.getStatus() == Status.STATUS_PREPARING) { tx.setStatus(Status.STATUS_MARKED_ROLLBACK); tx.doBeforeCompletion(); // delist resources? try { rolledBackTxCount.incrementAndGet(); tx.doRollback(); } catch (XAException e) { log.log( Level.SEVERE, "Unable to rollback marked or active transaction. " + "Some resources may be commited others not. " + "Neo4j kernel should be SHUTDOWN for " + "resource maintance and transaction recovery ---->", e); setTmNotOk(e); throw logAndReturn( "TM error tx rollback", Exceptions.withCause( new SystemException( "Unable to rollback " + " ---> error code for rollback: " + e.errorCode), e)); } tx.doAfterCompletion(); txThreadMap.remove(thread); try { if (tx.isGlobalStartRecordWritten()) { getTxLog().txDone(tx.getGlobalId()); } } catch (IOException e) { log.log(Level.SEVERE, "Error writing transaction log", e); setTmNotOk(e); throw logAndReturn( "TM error tx rollback", Exceptions.withCause( new SystemException( "TM encountered a problem, " + " error writing transaction log"), e)); } tx.setStatus(Status.STATUS_NO_TRANSACTION); } else { throw new IllegalStateException("Tx status is: " + getTxStatusAsString(tx.getStatus())); } } finally { if (hasAnyLocks) { finishHook.finishTransaction(tx.getEventIdentifier(), false); } } }
private void commit(Thread thread, TransactionImpl tx) throws SystemException, HeuristicMixedException, HeuristicRollbackException { // mark as commit in log done TxImpl.doCommit() Throwable commitFailureCause = null; int xaErrorCode = -1; if (tx.getResourceCount() == 0) { tx.setStatus(Status.STATUS_COMMITTED); } else { try { tx.doCommit(); } catch (XAException e) { xaErrorCode = e.errorCode; log.log( Level.SEVERE, "Commit failed, status=" + getTxStatusAsString(tx.getStatus()) + ", errorCode=" + xaErrorCode, e); if (tx.getStatus() == Status.STATUS_COMMITTED) { // this should never be setTmNotOk(e); throw logAndReturn( "TM error tx commit", new TransactionFailureException( "commit threw exception but status is committed?", e)); } } catch (Throwable t) { log.log(Level.SEVERE, "Commit failed", t); commitFailureCause = t; } } if (tx.getStatus() != Status.STATUS_COMMITTED) { try { tx.doRollback(); } catch (Throwable e) { log.log( Level.SEVERE, "Unable to rollback transaction. " + "Some resources may be commited others not. " + "Neo4j kernel should be SHUTDOWN for " + "resource maintance and transaction recovery ---->", e); setTmNotOk(e); String commitError; if (commitFailureCause != null) { commitError = "error in commit: " + commitFailureCause; } else { commitError = "error code in commit: " + xaErrorCode; } String rollbackErrorCode = "Uknown error code"; if (e instanceof XAException) { rollbackErrorCode = Integer.toString(((XAException) e).errorCode); } throw logAndReturn( "TM error tx commit", Exceptions.withCause( new HeuristicMixedException( "Unable to rollback ---> " + commitError + " ---> error code for rollback: " + rollbackErrorCode), e)); } tx.doAfterCompletion(); txThreadMap.remove(thread); try { if (tx.isGlobalStartRecordWritten()) { getTxLog().txDone(tx.getGlobalId()); } } catch (IOException e) { log.log(Level.SEVERE, "Error writing transaction log", e); setTmNotOk(e); throw logAndReturn( "TM error tx commit", Exceptions.withCause( new SystemException( "TM encountered a problem, " + " error writing transaction log"), e)); } tx.setStatus(Status.STATUS_NO_TRANSACTION); if (commitFailureCause == null) { throw logAndReturn( "TM error tx commit", new HeuristicRollbackException( "Failed to commit, transaction rolledback ---> " + "error code was: " + xaErrorCode)); } else { throw logAndReturn( "TM error tx commit", Exceptions.withCause( new HeuristicRollbackException( "Failed to commit, transaction rolledback ---> " + commitFailureCause), commitFailureCause)); } } tx.doAfterCompletion(); txThreadMap.remove(thread); try { if (tx.isGlobalStartRecordWritten()) { getTxLog().txDone(tx.getGlobalId()); } } catch (IOException e) { log.log(Level.SEVERE, "Error writing transaction log", e); setTmNotOk(e); throw logAndReturn( "TM error tx commit", Exceptions.withCause( new SystemException("TM encountered a problem, " + " error writing transaction log"), e)); } tx.setStatus(Status.STATUS_NO_TRANSACTION); }