Transactions

One of the most important features of EJB is transaction management. Whether you have a simple two-tier application or a complex three-tier application, if you have a database in the mix, you have to worry about transactions. Transactions were discussed in the context of JDBC in both Chapter 3, and Chapter 4. Transactions in a three-tier environment are much more complex, but they face many of the same issues. For example, if you perform a transfer from a savings account object to a checking account object, you want to make sure that a failure at any point in that transaction results in a return to the original state of affairs. For example, if the savings account successfully debits itself but the crediting of the checking account fails, the savings account needs to get back the amount debited.

Transaction management at the distributed component level means worrying about a lot of details in the code for every single transaction; a mistake in any one of those details can place the system permanently in an inconsistent state. At the very least, a transaction in a distributed component environment needs to do the following:[28]

  • Recognize when a transaction begins.

  • Track changes that occur during a transaction.

  • Lock down the modified objects against modification by other transactions for the duration of the transaction.

  • Recognize when the transaction ends.

  • Notify the persistence library that changes need to be saved and save them within a single data store transaction.

  • Commit ...

Get Database Programming with JDBC & Java, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.