org.springframework.transaction.interceptor.TransactionAttribute Java Examples

The following examples show how to use org.springframework.transaction.interceptor.TransactionAttribute. 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 spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 #2
Source File: AnnotationTransactionAttributeSourceTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 #3
Source File: AnnotationTransactionAttributeSourceTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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
Source File: AnnotationTransactionAttributeSourceTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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
Source File: AnnotationTransactionAttributeSourceTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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 #6
Source File: AnnotationTransactionAttributeSourceTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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
Source File: AnnotationTransactionAttributeSourceTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 #8
Source File: AnnotationTransactionAttributeSourceTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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 #9
Source File: AnnotationTransactionAttributeSourceTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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 #10
Source File: TransactionInterceptor.java    From micronaut-spring with 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
Source File: SpringTransactionAnnotationParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement ae) {
	AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(ae, Transactional.class);
	if (attributes != null) {
		return parseTransactionAnnotation(attributes);
	}
	else {
		return null;
	}
}
 
Example #12
Source File: SpringTransactionAnnotationParser.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement element) {
	AnnotationAttributes attributes = AnnotatedElementUtils.findMergedAnnotationAttributes(
			element, Transactional.class, false, false);
	if (attributes != null) {
		return parseTransactionAnnotation(attributes);
	}
	else {
		return null;
	}
}
 
Example #13
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 #14
Source File: AnnotationTransactionAttributeSourceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 #15
Source File: AnnotationTransactionAttributeSourceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void transactionAttributeDeclaredOnClassWithJta() throws Exception {
	Method getAgeMethod = ITestBean.class.getMethod("getAge");
	Method getNameMethod = ITestBean.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 #16
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 #17
Source File: AnnotationTransactionAttributeSourceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #18
Source File: AnnotationTransactionAttributeSourceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #19
Source File: AnnotationTransactionAttributeSourceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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 #20
Source File: AnnotationTransactionAttributeSource.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example #21
Source File: DomainTransactionAnnotationParser.java    From syncope with Apache License 2.0 5 votes vote down vote up
@Override
protected TransactionAttribute parseTransactionAnnotation(final AnnotationAttributes attributes) {
    RuleBasedTransactionAttribute rbta =
            (RuleBasedTransactionAttribute) super.parseTransactionAnnotation(attributes);
    rbta.setQualifier(AuthContextUtils.getDomain());
    return rbta;
}
 
Example #22
Source File: AnnotationTransactionAttributeSourceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 #23
Source File: AnnotationTransactionAttributeSource.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 #24
Source File: Ejb3TransactionAnnotationParser.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement element) {
	javax.ejb.TransactionAttribute ann = element.getAnnotation(javax.ejb.TransactionAttribute.class);
	if (ann != null) {
		return parseTransactionAnnotation(ann);
	}
	else {
		return null;
	}
}
 
Example #25
Source File: AnnotationTransactionAttributeSourceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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 transactionAttributeDeclaredOnClassMethod() throws Exception {
	Method classMethod = ITestBean1.class.getMethod("getAge");

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute actual = atas.getTransactionAttribute(classMethod, TestBean1.class);

	RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
	rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
	assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());
}
 
Example #26
Source File: JtaTransactionAnnotationParser.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 #27
Source File: AnnotationTransactionAttributeSourceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #28
Source File: AnnotationTransactionAttributeSourceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 #29
Source File: AnnotationTransactionAttributeSourceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void customMethodAttributeDetected() throws Exception {
	Method method = TestBean6.class.getMethod("getAge");

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean6.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 #30
Source File: AnnotationTransactionAttributeSourceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void customMethodAttributeDetected() throws Exception {
	Method method = TestBean6.class.getMethod("getAge");

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean6.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());
}