OK, got your point and I know what you are thinking, now let me show how powerful it is,
Please see the
Fake code
[Module A]
Code:
@Transactional
public void complexBusinessLogic()
{
moudleB.step1();
moudleB.step2();
moudleB.step3();
moudleB.step4();
... other code ...
}
[Module B]
Code:
@Transaction(propagation = Propagation.REQUIRED) // or nothing
public void step1() { ... }
@Transaction(propagation = Propagation.REQUIRES_NEW)
public void step2() { ... }
@Transaction(propagation = Propagation.REQUIRED) // or nothing
public void step3() { ... }
@Transaction(propagation = Propagation.REQUIRES_NEW)
public void step4() { ... }
As we see, one complex business has 4 sub steps.
(1) ModuleA.complexBusinessLogic: uses @Transactional, it's necessary, can't be omitted, create an transaction
(2) MoudleB.step1 and ModuleB.step3: uses @Transactional with Propagation.REQUIRED, that means they will share the transaction created by the business logic module
(3) MoudleB.step2 and ModuleB.step4: uses @Transactional with Propagation.REQUIRES_NEW, that means they will ignore the transaction created by the business logic module and create their own transactions.
Finally.
3 Db connections and transactions are created.
One of them is created by business logic module and it's shared by step1 and step3, so db operation of step1 and step3 will be rollback if the complex business throw exception or error. they can only be committed when the business logic is done successfully; the other two of them are created by step2 and step4, so step2 and step4 are out of control, they can still be committed even if the business logic meet some exception or error.
Transaction Propagation Table: (Order by time desc, youngest first, oldest last. Young technology learns from old technology)
(1) Spring: REQUIRED, REQUIRES_NEW, SUPPORTS, NOT_SUPPORTED, MANDATORY, NEVER, NESTED(Requires special database)
link: http://www.byteslounge.com/tutorials/spring-transaction-propagation-tutorial
(2) EJB: REQUIRED, REQUIRES_NEW, SUPPORTS, NOT_SUPPORTED, MANDATORY, NEVER
link: http://www.tutorialspoint.com/ejb/ejb_transactions.htm
(3) COM+(Microsoft Visual C++ or .NET): Required, RequiresNew, Supported, NotSupported, Disabled
link: https://msdn.microsoft.com/en-us/library/windows/desktop/ms687120(v=vs.85).aspx