Java Code Examples for org.springframework.transaction.interceptor.TransactionInterceptor

The following examples show how to use org.springframework.transaction.interceptor.TransactionInterceptor. These examples are extracted from open source projects. 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
@Test
public void serializable() throws Exception {
	TestBean1 tb = new TestBean1();
	CallCountingTransactionManager ptm = new CallCountingTransactionManager();
	AnnotationTransactionAttributeSource tas = new AnnotationTransactionAttributeSource();
	TransactionInterceptor ti = new TransactionInterceptor(ptm, tas);

	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setInterfaces(ITestBean1.class);
	proxyFactory.addAdvice(ti);
	proxyFactory.setTarget(tb);
	ITestBean1 proxy = (ITestBean1) proxyFactory.getProxy();
	proxy.getAge();
	assertEquals(1, ptm.commits);

	ITestBean1 serializedProxy = (ITestBean1) SerializationTestUtils.serializeAndDeserialize(proxy);
	serializedProxy.getAge();
	Advised advised = (Advised) serializedProxy;
	TransactionInterceptor serializedTi = (TransactionInterceptor) advised.getAdvisors()[0].getAdvice();
	CallCountingTransactionManager serializedPtm =
			(CallCountingTransactionManager) serializedTi.getTransactionManager();
	assertEquals(2, serializedPtm.commits);
}
 
Example 2
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
	// do transaction checks
	if (requireTransactionContext) {
		TransactionInterceptor.currentTransactionStatus();
	}
	else {
		try {
			TransactionInterceptor.currentTransactionStatus();
			throw new RuntimeException("Shouldn't have a transaction");
		}
		catch (NoTransactionException ex) {
			// this is Ok
		}
	}
	super.before(method, args, target);
}
 
Example 3
@Test
public void serializable() throws Exception {
	TestBean1 tb = new TestBean1();
	CallCountingTransactionManager ptm = new CallCountingTransactionManager();
	AnnotationTransactionAttributeSource tas = new AnnotationTransactionAttributeSource();
	TransactionInterceptor ti = new TransactionInterceptor(ptm, tas);

	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setInterfaces(ITestBean1.class);
	proxyFactory.addAdvice(ti);
	proxyFactory.setTarget(tb);
	ITestBean1 proxy = (ITestBean1) proxyFactory.getProxy();
	proxy.getAge();
	assertEquals(1, ptm.commits);

	ITestBean1 serializedProxy = (ITestBean1) SerializationTestUtils.serializeAndDeserialize(proxy);
	serializedProxy.getAge();
	Advised advised = (Advised) serializedProxy;
	TransactionInterceptor serializedTi = (TransactionInterceptor) advised.getAdvisors()[0].getAdvice();
	CallCountingTransactionManager serializedPtm =
			(CallCountingTransactionManager) serializedTi.getTransactionManager();
	assertEquals(2, serializedPtm.commits);
}
 
Example 4
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
	// do transaction checks
	if (requireTransactionContext) {
		TransactionInterceptor.currentTransactionStatus();
	}
	else {
		try {
			TransactionInterceptor.currentTransactionStatus();
			throw new RuntimeException("Shouldn't have a transaction");
		}
		catch (NoTransactionException ex) {
			// this is Ok
		}
	}
	super.before(method, args, target);
}
 
Example 5
Source Project: SpringBoot2.0   Source File: TransactionAdviceConfig.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public TransactionInterceptor txAdvice() {

    DefaultTransactionAttribute required = new DefaultTransactionAttribute();
    required.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

    DefaultTransactionAttribute readonly = new DefaultTransactionAttribute();
    readonly.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    readonly.setReadOnly(true);

    NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
    source.addTransactionalMethod("add*", required);
    source.addTransactionalMethod("save*", required);
    source.addTransactionalMethod("delete*", required);
    source.addTransactionalMethod("update*", required);
    source.addTransactionalMethod("exec*", required);
    source.addTransactionalMethod("set*", required);
    source.addTransactionalMethod("do*", required);

    source.addTransactionalMethod("get*", readonly);
    source.addTransactionalMethod("query*", readonly);
    source.addTransactionalMethod("find*", readonly);
    source.addTransactionalMethod("list*", readonly);
    source.addTransactionalMethod("count*", readonly);
    return new TransactionInterceptor(transactionManager, source);
}
 
