public void commitInOnePhase() throws StoreAccessException, IllegalStateException, TransactionTimeoutException { if (journal.isInDoubt(transactionId)) { throw new IllegalStateException("Cannot commit-one-phase transaction that has been prepared : " + transactionId); } int prepared = prepare(); if (prepared > 0) { commit(false); } }
commitContext.commit(transactionContext == null);
commitContext.commit(transactionContext == null);
@Test public void testCannotCommitUnknownXidInFlight() throws Exception { EhcacheXAResource<Long, String> xaResource = new EhcacheXAResource<>(underlyingStore, journal, xaTransactionContextFactory); when(journal.isInDoubt(eq(new TransactionId(new TestXid(0, 0))))).thenReturn(false); when(xaTransactionContextFactory.get(eq(new TransactionId(new TestXid(0, 0))))).thenReturn(xaTransactionContext); doThrow(IllegalArgumentException.class).when(xaTransactionContext).commit(eq(false)); try { xaResource.commit(new TestXid(0, 0), false); fail("expected XAException"); } catch (XAException xae) { assertThat(xae.errorCode, is(XAException.XAER_NOTA)); } }
@Test public void testCannotCommitNonPreparedXid() throws Exception { EhcacheXAResource<Long, String> xaResource = new EhcacheXAResource<>(underlyingStore, journal, xaTransactionContextFactory); when(xaTransactionContextFactory.get(eq(new TransactionId(new TestXid(0, 0))))).thenReturn(xaTransactionContext); doThrow(IllegalStateException.class).when(xaTransactionContext).commit(anyBoolean()); try { xaResource.commit(new TestXid(0, 0), false); fail("expected XAException"); } catch (XAException xae) { assertThat(xae.errorCode, is(XAException.XAER_PROTO)); } }
@Test @SuppressWarnings("unchecked") public void testCommitConflictsEvicts() throws Exception { XATransactionContext<Long, String> xaTransactionContext = getXaTransactionContext(); when(journal.isInDoubt(eq(new TransactionId(new TestXid(0, 0))))).thenReturn(true); when(journal.getInDoubtKeys(eq(new TransactionId(new TestXid(0, 0))))).thenReturn(Arrays.asList(1L, 2L)); when(underlyingStore.get(eq(1L))).thenReturn(new AbstractValueHolder<SoftLock<String>>(-1, -1) { @Override public SoftLock<String> get() { return new SoftLock<>(new TransactionId(new TestXid(0, 0)), "old1", new XAValueHolder<>("new1", timeSource .getTimeMillis())); } }); when(underlyingStore.get(eq(2L))).thenReturn(new AbstractValueHolder<SoftLock<String>>(-1, -1) { @Override public SoftLock<String> get() { return new SoftLock<>(new TransactionId(new TestXid(0, 0)), "old2", null); } }); when(underlyingStore.replace(any(Long.class), any(SoftLock.class), any(SoftLock.class))).thenReturn(ReplaceStatus.MISS_NOT_PRESENT); when(underlyingStore.remove(any(Long.class), any(SoftLock.class))).thenReturn(RemoveStatus.KEY_MISSING); xaTransactionContext.commit(false); verify(underlyingStore, times(1)).replace(eq(1L), eq(new SoftLock<>(new TransactionId(new TestXid(0, 0)), "old1", new XAValueHolder<>("new1", timeSource .getTimeMillis()))), eq(new SoftLock<>(null, "new1", null))); verify(underlyingStore, times(1)).remove(eq(1L)); verify(underlyingStore, times(1)).remove(eq(2L), eq(new SoftLock<>(new TransactionId(new TestXid(0, 0)), "old2", null))); verify(underlyingStore, times(1)).remove(eq(2L)); }
@Test public void testCommitNotPreparedInFlightThrows() throws Exception { XATransactionContext<Long, String> xaTransactionContext = getXaTransactionContext(); xaTransactionContext.addCommand(1L, new StorePutCommand<>("one", new XAValueHolder<>("un", timeSource.getTimeMillis()))); xaTransactionContext.addCommand(2L, new StorePutCommand<>("two", new XAValueHolder<>("deux", timeSource.getTimeMillis()))); @SuppressWarnings("unchecked") Store.ValueHolder<SoftLock<String>> mockValueHolder = mock(Store.ValueHolder.class); when(mockValueHolder.get()).thenReturn(new SoftLock<>(null, "two", null)); when(underlyingStore.get(eq(2L))).thenReturn(mockValueHolder); try { xaTransactionContext.commit(false); fail("expected IllegalArgumentException"); } catch (IllegalArgumentException ise) { // expected } }
when(underlyingStore.remove(any(Long.class), any(SoftLock.class))).thenReturn(RemoveStatus.KEY_MISSING); xaTransactionContext.commit(false); verify(journal, times(1)).saveCommitted(eq(new TransactionId(new TestXid(0, 0))), eq(false)); verify(journal, times(0)).saveRolledBack(eq(new TransactionId(new TestXid(0, 0))), anyBoolean());
public void commitInOnePhase() throws StoreAccessException, IllegalStateException, TransactionTimeoutException { if (journal.isInDoubt(transactionId)) { throw new IllegalStateException("Cannot commit-one-phase transaction that has been prepared : " + transactionId); } int prepared = prepare(); if (prepared > 0) { commit(false); } }