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

The following examples show how to use org.springframework.transaction.interceptor.TransactionAttribute. 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 the important case where the invocation is on a proxied interface method
 * but the attribute is defined on the target class.
 */
@Test
public void transactionAttributeDeclaredOnCglibClassMethod() throws Exception {
	Method classMethod = ITestBean1.class.getMethod("getAge");
	TestBean1 tb = new TestBean1();
	ProxyFactory pf = new ProxyFactory(tb);
	pf.setProxyTargetClass(true);
	Object proxy = pf.getProxy();

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute actual = atas.getTransactionAttribute(classMethod, proxy.getClass());

	RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
	rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
	assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());
}
 
Example 2
/**
 * Test that when an attribute exists on both class and interface, class takes precedence.
 */
@Test
public void transactionAttributeOnTargetClassMethodOverridesAttributeOnInterfaceMethod() throws Exception {
	Method interfaceMethod = ITestBean3.class.getMethod("getAge");
	Method interfaceMethod2 = ITestBean3.class.getMethod("getName");

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute actual = atas.getTransactionAttribute(interfaceMethod, TestBean3.class);
	assertEquals(TransactionAttribute.PROPAGATION_REQUIRES_NEW, actual.getPropagationBehavior());
	assertEquals(TransactionAttribute.ISOLATION_REPEATABLE_READ, actual.getIsolationLevel());
	assertEquals(5, actual.getTimeout());
	assertTrue(actual.isReadOnly());

	RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
	rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
	rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class));
	assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());

	TransactionAttribute actual2 = atas.getTransactionAttribute(interfaceMethod2, TestBean3.class);
	assertEquals(TransactionAttribute.PROPAGATION_REQUIRED, actual2.getPropagationBehavior());
}
 
Example 3
@Test
public void customClassAttributeWithReadOnlyOverrideOnInterface() throws Exception {
	Method method = TestInterface9.class.getMethod("getAge");

	Transactional annotation = AnnotationUtils.findAnnotation(method, Transactional.class);
	assertNull("AnnotationUtils.findAnnotation should not find @Transactional for TestBean9.getAge()", annotation);
	annotation = AnnotationUtils.findAnnotation(TestBean9.class, Transactional.class);
	assertNotNull("AnnotationUtils.findAnnotation failed to find @Transactional for TestBean9", annotation);

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean9.class);
	assertNotNull("Failed to retrieve TransactionAttribute for TestBean9.getAge()", actual);

	RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
	rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
	rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class));
	assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());

	assertTrue(actual.isReadOnly());
}
 
Example 4
@Test
public void customMethodAttributeWithReadOnlyOverrideOnInterface() throws Exception {
	Method method = TestInterface10.class.getMethod("getAge");

	Transactional annotation = AnnotationUtils.findAnnotation(method, Transactional.class);
	assertNotNull("AnnotationUtils.findAnnotation failed to find @Transactional for TestBean10.getAge()",
			annotation);
	annotation = AnnotationUtils.findAnnotation(TestBean10.class, Transactional.class);
	assertNull("AnnotationUtils.findAnnotation should not find @Transactional for TestBean10", annotation);

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean10.class);
	assertNotNull("Failed to retrieve TransactionAttribute for TestBean10.getAge()", actual);

	RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
	rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
	rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class));
	assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());

	assertTrue(actual.isReadOnly());
}
 
Example 5
@Test
public void customMethodAttributeWithReadOnlyOverrideOnInterface() throws Exception {
	Method method = TestInterface10.class.getMethod("getAge");

	Transactional annotation = AnnotationUtils.findAnnotation(method, Transactional.class);
	assertNotNull("AnnotationUtils.findAnnotation failed to find @Transactional for TestBean10.getAge()",
			annotation);
	annotation = AnnotationUtils.findAnnotation(TestBean10.class, Transactional.class);
	assertNull("AnnotationUtils.findAnnotation should not find @Transactional for TestBean10", annotation);

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean10.class);
	assertNotNull("Failed to retrieve TransactionAttribute for TestBean10.getAge()", actual);

	RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
	rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
	rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class));
	assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());

	assertTrue(actual.isReadOnly());
}
 