Example 6
@Test
public void serializable() throws Exception {
	TestBean1 tb = new TestBean1();
	CallCountingTransactionManager ptm = new CallCountingTransactionManager();
	AnnotationTransactionAttributeSource tas = new AnnotationTransactionAttributeSource();
	TransactionInterceptor ti = new TransactionInterceptor(ptm, tas);

	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setInterfaces(new Class[] {ITestBean.class});
	proxyFactory.addAdvice(ti);
	proxyFactory.setTarget(tb);
	ITestBean proxy = (ITestBean) proxyFactory.getProxy();
	proxy.getAge();
	assertEquals(1, ptm.commits);

	ITestBean serializedProxy = (ITestBean) SerializationTestUtils.serializeAndDeserialize(proxy);
	serializedProxy.getAge();
	Advised advised = (Advised) serializedProxy;
	TransactionInterceptor serializedTi = (TransactionInterceptor) advised.getAdvisors()[0].getAdvice();
	CallCountingTransactionManager serializedPtm =
			(CallCountingTransactionManager) serializedTi.getTransactionManager();
	assertEquals(2, serializedPtm.commits);
}
 
Example 7
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
	// do transaction checks
	if (requireTransactionContext) {
		TransactionInterceptor.currentTransactionStatus();
	}
	else {
		try {
			TransactionInterceptor.currentTransactionStatus();
			throw new RuntimeException("Shouldn't have a transaction");
		}
		catch (NoTransactionException ex) {
			// this is Ok
		}
	}
	super.before(method, args, target);
}
 
Example 8
@Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor(
		TransactionAttributeSource transactionAttributeSource, TransactionInterceptor transactionInterceptor) {
	BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor();
	advisor.setTransactionAttributeSource(transactionAttributeSource);
	advisor.setAdvice(transactionInterceptor);
	if (this.enableTx != null) {
		advisor.setOrder(this.enableTx.<Integer>getNumber("order"));
	}
	return advisor;
}
 
Example 9
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public TransactionInterceptor transactionInterceptor() {
	TransactionInterceptor interceptor = new TransactionInterceptor();
	interceptor.setTransactionAttributeSource(transactionAttributeSource());
	if (this.txManager != null) {
		interceptor.setTransactionManager(this.txManager);
	}
	return interceptor;
}
 
Example 10
@Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor(
		TransactionAttributeSource transactionAttributeSource, TransactionInterceptor transactionInterceptor) {
	BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor();
	advisor.setTransactionAttributeSource(transactionAttributeSource);
	advisor.setAdvice(transactionInterceptor);
	if (this.enableTx != null) {
		advisor.setOrder(this.enableTx.<Integer>getNumber("order"));
	}
	return advisor;
}
 
Example 11
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public TransactionInterceptor transactionInterceptor() {
	TransactionInterceptor interceptor = new TransactionInterceptor();
	interceptor.setTransactionAttributeSource(transactionAttributeSource());
	if (this.txManager != null) {
		interceptor.setTransactionManager(this.txManager);
	}
	return interceptor;
}
 
Example 12
@Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor(
		TransactionAttributeSource transactionAttributeSource,
		TransactionInterceptor transactionInterceptor) {
	BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor();
	advisor.setTransactionAttributeSource(transactionAttributeSource);
	advisor.setAdvice(transactionInterceptor);
	if (this.enableTx != null) {
		advisor.setOrder(this.enableTx.<Integer>getNumber("order"));
	}
	return advisor;
}
 
Example 13
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public TransactionInterceptor transactionInterceptor() {
	TransactionInterceptor interceptor = new TransactionInterceptor();
	interceptor.setTransactionAttributeSource(transactionAttributeSource());
	if (this.txManager != null) {
		interceptor.setTransactionManager(this.txManager);
	}
	return interceptor;
}
 
Example 14
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public TransactionInterceptor transactionInterceptor() {
	TransactionInterceptor interceptor = new TransactionInterceptor();
	interceptor.setTransactionAttributeSource(transactionAttributeSource());
	if (this.txManager != null) {
		interceptor.setTransactionManager(this.txManager);
	}
	return interceptor;
}
 
