org.springframework.transaction.interceptor.TransactionInterceptor Java Examples

The following examples show how to use org.springframework.transaction.interceptor.TransactionInterceptor. 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: AnnotationTransactionAttributeSourceTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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
Source File: AnnotationTransactionAttributeSourceTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 #3
Source File: AdvisorAutoProxyCreatorIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 #4
Source File: TransactionAdviceConfig.java    From SpringBoot2.0 with 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 #5
Source File: AdvisorAutoProxyCreatorIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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 #6
Source File: AnnotationTransactionAttributeSourceTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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
Source File: AdvisorAutoProxyCreatorIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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
Source File: TxNamespaceHandlerTests.java    From spring4-understanding with Apache License 2.0 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 #9
Source File: ZebraRoutingDataSource.java    From Zebra with 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 #10
Source File: SampleTransactionManagementConfiguration.java    From spring-boot-graal-feature with Apache License 2.0 5 votes vote down vote up
@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 #11
Source File: AnnotationDrivenBeanDefinitionParser.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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 #12
Source File: ProgrammaticTransactionConfig.java    From spring-boot-cookbook with Apache License 2.0 5 votes vote down vote up
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 #13
Source File: ProxyTransactionManagementConfiguration.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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
Source File: AnnotationDrivenBeanDefinitionParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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 #15
Source File: ProxyTransactionManagementConfiguration.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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 #16
Source File: MulCommonBaseServiceParser.java    From zxl with 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 #17
Source File: DomainTransactionInterceptorInjector.java    From syncope with Apache License 2.0 5 votes vote down vote up
@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 #18
Source File: TxNamespaceHandlerTests.java    From java-technology-stack with 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 #19
Source File: PersistentServiceFactory.java    From jdal with 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 #20
Source File: SampleTransactionManagementConfiguration.java    From spring-boot-graal-feature with Apache License 2.0 5 votes vote down vote up
@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 #21
Source File: ProxyTransactionManagementConfiguration.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #22
Source File: SampleTransactionManagementConfiguration.java    From spring-boot-graal-feature with Apache License 2.0 5 votes vote down vote up
@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 #23
Source File: SampleTransactionManagementConfiguration.java    From spring-boot-graal-feature with Apache License 2.0 5 votes vote down vote up
@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 #24
Source File: TransactionManager.java    From plumemo with 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 #25
Source File: SampleTransactionManagementConfiguration.java    From spring-boot-graal-feature with Apache License 2.0 5 votes vote down vote up
@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 #26
Source File: SampleTransactionManagementConfiguration.java    From spring-boot-graal-feature with Apache License 2.0 5 votes vote down vote up
@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 #27
Source File: ProxyTransactionManagementConfiguration.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 #28
Source File: TxNamespaceHandlerTests.java    From spring-analysis-note with 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 #29
Source File: AnnotationDrivenBeanDefinitionParser.java    From java-technology-stack with MIT License 5 votes vote down vote up
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 #30
Source File: TxAdviceBeanDefinitionParser.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected Class<?> getBeanClass(Element element) {
	return TransactionInterceptor.class;
}