Example 6
/**
 * Test the important case where the invocation is on a proxied interface method
 * but the attribute is defined on the target class.
 */
@Test
public void transactionAttributeDeclaredOnCglibClassMethod() throws Exception {
	Method classMethod = ITestBean1.class.getMethod("getAge");
	TestBean1 tb = new TestBean1();
	ProxyFactory pf = new ProxyFactory(tb);
	pf.setProxyTargetClass(true);
	Object proxy = pf.getProxy();

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute actual = atas.getTransactionAttribute(classMethod, proxy.getClass());

	RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
	rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
	assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());
}
 
Example 7
/**
 * Test that when an attribute exists on both class and interface, class takes precedence.
 */
@Test
public void transactionAttributeOnTargetClassMethodOverridesAttributeOnInterfaceMethod() throws Exception {
	Method interfaceMethod = ITestBean3.class.getMethod("getAge");
	Method interfaceMethod2 = ITestBean3.class.getMethod("getName");

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute actual = atas.getTransactionAttribute(interfaceMethod, TestBean3.class);
	assertEquals(TransactionAttribute.PROPAGATION_REQUIRES_NEW, actual.getPropagationBehavior());
	assertEquals(TransactionAttribute.ISOLATION_REPEATABLE_READ, actual.getIsolationLevel());
	assertEquals(5, actual.getTimeout());
	assertTrue(actual.isReadOnly());

	RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
	rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
	rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class));
	assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());

	TransactionAttribute actual2 = atas.getTransactionAttribute(interfaceMethod2, TestBean3.class);
	assertEquals(TransactionAttribute.PROPAGATION_REQUIRED, actual2.getPropagationBehavior());
}
 
Example 8
@Test
public void customClassAttributeWithReadOnlyOverrideOnInterface() throws Exception {
	Method method = TestInterface9.class.getMethod("getAge");

	Transactional annotation = AnnotationUtils.findAnnotation(method, Transactional.class);
	assertNull("AnnotationUtils.findAnnotation should not find @Transactional for TestBean9.getAge()", annotation);
	annotation = AnnotationUtils.findAnnotation(TestBean9.class, Transactional.class);
	assertNotNull("AnnotationUtils.findAnnotation failed to find @Transactional for TestBean9", annotation);

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean9.class);
	assertNotNull("Failed to retrieve TransactionAttribute for TestBean9.getAge()", actual);

	RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
	rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
	rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class));
	assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());

	assertTrue(actual.isReadOnly());
}
 
Example 9
@Test
public void customClassAttributeWithReadOnlyOverrideOnInterface() throws Exception {
	Method method = TestInterface9.class.getMethod("getAge");

	Transactional annotation = AnnotationUtils.findAnnotation(method, Transactional.class);
	assertNull("AnnotationUtils.findAnnotation should not find @Transactional for TestBean9.getAge()", annotation);
	annotation = AnnotationUtils.findAnnotation(TestBean9.class, Transactional.class);
	assertNotNull("AnnotationUtils.findAnnotation failed to find @Transactional for TestBean9", annotation);

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean9.class);
	assertNotNull("Failed to retrieve TransactionAttribute for TestBean9.getAge()", actual);

	RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
	rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
	rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class));
	assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());

	assertTrue(actual.isReadOnly());
}
 
Example 10
Source Project: micronaut-spring   Source File: TransactionInterceptor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param targetMethod           The target method
 * @param annotationMetadata     The annotation metadata
 * @param transactionManagerName The transaction manager
 * @return The {@link TransactionAttribute}
 */
