Java Code Examples for org.springframework.transaction.TransactionDefinition#PROPAGATION_REQUIRED

The following examples show how to use org.springframework.transaction.TransactionDefinition#PROPAGATION_REQUIRED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Neo4jTransactionUtils.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
/**
 * Maps a Spring {@link TransactionDefinition transaction definition} to a native Neo4j driver transaction.
 * Only the default isolation leven ({@link TransactionDefinition#ISOLATION_DEFAULT}) and
 * {@link TransactionDefinition#PROPAGATION_REQUIRED propagation required} behaviour are supported.
 *
 * @param definition The transaction definition passed to a Neo4j transaction manager
 * @return A Neo4j native transaction configuration
 */
static TransactionConfig createTransactionConfigFrom(TransactionDefinition definition) {

	if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
		throw new InvalidIsolationLevelException(
			"Neo4jTransactionManager is not allowed to support custom isolation levels.");
	}

	int propagationBehavior = definition.getPropagationBehavior();
	if (!(propagationBehavior == TransactionDefinition.PROPAGATION_REQUIRED || propagationBehavior == TransactionDefinition.PROPAGATION_REQUIRES_NEW)) {
		throw new IllegalTransactionStateException("Neo4jTransactionManager only supports 'required' or 'requires new' propagation.");
	}

	TransactionConfig.Builder builder = TransactionConfig.builder();
	if (definition.getTimeout() > 0) {
		builder = builder.withTimeout(Duration.ofSeconds(definition.getTimeout()));
	}

	return builder.build();
}
 
Example 2
Source File: RuleBasedTransactionAttributeTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testToStringMatchesEditor() {
	List<RollbackRuleAttribute> list = new LinkedList<>();
	list.add(new NoRollbackRuleAttribute("Throwable"));
	RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, list);

	TransactionAttributeEditor tae = new TransactionAttributeEditor();
	tae.setAsText(rta.toString());
	rta = (RuleBasedTransactionAttribute) tae.getValue();

	assertFalse(rta.rollbackOn(new Throwable()));
	assertFalse(rta.rollbackOn(new RuntimeException()));
	assertFalse(rta.rollbackOn(new MyRuntimeException("")));
	assertFalse(rta.rollbackOn(new Exception()));
	assertFalse(rta.rollbackOn(new IOException()));
}
 
Example 3
Source File: MicroServiceTemplateSupport.java    From nh-micro with Apache License 2.0 6 votes vote down vote up
public Object execGroovyRetObjByDbTranNest(String groovyName, String methodName, Integer nestDef,
			Object... paramArray) throws Exception{
/*		MicroMetaDao microDao=MicroMetaDao.getInstance(dbName,dbType);
		DataSource dataSource=microDao.getMicroDataSource();
		PlatformTransactionManager  transactionManager=new DataSourceTransactionManager(dataSource);*/
		PlatformTransactionManager  transactionManager=MicroTranManagerHolder.getTransactionManager(dbName);
	    DefaultTransactionDefinition def =new DefaultTransactionDefinition();
	    def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
	    if(nestDef==null){
	    	nestDef=TransactionDefinition.PROPAGATION_REQUIRED;
	    }
	    def.setPropagationBehavior(nestDef);
	    TransactionStatus status=transactionManager.getTransaction(def);
	    try
	    {
	    	Object retObj= GroovyExecUtil.execGroovyRetObj(groovyName, methodName, paramArray);
	    	transactionManager.commit(status);
	    	return retObj;
	    }
	    catch(Exception ex)
	    {
	    	transactionManager.rollback(status);
	        throw ex;
	    }
		
	}
 
Example 4
Source File: RuleBasedTransactionAttributeTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testToStringMatchesEditor() {
	List<RollbackRuleAttribute> list = new LinkedList<>();
	list.add(new NoRollbackRuleAttribute("Throwable"));
	RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, list);

	TransactionAttributeEditor tae = new TransactionAttributeEditor();
	tae.setAsText(rta.toString());
	rta = (RuleBasedTransactionAttribute) tae.getValue();

	assertFalse(rta.rollbackOn(new Throwable()));
	assertFalse(rta.rollbackOn(new RuntimeException()));
	assertFalse(rta.rollbackOn(new MyRuntimeException("")));
	assertFalse(rta.rollbackOn(new Exception()));
	assertFalse(rta.rollbackOn(new IOException()));
}
 
Example 5
Source File: RuleBasedTransactionAttributeTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Check that a rule can cause commit on a IOException
 * when Exception prompts a rollback.
 */
