/** * Getter for the {@link TransactionAttribute} for subclasses only. * @return the transactionAttribute */ @SuppressWarnings("serial") protected TransactionAttribute getTransactionAttribute() { DefaultTransactionAttribute attribute = new DefaultTransactionAttribute(); attribute.setPropagationBehavior(propagation.value()); attribute.setIsolationLevel(isolation.value()); attribute.setTimeout(transactionTimeout); return new DefaultTransactionAttribute(attribute) { /** * Ignore the default behaviour and rollback on all exceptions that bubble up to the tasklet level. The * tasklet has to deal with the rollback rules internally. */ @Override public boolean rollbackOn(Throwable ex) { return true; } }; }
@Test public void testDefaultTransactionAttributeToString() { DefaultTransactionAttribute source = new DefaultTransactionAttribute(); source.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS); source.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ); source.setTimeout(10); source.setReadOnly(true); TransactionAttributeEditor pe = new TransactionAttributeEditor(); pe.setAsText(source.toString()); TransactionAttribute ta = (TransactionAttribute) pe.getValue(); assertEquals(ta, source); assertEquals(TransactionDefinition.PROPAGATION_SUPPORTS, ta.getPropagationBehavior()); assertEquals(TransactionDefinition.ISOLATION_REPEATABLE_READ, ta.getIsolationLevel()); assertEquals(10, ta.getTimeout()); assertTrue(ta.isReadOnly()); assertTrue(ta.rollbackOn(new RuntimeException())); assertFalse(ta.rollbackOn(new Exception())); source.setTimeout(9); assertNotSame(ta, source); source.setTimeout(10); assertEquals(ta, source); }
/** * Return an identifying description for this transaction attribute. * <p>Available to subclasses, for inclusion in their {@code toString()} result. */ protected final StringBuilder getAttributeDescription() { StringBuilder result = getDefinitionDescription(); if (StringUtils.hasText(this.qualifier)) { result.append("; '").append(this.qualifier).append("'"); } return result; }
@Test public void determineTransactionManagerWithEmptyQualifierAndDefaultName() { BeanFactory beanFactory = mock(BeanFactory.class); PlatformTransactionManager defaultTransactionManager = associateTransactionManager(beanFactory, "defaultTransactionManager"); TransactionInterceptor ti = transactionInterceptorWithTransactionManagerName( "defaultTransactionManager", beanFactory); DefaultTransactionAttribute attribute = new DefaultTransactionAttribute(); attribute.setQualifier(""); assertSame(defaultTransactionManager, ti.determineTransactionManager(attribute)); }
@Override protected boolean doIsProcessed(Message message) throws Exception { currentStatus.set(this.transactionManager.getTransaction(new DefaultTransactionAttribute())); return idempotentChecker.doIsProcessed(message); }
public JpaOutboundGateway save(JpaExecutor jpaExecutor, PlatformTransactionManager transactionManager) throws Exception { DefaultTransactionAttribute dta = new DefaultTransactionAttribute(); dta.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); dta.setReadOnly(false);
/** * Using the default transaction attribute for the job will cause the * TaskLauncher not to see the latest state in the database but rather * what is in its transaction. By setting isolation to READ_COMMITTED * The task launcher can see latest state of the db. Since the changes * to the task execution are done by the tasks. * @return DefaultTransactionAttribute with isolation set to READ_COMMITTED. */ private TransactionAttribute getTransactionAttribute() { DefaultTransactionAttribute defaultTransactionAttribute = new DefaultTransactionAttribute(); defaultTransactionAttribute.setIsolationLevel( Isolation.READ_COMMITTED.value()); return defaultTransactionAttribute; }
public TransactionAttribute getTransactionAttribute(Method method, Class targetClass) { try { Principal principal = current.getLast(); String uuid = principal.getName(); EventContext ec = cache.getSessionContext(uuid); Long shareId = ec.getCurrentShareId(); if (ec.getCurrentShareId() != null) { log.debug("Returning readOnly tx for shared " + shareId); DefaultTransactionAttribute ta = new DefaultTransactionAttribute(); ta.setReadOnly(true); return ta; } } catch (SessionException se) { // No worries. It's not our job to enforce anything. return null; } catch (NoSuchElementException nse) { // No one is logged in so there can't be a share active! return null; } return null; } }
public TransactionInterceptorBuilder propagation(Propagation propagation) { Assert.notNull(propagation, "'propagation' must not be null."); this.transactionAttribute.setPropagationBehavior(propagation.value()); return this; }
public TransactionInterceptorBuilder isolation(Isolation isolation) { Assert.notNull(isolation, "'isolation' must not be null."); this.transactionAttribute.setIsolationLevel(isolation.value()); return this; }
public TransactionInterceptorBuilder timeout(int timeout) { this.transactionAttribute.setTimeout(timeout); return this; }
public TransactionInterceptorBuilder readOnly(boolean readOnly) { this.transactionAttribute.setReadOnly(readOnly); return this; }
@Override public boolean rollbackOn(Throwable ex) { ApplicationException ann = ex.getClass().getAnnotation(ApplicationException.class); return (ann != null ? ann.rollback() : super.rollbackOn(ex)); } }
TransactionDefinition.PROPAGATION_REQUIRED); TransactionAttribute txAttr = TestContextTransactionUtils.createDelegatingTransactionAttribute( testContext, new DefaultTransactionAttribute(propagation)); new TransactionTemplate(txMgr, txAttr).execute(status -> { populator.execute(finalDataSource);
@Test public void determineTransactionManagerWithQualifierUnknown() { BeanFactory beanFactory = mock(BeanFactory.class); TransactionInterceptor ti = simpleTransactionInterceptor(beanFactory); DefaultTransactionAttribute attribute = new DefaultTransactionAttribute(); attribute.setQualifier("fooTransactionManager"); thrown.expect(NoSuchBeanDefinitionException.class); thrown.expectMessage("'fooTransactionManager'"); ti.determineTransactionManager(attribute); }
public TransactionInterceptorBuilder propagation(Propagation propagation) { Assert.notNull(propagation, "'propagation' must not be null."); this.transactionAttribute.setPropagationBehavior(propagation.value()); return this; }
public TransactionInterceptorBuilder isolation(Isolation isolation) { Assert.notNull(isolation, "'isolation' must not be null."); this.transactionAttribute.setIsolationLevel(isolation.value()); return this; }
public TransactionInterceptorBuilder timeout(int timeout) { this.transactionAttribute.setTimeout(timeout); return this; }
public TransactionInterceptorBuilder readOnly(boolean readOnly) { this.transactionAttribute.setReadOnly(readOnly); return this; }
return super.rollbackOn(ex);