I'm trying to find out the best way to handle transactions at object level (not database level). Short example: 4 objects A, B, C and D. A starts a transaction and calls methods in B and C. Whithin this transaction C is also calling D. The methods being called aren't supposed to always participate in this transaction, but can be called also on their own. Are there any patterns for managing transactions at object level?
I didn't really find something, so I came up with this: Use a TransactionContext where one can register TransactionListeners. If a transaction is started using the TransactionContext, then it will inject the running transaction into each of the registered listeners, which in turn will use a running transaction or elsewise will start one on their own if needed. This way I'm pretty free to decide wether I want an object participating in my transaction or not.
The problem comes when having object calling chains like above. When starting the transaction I just know that B and C must participate in the transaction so I add them to the TransactionContext. But what about D? I don't really want to pass the TransactionContext around to B and C.
I would appreciate some input on my approach as well as some pointers to proven patterns (even better).