@Test
public void testRuleForCommitOnSubclassOfChecked() {
	List<RollbackRuleAttribute> list = new LinkedList<>();
	// Note that it's important to ensure that we have this as
	// a FQN: otherwise it will match everything!
	list.add(new RollbackRuleAttribute("java.lang.Exception"));
	list.add(new NoRollbackRuleAttribute("IOException"));
	RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, list);

	assertTrue(rta.rollbackOn(new RuntimeException()));
	assertTrue(rta.rollbackOn(new Exception()));
	// Check that default behaviour is overridden
	assertFalse(rta.rollbackOn(new IOException()));
}
 
Example 6
Source File: RuleBasedTransactionAttributeTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Check that a rule can cause commit on a IOException
 * when Exception prompts a rollback.
 */
@Test
public void testRuleForCommitOnSubclassOfChecked() {
	List<RollbackRuleAttribute> list = new LinkedList<RollbackRuleAttribute>();
	// Note that it's important to ensure that we have this as
	// a FQN: otherwise it will match everything!
	list.add(new RollbackRuleAttribute("java.lang.Exception"));
	list.add(new NoRollbackRuleAttribute("IOException"));
	RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, list);

	assertTrue(rta.rollbackOn(new RuntimeException()));
	assertTrue(rta.rollbackOn(new Exception()));
	// Check that default behaviour is overridden
	assertFalse(rta.rollbackOn(new IOException()));
}
 
Example 7
Source File: RuleBasedTransactionAttributeTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testRuleForSelectiveRollbackOnCheckedWithString() {
	List<RollbackRuleAttribute> l = new LinkedList<>();
	l.add(new RollbackRuleAttribute(java.rmi.RemoteException.class.getName()));
	RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, l);
	doTestRuleForSelectiveRollbackOnChecked(rta);
}
 
Example 8
Source File: RuleBasedTransactionAttributeTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testRuleForCommitOnUnchecked() {
	List<RollbackRuleAttribute> list = new LinkedList<>();
	list.add(new NoRollbackRuleAttribute(MyRuntimeException.class.getName()));
	list.add(new RollbackRuleAttribute(IOException.class.getName()));
	RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, list);

	assertTrue(rta.rollbackOn(new RuntimeException()));
	// Check default behaviour is overridden
	assertFalse(rta.rollbackOn(new MyRuntimeException("")));
	assertFalse(rta.rollbackOn(new Exception()));
	// Check that default behaviour is overridden
	assertTrue(rta.rollbackOn(new IOException()));
}
 
Example 9
Source File: RuleBasedTransactionAttributeTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Test one checked exception that should roll back.
 */
@Test
public void testRuleForRollbackOnChecked() {
	List<RollbackRuleAttribute> list = new LinkedList<>();
	list.add(new RollbackRuleAttribute(IOException.class.getName()));
	RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, list);

	assertTrue(rta.rollbackOn(new RuntimeException()));
	assertTrue(rta.rollbackOn(new MyRuntimeException("")));
	assertFalse(rta.rollbackOn(new Exception()));
	// Check that default behaviour is overridden
	assertTrue(rta.rollbackOn(new IOException()));
}
 
Example 10
Source File: SpringTransactionManager.java    From dalesbred with MIT License 5 votes vote down vote up
static int springPropagationCode(@NotNull Propagation propagation) {
    switch (propagation) {
        case REQUIRED:
            return TransactionDefinition.PROPAGATION_REQUIRED;
        case MANDATORY:
            return TransactionDefinition.PROPAGATION_MANDATORY;
        case NESTED:
            return TransactionDefinition.PROPAGATION_NESTED;
        case REQUIRES_NEW:
            return TransactionDefinition.PROPAGATION_REQUIRES_NEW;
    }
    throw new IllegalArgumentException("unknown propagation: " + propagation);
}
 
Example 11
Source File: RuleBasedTransactionAttributeTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * See <a href="https://forum.springframework.org/showthread.php?t=41350">this forum post</a>.
 */
@Test
public void testConflictingRulesToDetermineExactContract() {
	List<RollbackRuleAttribute> list = new LinkedList<>();
	list.add(new NoRollbackRuleAttribute(MyBusinessWarningException.class));
	list.add(new RollbackRuleAttribute(MyBusinessException.class));
	RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, list);

	assertTrue(rta.rollbackOn(new MyBusinessException()));
	assertFalse(rta.rollbackOn(new MyBusinessWarningException()));
}
 
