You'd need to pick up a
J2EE book and have a look at transactions, but in the broadest of terms:
J2EE supports the 2-phase protocol, which allows the server to gather and combine transactions from multiple '2-phase transaction supporting locations'. Taking this back to your question, as long as the database and drivers are XA compliant (a clever way of saying 'we do that 2-phase thingy') the server maintains a transaction context, and any external transactions (like those in the database) are considered part of the server transaction.
I'm going to stop here, since I've exhausted my knowledge on the area