"Mu class network java architect train" course notes

1. spread affairs

REQUIRED(必须的)(TransactionDefinition.PROPAGATION_REQUIRED)Use the current transaction, if no transaction, their new one transaction, the child is to be run in a transaction, if the current transaction exists, then join the transaction as a whole.
For example: leadership no money, but I have money, I would buy myself eat (transaction does not exist); if the money leader, will give some to me (affairs);

SUPPORTS(支持)(TransactionDefinition.PROPAGATION_SUPPORTS)If you currently have a transaction, the transaction is used, and if not, is not used.
For example: leadership did not eat, I did not eat, leading food to eat, I have food to eat, that is, as the following code, I added a transaction on savechild () method, but there is no increase in the parent process , there will be no business, if I'm the parent method also added a transaction, the transaction will be achieved.

    @Transactional(propagation = Propagation.REQUIRED)
    @Test
    public void saveParent(){
        stuService.saveParent();
        stuService.saveChild();
    }
    //当propagation =  Propagation.SUPPORTS时,单独加这个事务,在调用过程中是不起作用的
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void saveChild() {
        saveChild1();
        int s = 1/0;
        saveChild2();

    }
    public void saveChild1(){
        Stu stu = new Stu();
        stu.setAge(23);
        stu.setName("child1");
    }

    public void saveChild2(){
        Stu stu = new Stu();
        stu.setAge(23);
        stu.setName("child2");
    }

MANDATORY(强制的)(TransactionDefinition.PROPAGATION_MANDATORY)This will be mandatory for the parent of the caller Add affairs, otherwise they will be reported the following error;
for example: Leadership must Guanfan, regardless of the meal I did not

org.springframework.transaction.IllegalTransactionStateException: No existing transaction found for transaction marked with propagation 'mandatory'

	at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:364)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:475)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:289)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
	at com.imooc.service.impl.StuServiceImpl$$EnhancerBySpringCGLIB$$ab72f9f8.saveChild(<generated>)
	at com.imooc.TranTest.saveParent(TranTest.java:33)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
	at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)


REQUIRES_NEW(必须创建新的)(TransactionDefinition.PROPAGATION_REQUIRES_NEW)If you currently have a transaction, the transaction is pending, and creates a new transaction for their own use. If no transaction is the same REQUIRED.

NOT_SUPPORTED(TransactionDefinition.PROPAGATION_NOT_SUPPORTED) If you currently have affairs, put the transaction pending, they are not executed.

NOT_SUPPORTED(TransactionDefinition.PROPAGATION_NOT_SUPPORTED)The caller does not allow any transactions.
NESTED(TransactionDefinition.PROPAGATION_NESTED)Call arisen abnormal, subtransaction rollback will also be affected. Abnormal child transaction occurs, will be rolled back, however, if we were to add the caller trycatch, then even if an exception has arisen is called, it will not affect the execution of the caller.

Published 16 original articles · won praise 4 · Views 2386

Guess you like

Origin blog.csdn.net/hzy3344520/article/details/104286490