List of usage examples for org.springframework.transaction.interceptor TransactionAttribute rollbackOn
boolean rollbackOn(Throwable ex);
From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRollbackTests.java
@Test public void testOverrideWithoutChangingRollbackRules() throws Exception { TransactionAttributeEditor editor = new TransactionAttributeEditor(); editor.setAsText("-RuntimeException"); TransactionAttribute attr = (TransactionAttribute) editor.getValue(); assertTrue(attr.rollbackOn(new RuntimeException(""))); assertFalse(attr.rollbackOn(new Exception(""))); }
From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRollbackTests.java
@Test public void testChangeRollbackRules() throws Exception { TransactionAttributeEditor editor = new TransactionAttributeEditor(); editor.setAsText("+RuntimeException"); TransactionAttribute attr = (TransactionAttribute) editor.getValue(); assertFalse(attr.rollbackOn(new RuntimeException(""))); assertFalse(attr.rollbackOn(new Exception(""))); }
From source file:org.springframework.transaction.interceptor.TransactionAspectSupport.java
/** * General delegate for around-advice-based subclasses, delegating to several other template * methods on this class. Able to handle {@link CallbackPreferringPlatformTransactionManager} * as well as regular {@link PlatformTransactionManager} implementations. * @param method the Method being invoked * @param targetClass the target class that we're invoking the method on * @param invocation the callback to use for proceeding with the target invocation * @return the return value of the method, if any * @throws Throwable propagated from the target invocation *//*from w w w.j a va 2s .c om*/ @Nullable protected Object invokeWithinTransaction(Method method, @Nullable Class<?> targetClass, final InvocationCallback invocation) throws Throwable { // If the transaction attribute is null, the method is non-transactional. TransactionAttributeSource tas = getTransactionAttributeSource(); final TransactionAttribute txAttr = (tas != null ? tas.getTransactionAttribute(method, targetClass) : null); final PlatformTransactionManager tm = determineTransactionManager(txAttr); final String joinpointIdentification = methodIdentification(method, targetClass, txAttr); if (txAttr == null || !(tm instanceof CallbackPreferringPlatformTransactionManager)) { // Standard transaction demarcation with getTransaction and commit/rollback calls. TransactionInfo txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification); Object retVal = null; try { // This is an around advice: Invoke the next interceptor in the chain. // This will normally result in a target object being invoked. retVal = invocation.proceedWithInvocation(); } catch (Throwable ex) { // target invocation exception completeTransactionAfterThrowing(txInfo, ex); throw ex; } finally { cleanupTransactionInfo(txInfo); } commitTransactionAfterReturning(txInfo); return retVal; } else { final ThrowableHolder throwableHolder = new ThrowableHolder(); // It's a CallbackPreferringPlatformTransactionManager: pass a TransactionCallback in. try { Object result = ((CallbackPreferringPlatformTransactionManager) tm).execute(txAttr, status -> { TransactionInfo txInfo = prepareTransactionInfo(tm, txAttr, joinpointIdentification, status); try { return invocation.proceedWithInvocation(); } catch (Throwable ex) { if (txAttr.rollbackOn(ex)) { // A RuntimeException: will lead to a rollback. if (ex instanceof RuntimeException) { throw (RuntimeException) ex; } else { throw new ThrowableHolderException(ex); } } else { // A normal return value: will lead to a commit. throwableHolder.throwable = ex; return null; } } finally { cleanupTransactionInfo(txInfo); } }); // Check result state: It might indicate a Throwable to rethrow. if (throwableHolder.throwable != null) { throw throwableHolder.throwable; } return result; } catch (ThrowableHolderException ex) { throw ex.getCause(); } catch (TransactionSystemException ex2) { if (throwableHolder.throwable != null) { logger.error("Application exception overridden by commit exception", throwableHolder.throwable); ex2.initApplicationException(throwableHolder.throwable); } throw ex2; } catch (Throwable ex2) { if (throwableHolder.throwable != null) { logger.error("Application exception overridden by commit exception", throwableHolder.throwable); } throw ex2; } } }
From source file:org.springframework.transaction.interceptor.TransactionInterceptor.java
/** * Handle a throwable./* ww w . j a va2 s . com*/ * We may commit or roll back, depending on our configuration. */ private void onThrowable(MethodInvocation invocation, TransactionAttribute txAtt, TransactionStatus status, Throwable ex) { if (txAtt.rollbackOn(ex)) { if (logger.isInfoEnabled()) { logger.info("Invoking rollback for transaction on method '" + invocation.getMethod().getName() + "' in class [" + invocation.getMethod().getDeclaringClass().getName() + "] due to throwable [" + ex + "]"); } try { this.transactionManager.rollback(status); } catch (TransactionException tex) { logger.error("Application exception overridden by rollback exception", ex); throw tex; } } else { if (logger.isDebugEnabled()) { logger.debug("Method '" + invocation.getMethod().getName() + "' in class [" + invocation.getMethod().getDeclaringClass().getName() + "] threw throwable [" + ex + "] but this does not force transaction rollback"); } // will still roll back if rollbackOnly is true this.transactionManager.commit(status); } }