I find myself working through another departments code. I find a session bean which has a method foo() that participates in a transaction. This method then instantiates a JDBC Data Access Object and calls a method bar(). This method obtains its own connection from the same JNDI DataSource and performs and insert/update and closes its own connection (connection creation and closure occurs within bar, there is no explicit transaction handling code in bar()).
My question, if the larger transaction in foo() gets rolled back for some reason, will the activity in bar() get rolled back also? I have not come across any documentation that indicates either way. I would like to be 100% sure. If you find a resource, please let me know.
I have read through the head first book and it does not mention what happens when and EJB method calls a non EJB class that does JDBC work. Could someone please clarify this for me?
This should work predicatably as you expect. It even works in case where the bar() method is implemented in a 'different' EJB. Also, if the bar() method should update a table in a different database (from different vendor too!), it can achieve consistent rollback by way of distributed transaction (remember two-phase commit?). The EJB container guarantees local as well as distributed transaction.
Though the spec explicitly does not discuss these scenarios, it does mention about local and distributed transaction, which is same as the case above.
So you are saying that even though this DAO (which is not a bean and manages its own connection) will still participate in the same transaction as foo()?
Yes. absolutely, provided all the participating components (EJB, DAO) are running under a J2EE container.
If the DAO is on a remote machine and if EJB makes a plain Java RMI invocation on it, it fails miserably. The transaction simply won't propagate. You need RMI-IIOP to carry transaction and security information across containers.
If you happen to come across any documentation, please post it here.
You best bet will be trying it yourself.
SCJP, SCWCD, SCBCD, OCA J8
Look ma! I'm selling my stuff!
Devious Experiments for a Truly Passive Greenhouse!