@Override public void commit(TransactionStatus status) { delegate.commit(status); }
/** * Immediately force a <em>commit</em> or <em>rollback</em> of the transaction for the * configured test context, according to the {@linkplain #isFlaggedForRollback rollback flag}. */ void endTransaction() { if (logger.isTraceEnabled()) { logger.trace(String.format( "Ending transaction for test context %s; transaction status [%s]; rollback [%s]", this.testContext, this.transactionStatus, this.flaggedForRollback)); } Assert.state(this.transactionStatus != null, () -> "Failed to end transaction - transaction does not exist: " + this.testContext); try { if (this.flaggedForRollback) { this.transactionManager.rollback(this.transactionStatus); } else { this.transactionManager.commit(this.transactionStatus); } } finally { this.transactionStatus = null; } if (logger.isInfoEnabled()) { logger.info((this.flaggedForRollback ? "Rolled back" : "Committed") + " transaction for test: " + this.testContext); } }
/** * Execute after successful completion of call, but not after an exception was handled. * Do nothing if we didn't create a transaction. * @param txInfo information about the current transaction */ protected void commitTransactionAfterReturning(@Nullable TransactionInfo txInfo) { if (txInfo != null && txInfo.getTransactionStatus() != null) { if (logger.isTraceEnabled()) { logger.trace("Completing transaction for [" + txInfo.getJoinpointIdentification() + "]"); } txInfo.getTransactionManager().commit(txInfo.getTransactionStatus()); } }
protected void endTransaction() { final boolean commit = this.complete; if (this.transactionStatus != null) { try { if (commit) { this.transactionManager.commit(this.transactionStatus); } else { this.transactionManager.rollback(this.transactionStatus); } } finally { this.transactionStatus = null; } } }
@Override @Nullable public <T> T execute(TransactionCallback<T> action) throws TransactionException { Assert.state(this.transactionManager != null, "No PlatformTransactionManager set"); if (this.transactionManager instanceof CallbackPreferringPlatformTransactionManager) { return ((CallbackPreferringPlatformTransactionManager) this.transactionManager).execute(this, action); } else { TransactionStatus status = this.transactionManager.getTransaction(this); T result; try { result = action.doInTransaction(status); } catch (RuntimeException | Error ex) { // Transactional code threw application exception -> rollback rollbackOnException(status, ex); throw ex; } catch (Throwable ex) { // Transactional code threw unexpected exception -> rollback rollbackOnException(status, ex); throw new UndeclaredThrowableException(ex, "TransactionCallback threw undeclared checked exception"); } this.transactionManager.commit(status); return result; } }
throw ex; this.transactionManager.commit(status); return messageReceived;
@Override public void commit() { //do noting if (transactionStatus != null) { if (rollback) { transactionTemplate.getTransactionManager().rollback(transactionStatus); } else { transactionTemplate.getTransactionManager().commit(transactionStatus); } } }
/** * Execute after successful completion of call, but not after an exception was handled. * Do nothing if we didn't create a transaction. * @param txInfo information about the current transaction */ protected void commitTransactionAfterReturning(@Nullable TransactionInfo txInfo) { if (txInfo != null && txInfo.getTransactionStatus() != null) { if (logger.isTraceEnabled()) { logger.trace("Completing transaction for [" + txInfo.getJoinpointIdentification() + "]"); } txInfo.getTransactionManager().commit(txInfo.getTransactionStatus()); } }
txInfo.getTransactionManager().commit(txInfo.getTransactionStatus());
@Test public void putTransactional() { Cache target = new ConcurrentMapCache("testCache"); Cache cache = new TransactionAwareCacheDecorator(target); TransactionStatus status = this.txManager.getTransaction( new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED)); Object key = new Object(); cache.put(key, "123"); assertNull(target.get(key)); this.txManager.commit(status); assertEquals("123", target.get(key, String.class)); }
@Test public void evictTransactional() { Cache target = new ConcurrentMapCache("testCache"); Cache cache = new TransactionAwareCacheDecorator(target); Object key = new Object(); cache.put(key, "123"); TransactionStatus status = this.txManager.getTransaction( new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED)); cache.evict(key); assertEquals("123", target.get(key, String.class)); this.txManager.commit(status); assertNull(target.get(key)); }
@Test public void clearTransactional() { Cache target = new ConcurrentMapCache("testCache"); Cache cache = new TransactionAwareCacheDecorator(target); Object key = new Object(); cache.put(key, "123"); TransactionStatus status = this.txManager.getTransaction( new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED)); cache.clear(); assertEquals("123", target.get(key, String.class)); this.txManager.commit(status); assertNull(target.get(key)); } }
/** * Test that TransactionStatus.setRollbackOnly works. */ @Test public void programmaticRollback() throws Exception { TransactionAttribute txatt = new DefaultTransactionAttribute(); Method m = getNameMethod; MapTransactionAttributeSource tas = new MapTransactionAttributeSource(); tas.register(m, txatt); TransactionStatus status = mock(TransactionStatus.class); PlatformTransactionManager ptm = mock(PlatformTransactionManager.class); given(ptm.getTransaction(txatt)).willReturn(status); final String name = "jenny"; TestBean tb = new TestBean() { @Override public String getName() { TransactionStatus txStatus = TransactionInterceptor.currentTransactionStatus(); txStatus.setRollbackOnly(); return name; } }; ITestBean itb = (ITestBean) advised(tb, ptm, tas); // verification!? assertTrue(name.equals(itb.getName())); verify(ptm).commit(status); }
/** * Check that a transaction is created and committed. */ @Test public void transactionShouldSucceed() throws Exception { TransactionAttribute txatt = new DefaultTransactionAttribute(); MapTransactionAttributeSource tas = new MapTransactionAttributeSource(); tas.register(getNameMethod, txatt); TransactionStatus status = mock(TransactionStatus.class); PlatformTransactionManager ptm = mock(PlatformTransactionManager.class); // expect a transaction given(ptm.getTransaction(txatt)).willReturn(status); TestBean tb = new TestBean(); ITestBean itb = (ITestBean) advised(tb, ptm, tas); checkTransactionStatus(false); itb.getName(); checkTransactionStatus(false); verify(ptm).commit(status); }
/** * Check that a transaction is created and committed. */ @Test public void transactionShouldSucceedWithNotNew() throws Exception { TransactionAttribute txatt = new DefaultTransactionAttribute(); MapTransactionAttributeSource tas = new MapTransactionAttributeSource(); tas.register(getNameMethod, txatt); TransactionStatus status = mock(TransactionStatus.class); PlatformTransactionManager ptm = mock(PlatformTransactionManager.class); // expect a transaction given(ptm.getTransaction(txatt)).willReturn(status); TestBean tb = new TestBean(); ITestBean itb = (ITestBean) advised(tb, ptm, tas); checkTransactionStatus(false); // verification!? itb.getName(); checkTransactionStatus(false); verify(ptm).commit(status); }
verify(ptm).commit(status);
/** * Check that two transactions are created and committed. */ @Test public void twoTransactionsShouldSucceed() throws Exception { TransactionAttribute txatt = new DefaultTransactionAttribute(); MapTransactionAttributeSource tas1 = new MapTransactionAttributeSource(); tas1.register(getNameMethod, txatt); MapTransactionAttributeSource tas2 = new MapTransactionAttributeSource(); tas2.register(setNameMethod, txatt); TransactionStatus status = mock(TransactionStatus.class); PlatformTransactionManager ptm = mock(PlatformTransactionManager.class); // expect a transaction given(ptm.getTransaction(txatt)).willReturn(status); TestBean tb = new TestBean(); ITestBean itb = (ITestBean) advised(tb, ptm, new TransactionAttributeSource[] {tas1, tas2}); checkTransactionStatus(false); itb.getName(); checkTransactionStatus(false); itb.setName("myName"); checkTransactionStatus(false); verify(ptm, times(2)).commit(status); }
given(ptm.getTransaction(txatt)).willReturn(status); UnexpectedRollbackException ex = new UnexpectedRollbackException("foobar", null); willThrow(ex).given(ptm).commit(status);