Example 15
Source Project: spring-analysis-note   Source File: TxNamespaceHandlerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void rollbackRules() {
	TransactionInterceptor txInterceptor = (TransactionInterceptor) context.getBean("txRollbackAdvice");
	TransactionAttributeSource txAttrSource = txInterceptor.getTransactionAttributeSource();
	TransactionAttribute txAttr = txAttrSource.getTransactionAttribute(getAgeMethod,ITestBean.class);
	assertTrue("should be configured to rollback on Exception",txAttr.rollbackOn(new Exception()));

	txAttr = txAttrSource.getTransactionAttribute(setAgeMethod, ITestBean.class);
	assertFalse("should not rollback on RuntimeException",txAttr.rollbackOn(new RuntimeException()));
}
 
Example 16
Source Project: plumemo   Source File: TransactionManager.java    License: Apache License 2.0 5 votes vote down vote up
public static TransactionInterceptor txAdvice(PlatformTransactionManager transactionManager) {

        // 只读事务,不做更新操作
        RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute();
        readOnlyTx.setReadOnly(true);
        readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);

        // 当前存在事务就使用当前事务,当前不存在事务就创建一个新事务
        RuleBasedTransactionAttribute requiredTx = new RuleBasedTransactionAttribute();
        requiredTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        requiredTx.setTimeout(Constants.TX_METHOD_TIMEOUT);

        Map<String, TransactionAttribute> txMap = new HashMap<>(20);
        txMap.put("save*", requiredTx);
        txMap.put("add*", requiredTx);
        txMap.put("register*", requiredTx);
        txMap.put("create*", requiredTx);
        txMap.put("update*", requiredTx);
        txMap.put("remove*", requiredTx);
        txMap.put("delete*", requiredTx);
        txMap.put("cancel*", requiredTx);
        txMap.put("do*", requiredTx);

        txMap.put("get*", readOnlyTx);
        txMap.put("list*", readOnlyTx);
        txMap.put("page*", readOnlyTx);
        txMap.put("find*", readOnlyTx);
        txMap.put("search*", readOnlyTx);
        txMap.put("load*", readOnlyTx);
        txMap.put("*", readOnlyTx);

        NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
        source.setNameMap(txMap);

        return new TransactionInterceptor(transactionManager, source);
    }
 
Example 17
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public TransactionInterceptor transactionInterceptor() {
	TransactionInterceptor interceptor = new TransactionInterceptor();
	interceptor.setTransactionAttributeSource(transactionAttributeSource());
	if (this.txManager != null) {
		interceptor.setTransactionManager(this.txManager);
	}
	return interceptor;
}
 
Example 18
public static void configureAutoProxyCreator(Element element, ParserContext parserContext) {
	AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(parserContext, element);

	String txAdvisorBeanName = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME;
	if (!parserContext.getRegistry().containsBeanDefinition(txAdvisorBeanName)) {
		Object eleSource = parserContext.extractSource(element);

		// Create the TransactionAttributeSource definition.
		RootBeanDefinition sourceDef = new RootBeanDefinition(
				"org.springframework.transaction.annotation.AnnotationTransactionAttributeSource");
		sourceDef.setSource(eleSource);
		sourceDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		String sourceName = parserContext.getReaderContext().registerWithGeneratedName(sourceDef);

		// Create the TransactionInterceptor definition.
		RootBeanDefinition interceptorDef = new RootBeanDefinition(TransactionInterceptor.class);
		interceptorDef.setSource(eleSource);
		interceptorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		registerTransactionManager(element, interceptorDef);
		interceptorDef.getPropertyValues().add("transactionAttributeSource", new RuntimeBeanReference(sourceName));
		String interceptorName = parserContext.getReaderContext().registerWithGeneratedName(interceptorDef);

		// Create the TransactionAttributeSourceAdvisor definition.
		RootBeanDefinition advisorDef = new RootBeanDefinition(BeanFactoryTransactionAttributeSourceAdvisor.class);
		advisorDef.setSource(eleSource);
		advisorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		advisorDef.getPropertyValues().add("transactionAttributeSource", new RuntimeBeanReference(sourceName));
		advisorDef.getPropertyValues().add("adviceBeanName", interceptorName);
		if (element.hasAttribute("order")) {
			advisorDef.getPropertyValues().add("order", element.getAttribute("order"));
		}
		parserContext.getRegistry().registerBeanDefinition(txAdvisorBeanName, advisorDef);

		CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), eleSource);
		compositeDef.addNestedComponent(new BeanComponentDefinition(sourceDef, sourceName));
		compositeDef.addNestedComponent(new BeanComponentDefinition(interceptorDef, interceptorName));
		compositeDef.addNestedComponent(new BeanComponentDefinition(advisorDef, txAdvisorBeanName));
		parserContext.registerComponent(compositeDef);
	}
}
 
