Propagation required clariification spring
wanted to clarify the Propagation required fundamental with below scenarios. . Please let me know if below understanding is correct.
Class MyClass1{
//Propagation is Propagation required
public void method1();
{
method1A();
method1B();
MyClass2 myClass2= new MyClass2();
myClass2.method2A();
myClass2.method2B();
}
// No porapgation is defined here so default will be reuired
public method1A()
{
//Some Transaction
}
// No porapgation is defined here so default will be reuired
private method1B()
{
//Some Transaction
}
}
Class MyClass2{
//Propagation is Propagation required
public void method2()
{
method2A();
method2B();
}
// No porapgation is defined here so default will be required
public method2A()
{
//Some Transaction
}
// No porapgati开发者_如何学JAVAon is defined here so default will be required
public method2B()
{
//Some Transaction
}
}
Now here are the scenarios
we call the method1() of MyClass1 inside main method
Scenarion1:-
No exception occurs. transaction will be created before method1A() and will be commited after myClass2.method2B();
Scenarion2:-
Runtime exception occurs during method1B. Complete transaction will be rolled back
Scenarion3:-
Runtime exception occurs during method2A(Transaction under method2A will be treated as part of transaction created under method1 in class1) .Complete transaction will be rolled back
Scenarion4:-
Runtime exception occurs during method2B(Transaction under method2A will be treated as part of transaction created under method1 in class1) .Complete transaction will be rolled back
Edit:-
Now if we consider the same scenarios with propagation as Nested for methods method2A and method2B.
Scenarion1:-
No exception occurs. transaction will be created on entering method1A() and will be commited on exit of method1A()
Scenarion2:-
Runtime exception occurs during method1B. Complete transaction will be rolled back
Scenarion3:-
Runtime exception occurs during method2A .Only transaction under method2A will be rolled back and rest of the transaction will be commited on exit of method1
Scenarion4:-
Runtime exception occurs during method2B. Only transaction under method2B will be rolled back and rest of the transaction will be commited on exit of method1
Your understanding is (generally) correct, but your example is flawed. By calling:
MyClass2 myClass2= new MyClass2();
You've ensured that method calls on myClass2 will not be intercepted by the transactional proxy, and therefore any propagation required semantics implied here don't really matter since they won't be applied. In this case, however, you will fall within the transactional boundaries of method1 and since you've marked it as propagation required, your code will execute as you've described. You would do well to come up with a SSCCE if you require further clarification.
Also, the Spring documentation on Transaction Management is some of the best you'll find, I highly recommend you take a look at it.
精彩评论