Example 12
Source File: SpringAwareUserTransactionTest.java    From alfresco-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
private UserTransaction getFailingTxn()
{
    return new SpringAwareUserTransaction(
            failingTransactionManager,
            false,
            TransactionDefinition.ISOLATION_DEFAULT,
            TransactionDefinition.PROPAGATION_REQUIRED,
            TransactionDefinition.TIMEOUT_DEFAULT);
}
 
Example 13
Source File: RuleBasedTransactionAttributeTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Check that a rule can cause commit on a IOException
 * when Exception prompts a rollback.
 */
@Test
public void testRuleForCommitOnSubclassOfChecked() {
	List<RollbackRuleAttribute> list = new LinkedList<>();
	// Note that it's important to ensure that we have this as
	// a FQN: otherwise it will match everything!
	list.add(new RollbackRuleAttribute("java.lang.Exception"));
	list.add(new NoRollbackRuleAttribute("IOException"));
	RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, list);

	assertTrue(rta.rollbackOn(new RuntimeException()));
	assertTrue(rta.rollbackOn(new Exception()));
	// Check that default behaviour is overridden
	assertFalse(rta.rollbackOn(new IOException()));
}
 
Example 14
Source File: PipeLine.java    From iaf with Apache License 2.0 5 votes vote down vote up
public boolean isTransacted() {
//		return transacted;
		int txAtt = getTransactionAttributeNum();
		return  txAtt==TransactionDefinition.PROPAGATION_REQUIRED || 
				txAtt==TransactionDefinition.PROPAGATION_REQUIRES_NEW ||
				txAtt==TransactionDefinition.PROPAGATION_MANDATORY;
	}
 
Example 15
Source File: RuleBasedTransactionAttributeTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Test one checked exception that should roll back.
 */
@Test
public void testRuleForRollbackOnChecked() {
	List<RollbackRuleAttribute> list = new LinkedList<RollbackRuleAttribute>();
	list.add(new RollbackRuleAttribute(IOException.class.getName()));
	RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, list);

	assertTrue(rta.rollbackOn(new RuntimeException()));
	assertTrue(rta.rollbackOn(new MyRuntimeException("")));
	assertFalse(rta.rollbackOn(new Exception()));
	// Check that default behaviour is overridden
	assertTrue(rta.rollbackOn(new IOException()));
}
 
Example 16
Source File: RuleBasedTransactionAttributeTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testRuleForSelectiveRollbackOnCheckedWithClass() {
	List<RollbackRuleAttribute> l = Collections.singletonList(new RollbackRuleAttribute(RemoteException.class));
	RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, l);
	doTestRuleForSelectiveRollbackOnChecked(rta);
}
 
Example 17
Source File: TransactionConfig.java    From scaffold-cloud with MIT License 4 votes vote down vote up
@Bean("txSource")
public TransactionAttributeSource transactionAttributeSource() {
    NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
    // 只读事务,不做更新操作
    RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute();
    readOnlyTx.setReadOnly(true);
    readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
    // 默认级别 如果有事务 就加入当前事务 没有就新建
    RuleBasedTransactionAttribute requiredTx =
            new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED,
            Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
    // 事务新建 不管有没有事务 都新建事务再执行
    RuleBasedTransactionAttribute requiredTxNew =
            new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRES_NEW,
            Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
    requiredTx.setTimeout(5);
    Map<String, TransactionAttribute> txMap = new HashMap<>();
    txMap.put("add*", requiredTx);
    txMap.put("create*", requiredTx);
    txMap.put("insert*", requiredTx);
    txMap.put("save*", requiredTx);
    txMap.put("update*", requiredTx);
    txMap.put("modify*", requiredTx);
    txMap.put("edit*", requiredTx);
    txMap.put("merge*", requiredTx);
    txMap.put("delete*", requiredTx);
    txMap.put("remove*", requiredTx);
    txMap.put("do*", requiredTx);
    txMap.put("handle*", requiredTx);
    // 以下为只读事务 PROPAGATION_NOT_SUPPORTED级别为不开启事务
    txMap.put("get*", readOnlyTx);
    txMap.put("query*", readOnlyTx);
    txMap.put("count*", readOnlyTx);
    txMap.put("find*", readOnlyTx);
    txMap.put("list*", readOnlyTx);
    txMap.put("load*", readOnlyTx);
    txMap.put("search*", readOnlyTx);
    txMap.put("*", readOnlyTx);
    source.setNameMap(txMap);
    return source;
}
 