Example 19
Source Project: java-technology-stack   Source File: TxNamespaceHandlerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void rollbackRules() {
	TransactionInterceptor txInterceptor = (TransactionInterceptor) context.getBean("txRollbackAdvice");
	TransactionAttributeSource txAttrSource = txInterceptor.getTransactionAttributeSource();
	TransactionAttribute txAttr = txAttrSource.getTransactionAttribute(getAgeMethod,ITestBean.class);
	assertTrue("should be configured to rollback on Exception",txAttr.rollbackOn(new Exception()));

	txAttr = txAttrSource.getTransactionAttribute(setAgeMethod, ITestBean.class);
	assertFalse("should not rollback on RuntimeException",txAttr.rollbackOn(new RuntimeException()));
}
 
Example 20
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public TransactionInterceptor transactionInterceptor() {
	TransactionInterceptor interceptor = new TransactionInterceptor();
	interceptor.setTransactionAttributeSource(transactionAttributeSource());
	if (this.txManager != null) {
		interceptor.setTransactionManager(this.txManager);
	}
	return interceptor;
}
 
Example 21
public static void configureAutoProxyCreator(Element element, ParserContext parserContext) {
	AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(parserContext, element);

	String txAdvisorBeanName = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME;
	if (!parserContext.getRegistry().containsBeanDefinition(txAdvisorBeanName)) {
		Object eleSource = parserContext.extractSource(element);

		// Create the TransactionAttributeSource definition.
		RootBeanDefinition sourceDef = new RootBeanDefinition(
				"org.springframework.transaction.annotation.AnnotationTransactionAttributeSource");
		sourceDef.setSource(eleSource);
		sourceDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		String sourceName = parserContext.getReaderContext().registerWithGeneratedName(sourceDef);

		// Create the TransactionInterceptor definition.
		RootBeanDefinition interceptorDef = new RootBeanDefinition(TransactionInterceptor.class);
		interceptorDef.setSource(eleSource);
		interceptorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		registerTransactionManager(element, interceptorDef);
		interceptorDef.getPropertyValues().add("transactionAttributeSource", new RuntimeBeanReference(sourceName));
		String interceptorName = parserContext.getReaderContext().registerWithGeneratedName(interceptorDef);

		// Create the TransactionAttributeSourceAdvisor definition.
		RootBeanDefinition advisorDef = new RootBeanDefinition(BeanFactoryTransactionAttributeSourceAdvisor.class);
		advisorDef.setSource(eleSource);
		advisorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		advisorDef.getPropertyValues().add("transactionAttributeSource", new RuntimeBeanReference(sourceName));
		advisorDef.getPropertyValues().add("adviceBeanName", interceptorName);
		if (element.hasAttribute("order")) {
			advisorDef.getPropertyValues().add("order", element.getAttribute("order"));
		}
		parserContext.getRegistry().registerBeanDefinition(txAdvisorBeanName, advisorDef);

		CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), eleSource);
		compositeDef.addNestedComponent(new BeanComponentDefinition(sourceDef, sourceName));
		compositeDef.addNestedComponent(new BeanComponentDefinition(interceptorDef, interceptorName));
		compositeDef.addNestedComponent(new BeanComponentDefinition(advisorDef, txAdvisorBeanName));
		parserContext.registerComponent(compositeDef);
	}
}
 
Example 22
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public TransactionInterceptor transactionInterceptor() {
	TransactionInterceptor interceptor = new TransactionInterceptor();
	interceptor.setTransactionAttributeSource(transactionAttributeSource());
	if (this.txManager != null) {
		interceptor.setTransactionManager(this.txManager);
	}
	return interceptor;
}
 