protected TransactionAttribute resolveTransactionAttribute(
        ExecutableMethod<Object, Object> targetMethod,
        AnnotationMetadata annotationMetadata,
        String transactionManagerName) {
    return transactionDefinitionMap.computeIfAbsent(targetMethod, method -> {

        BindableRuleBasedTransactionAttribute attribute = new BindableRuleBasedTransactionAttribute();
        attribute.setReadOnly(annotationMetadata.isTrue(Transactional.class, "readOnly"));
        attribute.setTimeout(annotationMetadata.intValue(Transactional.class, "timeout").orElse(TransactionDefinition.TIMEOUT_DEFAULT));
        //noinspection unchecked
        attribute.setRollbackFor(annotationMetadata.classValues(Transactional.class, "rollbackFor"));
        //noinspection unchecked
        attribute.setNoRollbackFor(annotationMetadata.classValues(Transactional.class, "noRollbackFor"));
        int propagation = annotationMetadata
                .enumValue(Transactional.class, "propagation", Propagation.class)
                .orElse(Propagation.REQUIRED).value();
        attribute.setPropagationBehavior(propagation);
        int isolation = annotationMetadata
                .enumValue(Transactional.class, "isolation", Isolation.class)
                .orElse(Isolation.DEFAULT).value();
        attribute.setIsolationLevel(isolation);
        attribute.setQualifier(transactionManagerName);
        return attribute;
    });
}
 
Example 11
/**
 * Determine the transaction attribute for the given method or class.
 * <p>This implementation delegates to configured
 * {@link TransactionAnnotationParser TransactionAnnotationParsers}
 * for parsing known annotations into Spring's metadata attribute class.
 * Returns {@code null} if it's not transactional.
 * <p>Can be overridden to support custom annotations that carry transaction metadata.
 * @param element the annotated method or class
 * @return the configured transaction attribute, or {@code null} if none was found
 */
@Nullable
protected TransactionAttribute determineTransactionAttribute(AnnotatedElement element) {
	for (TransactionAnnotationParser parser : this.annotationParsers) {
		TransactionAttribute attr = parser.parseTransactionAnnotation(element);
		if (attr != null) {
			return attr;
		}
	}
	return null;
}
 
Example 12
@Test
public void transactionAttributeDeclaredOnClassMethodWithEjb3() throws Exception {
	Method getAgeMethod = ITestBean.class.getMethod("getAge");
	Method getNameMethod = ITestBean.class.getMethod("getName");

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute getAgeAttr = atas.getTransactionAttribute(getAgeMethod, Ejb3AnnotatedBean1.class);
	assertEquals(TransactionAttribute.PROPAGATION_REQUIRED, getAgeAttr.getPropagationBehavior());
	TransactionAttribute getNameAttr = atas.getTransactionAttribute(getNameMethod, Ejb3AnnotatedBean1.class);
	assertEquals(TransactionAttribute.PROPAGATION_SUPPORTS, getNameAttr.getPropagationBehavior());
}
 
Example 13
@Override
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement ae) {
	AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(ae, Transactional.class);
	if (attributes != null) {
		return parseTransactionAnnotation(attributes);
	}
	else {
		return null;
	}
}
 
Example 14
/**
 * Determine the transaction attribute for the given method or class.
 * <p>This implementation delegates to configured
 * {@link TransactionAnnotationParser TransactionAnnotationParsers}
 * for parsing known annotations into Spring's metadata attribute class.
 * Returns {@code null} if it's not transactional.
 * <p>Can be overridden to support custom annotations that carry transaction metadata.
 * @param ae the annotated method or class
 * @return TransactionAttribute the configured transaction attribute,
 * or {@code null} if none was found
 */
protected TransactionAttribute determineTransactionAttribute(AnnotatedElement ae) {
	if (ae.getAnnotations().length > 0) {
		for (TransactionAnnotationParser annotationParser : this.annotationParsers) {
			TransactionAttribute attr = annotationParser.parseTransactionAnnotation(ae);
			if (attr != null) {
				return attr;
			}
		}
	}
	return null;
}
 
Example 15
@Override
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement ae) {
	javax.ejb.TransactionAttribute ann = ae.getAnnotation(javax.ejb.TransactionAttribute.class);
	if (ann != null) {
		return parseTransactionAnnotation(ann);
	}
	else {
		return null;
	}
}
 
Example 16
/**
 * Test case where attribute is on the interface method.
 */
@Test
public void transactionAttributeDeclaredOnInterfaceMethodOnly() throws Exception {
	Method interfaceMethod = ITestBean2.class.getMethod("getAge");

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute actual = atas.getTransactionAttribute(interfaceMethod, TestBean2.class);

	RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
	assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());
}
 