Example 18
Source File: RuleBasedTransactionAttributeTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testRuleForSelectiveRollbackOnCheckedWithClass() {
	List<RollbackRuleAttribute> l = Collections.singletonList(new RollbackRuleAttribute(RemoteException.class));
	RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, l);
	doTestRuleForSelectiveRollbackOnChecked(rta);
}
 
Example 19
Source File: SpringJmsConnector.java    From iaf with Apache License 2.0 4 votes vote down vote up
private void configureEndpointConnection() throws ConfigurationException {
	// Create the Message Listener Container manually.
	// This is needed, because otherwise the Spring Factory will
	// call afterPropertiesSet() on the object which will validate
	// that all required properties are set before we get a chance
	// to insert our dynamic values from the config. file.
	jmsContainer = createMessageListenerContainer();

	if (jmsContainer instanceof IbisMessageListenerContainer) {
		IbisMessageListenerContainer ibisMessageListenerContainer = (IbisMessageListenerContainer)jmsContainer;
		ibisMessageListenerContainer.setCredentialFactory(credentialFactory);
	}

	if (getReceiver().isTransacted()) {
		log.debug(getLogPrefix()+"setting transction manager to ["+txManager+"]");
		jmsContainer.setTransactionManager(txManager);
		if (getReceiver().getTransactionTimeout()>0) {
			jmsContainer.setTransactionTimeout(getReceiver().getTransactionTimeout());
		}
		TX = new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED);
		if (receiveTimeout > TX.getTimeout() && TX.getTimeout() != -1) {
			throw new ConfigurationException(getLogPrefix() + "receive timeout [" + receiveTimeout
					+ "] should be smaller than transaction timeout [" + TX.getTimeout()
					+ "] as the receive time is part of the total transaction time");
		}
	} else { 
		log.debug(getLogPrefix()+"setting no transction manager");
	}
	if (sessionTransacted) { 
		jmsContainer.setSessionTransacted(sessionTransacted);
	} 
	if (StringUtils.isNotEmpty(messageSelector)) {
		jmsContainer.setMessageSelector(messageSelector);
	}
	jmsContainer.setReceiveTimeout(receiveTimeout);

	// Initialize with a number of dynamic properties which come from the configuration file
	jmsContainer.setConnectionFactory(getConnectionFactory());
	jmsContainer.setDestination(getDestination());

	jmsContainer.setExceptionListener(this);

	if (getReceiver().getNumThreads() > 0) {
		jmsContainer.setMaxConcurrentConsumers(getReceiver().getNumThreads());
	} else {
		jmsContainer.setMaxConcurrentConsumers(1);
	}
	jmsContainer.setIdleTaskExecutionLimit(IDLE_TASK_EXECUTION_LIMIT);

	if (StringUtils.isNotEmpty(cacheMode)) {
		jmsContainer.setCacheLevelName(cacheMode);
	} else {
		if (getReceiver().isTransacted()) {
			jmsContainer.setCacheLevel(DEFAULT_CACHE_LEVEL_TRANSACTED);
		} else {
			jmsContainer.setCacheLevel(DEFAULT_CACHE_LEVEL_NON_TRANSACTED);
		}
	}
	if (acknowledgeMode>=0) {
		jmsContainer.setSessionAcknowledgeMode(acknowledgeMode);
	}
	jmsContainer.setMessageListener(this);
	// Use Spring BeanFactory to complete the auto-wiring of the JMS Listener Container,
	// and run the bean lifecycle methods.
	try {
		((AutowireCapableBeanFactory) this.beanFactory).configureBean(this.jmsContainer, "proto-jmsContainer");
	} catch (BeansException e) {
		throw new ConfigurationException(getLogPrefix()+"Out of luck wiring up and configuring Default JMS Message Listener Container for JMS Listener ["+ (getListener().getName()+"]"), e);
	}

	// Finally, set bean name to something we can make sense of
	if (getListener().getName() != null) {
		jmsContainer.setBeanName(getListener().getName());
	} else {
		jmsContainer.setBeanName(getReceiver().getName());
	}
}
 
Example 20
Source File: RuleBasedTransactionAttributeTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void testRuleForSelectiveRollbackOnCheckedWithClass() {
	List<RollbackRuleAttribute> l = Collections.singletonList(new RollbackRuleAttribute(RemoteException.class));
	RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, l);
	doTestRuleForSelectiveRollbackOnChecked(rta);
}