Example 23
public static void configureAutoProxyCreator(Element element, ParserContext parserContext) {
	AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(parserContext, element);

	String txAdvisorBeanName = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME;
	if (!parserContext.getRegistry().containsBeanDefinition(txAdvisorBeanName)) {
		Object eleSource = parserContext.extractSource(element);

		// Create the TransactionAttributeSource definition.
		RootBeanDefinition sourceDef = new RootBeanDefinition(
				"org.springframework.transaction.annotation.AnnotationTransactionAttributeSource");
		sourceDef.setSource(eleSource);
		sourceDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		String sourceName = parserContext.getReaderContext().registerWithGeneratedName(sourceDef);

		// Create the TransactionInterceptor definition.
		RootBeanDefinition interceptorDef = new RootBeanDefinition(TransactionInterceptor.class);
		interceptorDef.setSource(eleSource);
		interceptorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		registerTransactionManager(element, interceptorDef);
		interceptorDef.getPropertyValues().add("transactionAttributeSource", new RuntimeBeanReference(sourceName));
		String interceptorName = parserContext.getReaderContext().registerWithGeneratedName(interceptorDef);

		// Create the TransactionAttributeSourceAdvisor definition.
		RootBeanDefinition advisorDef = new RootBeanDefinition(BeanFactoryTransactionAttributeSourceAdvisor.class);
		advisorDef.setSource(eleSource);
		advisorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		advisorDef.getPropertyValues().add("transactionAttributeSource", new RuntimeBeanReference(sourceName));
		advisorDef.getPropertyValues().add("adviceBeanName", interceptorName);
		if (element.hasAttribute("order")) {
			advisorDef.getPropertyValues().add("order", element.getAttribute("order"));
		}
		parserContext.getRegistry().registerBeanDefinition(txAdvisorBeanName, advisorDef);

		CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), eleSource);
		compositeDef.addNestedComponent(new BeanComponentDefinition(sourceDef, sourceName));
		compositeDef.addNestedComponent(new BeanComponentDefinition(interceptorDef, interceptorName));
		compositeDef.addNestedComponent(new BeanComponentDefinition(advisorDef, txAdvisorBeanName));
		parserContext.registerComponent(compositeDef);
	}
}
 
Example 24
@Test
public void rollbackRules() {
	TransactionInterceptor txInterceptor = (TransactionInterceptor) context.getBean("txRollbackAdvice");
	TransactionAttributeSource txAttrSource = txInterceptor.getTransactionAttributeSource();
	TransactionAttribute txAttr = txAttrSource.getTransactionAttribute(getAgeMethod,ITestBean.class);
	assertTrue("should be configured to rollback on Exception",txAttr.rollbackOn(new Exception()));

	txAttr = txAttrSource.getTransactionAttribute(setAgeMethod, ITestBean.class);
	assertFalse("should not rollback on RuntimeException",txAttr.rollbackOn(new RuntimeException()));
}
 
Example 25
Source Project: Zebra   Source File: ZebraRoutingDataSource.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String beanName, TargetSource customTargetSource)
      throws BeansException {
	Method[] declaredMethods = beanClass.getDeclaredMethods();
	for (Method declaredMethod : declaredMethods) {
		if (pointcut.match(declaredMethod, beanClass)) {
			Advised advised = this.advisedMap.get(beanName);
			if (advised != null) {
				Advisor[] advisors = advised.getAdvisors();
				boolean added = false;
				for (int i = 0; i < advisors.length; i++) {
					if (advisors[i].getAdvice() instanceof TransactionInterceptor) {
						advised.addAdvisor(i, this);
						added = true;
						break;
					}
				}
				if (!added) {
					advised.addAdvisor(this);
				}
				return DO_NOT_PROXY;
			}
			return new Object[] { this };
		}
	}
	return DO_NOT_PROXY;
}
 