Example 17
/**
 * Test that transaction attribute is inherited from class
 * if not specified on method.
 */
@Test
public void defaultsToClassTransactionAttribute() throws Exception {
	Method method = TestBean4.class.getMethod("getAge");

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean4.class);

	RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
	rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
	rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class));
	assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());
}
 
Example 18
/**
 * If the test method of the supplied {@linkplain TestContext test context}
 * is configured to run within a transaction, this method will run
 * {@link BeforeTransaction @BeforeTransaction} methods and start a new
 * transaction.
 * <p>Note that if a {@code @BeforeTransaction} method fails, any remaining
 * {@code @BeforeTransaction} methods will not be invoked, and a transaction
 * will not be started.
 * @see org.springframework.transaction.annotation.Transactional
 * @see #getTransactionManager(TestContext, String)
 */
@Override
public void beforeTestMethod(final TestContext testContext) throws Exception {
	final Method testMethod = testContext.getTestMethod();
	final Class<?> testClass = testContext.getTestClass();
	Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null");

	TransactionContext txContext = TransactionContextHolder.removeCurrentTransactionContext();
	if (txContext != null) {
		throw new IllegalStateException("Cannot start a new transaction without ending the existing transaction.");
	}

	PlatformTransactionManager tm = null;
	TransactionAttribute transactionAttribute = this.attributeSource.getTransactionAttribute(testMethod, testClass);

	if (transactionAttribute != null) {
		transactionAttribute = TestContextTransactionUtils.createDelegatingTransactionAttribute(testContext,
			transactionAttribute);

		if (logger.isDebugEnabled()) {
			logger.debug("Explicit transaction definition [" + transactionAttribute + "] found for test context "
					+ testContext);
		}

		if (transactionAttribute.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
			return;
		}

		tm = getTransactionManager(testContext, transactionAttribute.getQualifier());
	}

	if (tm != null) {
		txContext = new TransactionContext(testContext, tm, transactionAttribute, isRollback(testContext));
		runBeforeTransactionMethods(testContext);
		txContext.startTransaction();
		TransactionContextHolder.setCurrentTransactionContext(txContext);
	}
}
 
Example 19
@Test
public void customMethodAttributeWithReadOnlyOverrideDetected() throws Exception {
	Method method = TestBean8.class.getMethod("getAge");

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean8.class);

	RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
	rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
	rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException.class));
	assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());

	assertTrue(actual.isReadOnly());
}
 
Example 20
/**
 * If the TransactionAttribute has a setTimeout() method, then this method will invoke it and pass the configured
 * transaction timeout.  This is to allow for proper setting of the transaction timeout on a JTA transaction.
 */
protected void setTimeout(TransactionAttribute transactionAttribute) {
    try {
        if (transactionTimeout != null) {
            PropertyUtils.setSimpleProperty(transactionAttribute, "timeout", new Integer(transactionTimeout));
        }
    } catch (Exception e) {
        // failed to set the timeout
    }
}
 
Example 21
@Test
public void transactionAttributeDeclaredOnInterfaceWithEjb3() throws Exception {
	Method getAgeMethod = ITestEjb.class.getMethod("getAge");
	Method getNameMethod = ITestEjb.class.getMethod("getName");

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute getAgeAttr = atas.getTransactionAttribute(getAgeMethod, Ejb3AnnotatedBean3.class);
	assertEquals(TransactionAttribute.PROPAGATION_REQUIRED, getAgeAttr.getPropagationBehavior());
	TransactionAttribute getNameAttr = atas.getTransactionAttribute(getNameMethod, Ejb3AnnotatedBean3.class);
	assertEquals(TransactionAttribute.PROPAGATION_SUPPORTS, getNameAttr.getPropagationBehavior());
}
 
Example 22
@Test
public void transactionAttributeDeclaredOnClassMethodWithJta() throws Exception {
	Method getAgeMethod = ITestBean.class.getMethod("getAge");
	Method getNameMethod = ITestBean.class.getMethod("getName");

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute getAgeAttr = atas.getTransactionAttribute(getAgeMethod, JtaAnnotatedBean1.class);
	assertEquals(TransactionAttribute.PROPAGATION_REQUIRED, getAgeAttr.getPropagationBehavior());
	TransactionAttribute getNameAttr = atas.getTransactionAttribute(getNameMethod, JtaAnnotatedBean1.class);
	assertEquals(TransactionAttribute.PROPAGATION_SUPPORTS, getNameAttr.getPropagationBehavior());
}
 
