Пример #1
0
  @Test
  public void shouldPropagateSystemExceptionFromBegin() throws Exception {
    // given
    AbstractTransactionManager txManager = mock(AbstractTransactionManager.class);
    when(txManager.suspend()).thenReturn(mock(javax.transaction.Transaction.class));
    SystemException exception = new SystemException();
    doThrow(exception).when(txManager).begin();

    @SuppressWarnings("unchecked")
    Transactor.Work<Object, KernelException> work = mock(Transactor.Work.class);

    Transactor transactor = new Transactor(txManager);

    // when
    try {
      transactor.execute(work);

      fail("expected exception");
    }
    // then
    catch (BeginTransactionFailureException e) {
      assertSame(exception, e.getCause());
    }
    verifyZeroInteractions(work);
    verify(txManager).suspend();
    verify(txManager).begin();
    verifyNoMoreInteractions(txManager);
  }
  @Test
  public void shouldThrowNotInTransactionExceptionWhenNotInTransaction() throws Exception {
    // Given
    AbstractTransactionManager txManager = mock(AbstractTransactionManager.class);
    when(txManager.getKernelTransaction()).thenReturn(null);
    ThreadToStatementContextBridge bridge = new ThreadToStatementContextBridge(null, txManager);

    // When
    try {
      bridge.statement();
      fail("Should throw");
    } catch (NotInTransactionException e) { // Good
    }
  }
Пример #3
0
 public ResourceHolder getResource() {
   TransactionState txState = transactionManager.getTransactionState();
   ResourceHolder resource = txState.getNeoStoreTransaction();
   if (resource == null) {
     txState.setNeoStoreTransaction(resource = createResource(getCurrentTransaction()));
   }
   return resource;
 }
Пример #4
0
  @Test
  public void shouldRollbackFailingStatement() throws Exception {
    // given
    AbstractTransactionManager txManager = mock(AbstractTransactionManager.class);

    javax.transaction.Transaction existingTransaction = mock(javax.transaction.Transaction.class);
    when(txManager.suspend()).thenReturn(existingTransaction);

    StatementOperationParts operations = mock(StatementOperationParts.class);
    KernelStatement statement = mock(KernelStatement.class);
    StubKernelTransaction kernelTransaction = spy(new StubKernelTransaction(operations, statement));
    when(txManager.getKernelTransaction()).thenReturn(kernelTransaction);

    @SuppressWarnings("unchecked")
    Transactor.Work<Object, KernelException> work = mock(Transactor.Work.class);
    SpecificKernelException exception = new SpecificKernelException();
    when(work.perform(any(StatementOperationParts.class), any(KernelStatement.class)))
        .thenThrow(exception);

    Transactor transactor = new Transactor(txManager);

    // when
    try {
      transactor.execute(work);

      fail("expected exception");
    }
    // then
    catch (SpecificKernelException e) {
      assertSame(exception, e);
    }
    InOrder order = inOrder(txManager, kernelTransaction, operations, statement, work);
    order.verify(txManager).suspend();
    order.verify(txManager).begin();
    order.verify(txManager).getKernelTransaction();
    order.verify(kernelTransaction).newStatement();
    order.verify(work).perform(operations, statement);
    order.verify(statement).close();
    order.verify(kernelTransaction).rollback();
    order.verify(txManager).resume(existingTransaction);
    order.verifyNoMoreInteractions();
  }
Пример #5
0
 public boolean hasCurrentTransaction() {
   try {
     if (null == transactionManager || null == transactionManager.getTransaction()) {
       return false;
     }
   } catch (SystemException se) {
     throw new TransactionFailureException(
         "Error fetching transaction " + "for current thread", se);
   }
   return true;
 }
Пример #6
0
 public Transaction getCurrentTransaction() throws NotInTransactionException {
   try {
     Transaction tx = transactionManager.getTransaction();
     if (tx == null) {
       throw new NotInTransactionException();
     }
     return tx;
   } catch (SystemException se) {
     throw new TransactionFailureException(
         "Error fetching transaction " + "for current thread", se);
   }
 }
Пример #7
0
  private ResourceHolder createResource(Transaction tx) {
    try {
      XaConnection xaConnection = persistenceSource.getXaDataSource().getXaConnection();
      NeoStoreTransaction resource = persistenceSource.createTransaction(xaConnection);
      ResourceHolder result = new ResourceHolder(tx, xaConnection, resource);

      TransactionState state = transactionManager.getTransactionState();
      tx.registerSynchronization(new ResourceCleanupHook(tx, state, result));
      return result;
    } catch (RollbackException | SystemException e) {
      throw new ResourceAcquisitionFailedException(e);
    }
  }
Пример #8
0
 void setRollbackOnly() {
   try {
     transactionManager.setRollbackOnly();
   } catch (IllegalStateException e) {
     // this exception always get generated in a finally block and
     // when it happens another exception has already been thrown
     // (most likley NotInTransactionException)
     logger.debug("Failed to set transaction rollback only", e);
   } catch (javax.transaction.SystemException se) {
     // our TM never throws this exception
     logger.error("Failed to set transaction rollback only", se);
   }
 }
Пример #9
0
  @Test
  public void shouldCommitSuccessfulStatement() throws Exception {
    // given
    AbstractTransactionManager txManager = mock(AbstractTransactionManager.class);

    javax.transaction.Transaction existingTransaction = mock(javax.transaction.Transaction.class);
    when(txManager.suspend()).thenReturn(existingTransaction);

    StatementOperationParts operations = mock(StatementOperationParts.class);
    KernelStatement statement = mock(KernelStatement.class);
    StubKernelTransaction kernelTransaction = spy(new StubKernelTransaction(operations, statement));
    when(txManager.getKernelTransaction()).thenReturn(kernelTransaction);

    @SuppressWarnings("unchecked")
    Transactor.Work<Object, KernelException> work = mock(Transactor.Work.class);
    Object expectedResult = new Object();
    when(work.perform(eq(operations), any(KernelStatement.class))).thenReturn(expectedResult);

    Transactor transactor = new Transactor(txManager);

    // when
    Object result = transactor.execute(work);

    // then
    assertEquals(expectedResult, result);
    InOrder order = inOrder(txManager, kernelTransaction, statement, work);
    order.verify(txManager).suspend();
    order.verify(txManager).begin();
    order.verify(txManager).getKernelTransaction();
    order.verify(kernelTransaction).newStatement();
    order.verify(work).perform(operations, statement);
    order.verify(statement).close();
    order.verify(kernelTransaction).commit();
    order.verify(txManager).resume(existingTransaction);
    order.verifyNoMoreInteractions();
  }
Пример #10
0
 private void assertInTransaction() {
   txManager.assertInTransaction();
 }
Пример #11
0
 TransactionState getTransactionState() {
   return transactionManager.getTransactionState();
 }