org.springframework.transaction.interceptor.RollbackRuleAttribute Java Examples

The following examples show how to use org.springframework.transaction.interceptor.RollbackRuleAttribute. 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 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 #4
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 #5
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 #6
Source File: AnnotationTransactionAttributeSourceTests.java    From java-technology-stack 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 #7
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 #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 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 #10
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 #11
Source File: AnnotationTransactionAttributeSourceTests.java    From spring4-understanding with Apache License 2.0 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 #12
Source File: AnnotationTransactionAttributeSourceTests.java    From spring4-understanding with Apache License 2.0 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 = ITestBean.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 #13
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 #14
Source File: AnnotationTransactionAttributeSourceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void customClassAttributeWithReadOnlyOverrideDetected() throws Exception {
	Method method = TestBean7.class.getMethod("getAge");

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean7.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 #15
Source File: TransactionalConfig.java    From spring-boot-mybatisplus-multiple-datasource with Apache License 2.0 5 votes vote down vote up
/**
 * 支持当前事务;如果不存在创建一个新的
 */
private RuleBasedTransactionAttribute requiredTransactionRule () {
    RuleBasedTransactionAttribute required = new RuleBasedTransactionAttribute();
    required.setRollbackRules( Collections.singletonList( new RollbackRuleAttribute( Exception.class ) ) );
    required.setPropagationBehavior( TransactionDefinition.PROPAGATION_REQUIRED );
    required.setTimeout( TransactionDefinition.TIMEOUT_DEFAULT );
    return required;
}
 
Example #16
Source File: TransactionalConfig.java    From spring-boot-start-current with Apache License 2.0 5 votes vote down vote up
/**
 * 支持当前事务;如果不存在创建一个新的
 * {@link org.springframework.transaction.annotation.Propagation#REQUIRED}
 */
private RuleBasedTransactionAttribute requiredTransactionRule () {
	RuleBasedTransactionAttribute required = new RuleBasedTransactionAttribute();
	required.setRollbackRules( Collections.singletonList( new RollbackRuleAttribute( Exception.class ) ) );
	required.setPropagationBehavior( TransactionDefinition.PROPAGATION_REQUIRED );
	required.setTimeout( TransactionDefinition.TIMEOUT_DEFAULT );
	return required;
}
 
Example #17
Source File: AnnotationTransactionAttributeSourceTests.java    From spring4-understanding with Apache License 2.0 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 = ITestBean.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 #18
Source File: AnnotationTransactionAttributeSourceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #19
Source File: AnnotationTransactionAttributeSourceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void customClassAttributeDetected() throws Exception {
	Method method = TestBean5.class.getMethod("getAge");

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

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean7.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 #22
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 #23
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 #24
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 #25
Source File: AnnotationTransactionAttributeSourceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void customClassAttributeDetected() throws Exception {
	Method method = TestBean5.class.getMethod("getAge");

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean5.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 #26
Source File: AnnotationTransactionAttributeSourceTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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 #27
Source File: AnnotationTransactionAttributeSourceTests.java    From spring-analysis-note 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 #28
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 #29
Source File: AnnotationTransactionAttributeSourceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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 #30
Source File: AnnotationTransactionAttributeSourceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void customClassAttributeDetected() throws Exception {
	Method method = TestBean5.class.getMethod("getAge");

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