Example 23
@Test
public void transactionAttributeDeclaredOnClassWithJta() throws Exception {
	Method getAgeMethod = ITestBean1.class.getMethod("getAge");
	Method getNameMethod = ITestBean1.class.getMethod("getName");

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute getAgeAttr = atas.getTransactionAttribute(getAgeMethod, JtaAnnotatedBean2.class);
	assertEquals(TransactionAttribute.PROPAGATION_REQUIRED, getAgeAttr.getPropagationBehavior());
	TransactionAttribute getNameAttr = atas.getTransactionAttribute(getNameMethod, JtaAnnotatedBean2.class);
	assertEquals(TransactionAttribute.PROPAGATION_SUPPORTS, getNameAttr.getPropagationBehavior());
}
 
Example 24
@Override
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement ae) {
	AnnotationAttributes attributes =
			AnnotatedElementUtils.getMergedAnnotationAttributes(ae, javax.transaction.Transactional.class);
	if (attributes != null) {
		return parseTransactionAnnotation(attributes);
	}
	else {
		return null;
	}
}
 
Example 25
@Test
public void transactionAttributeDeclaredOnGroovyClass() throws Exception {
	Method getAgeMethod = ITestBean1.class.getMethod("getAge");
	Method getNameMethod = ITestBean1.class.getMethod("getName");
	Method getMetaClassMethod = GroovyObject.class.getMethod("getMetaClass");

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute getAgeAttr = atas.getTransactionAttribute(getAgeMethod, GroovyTestBean.class);
	assertEquals(TransactionAttribute.PROPAGATION_REQUIRED, getAgeAttr.getPropagationBehavior());
	TransactionAttribute getNameAttr = atas.getTransactionAttribute(getNameMethod, GroovyTestBean.class);
	assertEquals(TransactionAttribute.PROPAGATION_REQUIRED, getNameAttr.getPropagationBehavior());
	assertNull(atas.getTransactionAttribute(getMetaClassMethod, GroovyTestBean.class));
}
 
Example 26
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 27
@Test
public void transactionAttributeDeclaredOnClassWithEjb3() throws Exception {
	Method getAgeMethod = ITestBean.class.getMethod("getAge");
	Method getNameMethod = ITestBean.class.getMethod("getName");

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute getAgeAttr = atas.getTransactionAttribute(getAgeMethod, Ejb3AnnotatedBean2.class);
	assertEquals(TransactionAttribute.PROPAGATION_REQUIRED, getAgeAttr.getPropagationBehavior());
	TransactionAttribute getNameAttr = atas.getTransactionAttribute(getNameMethod, Ejb3AnnotatedBean2.class);
	assertEquals(TransactionAttribute.PROPAGATION_SUPPORTS, getNameAttr.getPropagationBehavior());
}
 
Example 28
@Override
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement ae) {
	AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(ae, Transactional.class);
	if (attributes != null) {
		return parseTransactionAnnotation(attributes);
	}
	else {
		return null;
	}
}
 
Example 29
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 30
/**
 * Determine the transaction attribute for the given method or class.
 * <p>This implementation delegates to configured
 * {@link TransactionAnnotationParser TransactionAnnotationParsers}
 * for parsing known annotations into Spring's metadata attribute class.
 * Returns {@code null} if it's not transactional.
 * <p>Can be overridden to support custom annotations that carry transaction metadata.
 * @param element the annotated method or class
 * @return the configured transaction attribute, or {@code null} if none was found
 */
@Nullable
protected TransactionAttribute determineTransactionAttribute(AnnotatedElement element) {
	for (TransactionAnnotationParser annotationParser : this.annotationParsers) {
		TransactionAttribute attr = annotationParser.parseTransactionAnnotation(element);
		if (attr != null) {
			return attr;
		}
	}
	return null;
}