Example 26
public TransactionInterceptor defaultTransactionInterceptor(PlatformTransactionManager transactionManager,
                                                            List<Class<? extends Exception>> additionalRollbackRuleExceptions) {
    TransactionInterceptor transactionInterceptor = new TransactionInterceptor();
    Properties transactionAttributes = new Properties();

    List<RollbackRuleAttribute> rollbackRules = Lists.newArrayList();
    rollbackRules.add(new RollbackRuleAttribute(Exception.class));
    //回滚异常
    if (additionalRollbackRuleExceptions != null && !additionalRollbackRuleExceptions.isEmpty()) {
        for (Class<? extends Exception> clazz : additionalRollbackRuleExceptions) {
            rollbackRules.add(new RollbackRuleAttribute(clazz));
        }
    }
    DefaultTransactionAttribute readOnlyTransactionAttributes =
            new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED);
    readOnlyTransactionAttributes.setReadOnly(true);

    RuleBasedTransactionAttribute writeTransactionAttributes =
            new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, rollbackRules);

    String readOnlyTransactionAttributesDefinition = readOnlyTransactionAttributes.toString();
    String writeTransactionAttributesDefinition = writeTransactionAttributes.toString();
    // read-only
    transactionAttributes.setProperty("is*", readOnlyTransactionAttributesDefinition);
    transactionAttributes.setProperty("has*", readOnlyTransactionAttributesDefinition);
    transactionAttributes.setProperty("get*", readOnlyTransactionAttributesDefinition);
    transactionAttributes.setProperty("list*", readOnlyTransactionAttributesDefinition);
    transactionAttributes.setProperty("search*", readOnlyTransactionAttributesDefinition);
    transactionAttributes.setProperty("find*", readOnlyTransactionAttributesDefinition);
    transactionAttributes.setProperty("count*", readOnlyTransactionAttributesDefinition);
    // write et rollback-rule
    transactionAttributes.setProperty("*", writeTransactionAttributesDefinition);

    transactionInterceptor.setTransactionAttributes(transactionAttributes);
    transactionInterceptor.setTransactionManager(this.transactionManager);
    return transactionInterceptor;
}
 
Example 27
Source Project: zxl   Source File: MulCommonBaseServiceParser.java    License: Apache License 2.0 5 votes vote down vote up
private BeanDefinition buildHibernateAdviceBeanDefinition(Element element, String name) {
	AbstractBeanDefinition beanDefinition = new GenericBeanDefinition();
	beanDefinition.setAttribute(ID_ATTRIBUTE, name + HIBERNATE_ADVICE_SUFFIX);
	beanDefinition.setBeanClass(TransactionInterceptor.class);
	MutablePropertyValues propertyValues = new MutablePropertyValues();
	propertyValues.add("transactionManager", new RuntimeBeanReference(name + HIBERNATE_TRANSACTION_MANAGER_SUFFIX));
	propertyValues.add("transactionAttributeSource", new RuntimeBeanReference(name + TRANSACTION_ATTRIBUTE_SOURCE_SUFFIX));
	beanDefinition.setPropertyValues(propertyValues);
	return beanDefinition;
}
 
Example 28
@Override
public void postProcessBeanFactory(final ConfigurableListableBeanFactory beanFactory) throws BeansException {
    for (String name : beanFactory.getBeanNamesForType(TransactionInterceptor.class, false, false)) {
        BeanDefinition bd = beanFactory.getBeanDefinition(name);
        bd.setBeanClassName(DomainTransactionInterceptor.class.getName());
        bd.setFactoryBeanName(null);
        bd.setFactoryMethodName(null);
    }
}
 
Example 29
Source Project: jdal   Source File: PersistentServiceFactory.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a default transactional proxy for service with default transacction attributes
 * @param <T>
 * @param service
 * @return a Tx Proxy for service with default tx attributes
 */
@SuppressWarnings("unchecked")
public <T>  PersistentManager<T, Serializable> makeTransactionalProxy(PersistentManager<T, Serializable> service) {
	ProxyFactory factory = new ProxyFactory(service);
	factory.setInterfaces(new Class[] {Dao.class});
	TransactionInterceptor interceptor = new TransactionInterceptor(transactionManager, 
			new MatchAlwaysTransactionAttributeSource()); 
	factory.addAdvice(interceptor);
	factory.setTarget(service);
	return (PersistentManager<T, Serializable>) factory.getProxy();
}
 
Example 30
@Override
protected Class<?> getBeanClass(Element element) {
	return TransactionInterceptor.class;
}