So lets get back to the overview of transactions in Java with Spring Framework.
Bean-managed transactions can be:
- container-managed transactions or declarative transaction management
- application-managed transactions or programmatic transaction management
Declarative transaction management can be XML-based or annotation-based. A disadvantage of declarative transactions is: when a method is executing, it can be associated either with a single transaction or no transaction at all.
Programmatic transaction management gives more liberty. Take as example this pseudocode from Java EE 6 Tutorial (quite old but makes the point):
begin transaction ... update table-a ... if (condition-x) commit transaction else if (condition-y) update table-b commit transaction else rollback transaction begin transaction update table-c commit transaction
This fine-grained programmatic dependency of when to commit or rollback can only be achieved without declarative transactions.
Not mentioned so obvious is the fact of what is the DEFAULT propagation in Spring transactions.
- The default propagation is REQUIRED
|PROPAGATION TYPE||no current transaction||there’s a current transaction|
||throw exception||use current transaction|
||don’t create a transaction, run method outside any transaction||throw exception|
||don’t create a transaction, run method outside any transaction||suspend current transaction, run method outside any transaction|
||don’t create a transaction, run method outside any transaction||use current transaction|
||create a new transaction||use current transaction|
||create a new transaction||suspend current transaction, create a new independent transaction|
||create a new transaction||create a new nested transaction|
Table is from ninjalj’s blog.
So lets see what this means in the context of a database connection and what the other propagation types.
And because I never went in too much detail here, I recommend to read Marco Behler’s blog to get the full picture.