First let me explain you what I mean by a nested transaction.
Example: say in the main class we call method1
and create the customer using jdbc[Transaction1]. It is not commited yet. Now we call开发者_运维技巧 method2
in the main class and create the account for the just created customer[Transaction2]. Now commit it. As per your explanation both these transactions will be treated as part of one transaction (as there can be a maximum of one transaction with a connection). Till here, if we compare the above scenario, it will be like propagation_required
in Spring. Is that correct?
Now if we want to commit transaction2 only not the one. Then this scenario will be like propagation_Nested
in Spring. Is that correct?
How can we implement a nested transaction in JDBC if both my assumptions stated above are correct?
This not exactly how nested transactions work. If you roll back transaction 1, transaction 2 also rolls back. With the nested transactions you can rollback transaction 2 and still commit transaction 1.
In the JDBC you can achieve this effect using savepoints. You can call Connection.setSavepoint() before creating account and if you want to rollback that action but still commit creation of the customer, you can rollback to that savepoint.
If you want to be able to commit/rollback two transactions completely independently, like Spring REQUIRES_NEW, in JDBC you should use two connections and manage transactions on them independently.
精彩评论