A Persistent Bank Server

The RemoteBankServer class of Example 21-2 does not have a persistent store for customer account data. We solve that problem here with the PersistentBankServer class of Example 21-3. In addition to reiterating RMI programming techniques, this class also demonstrates the use of SQL atomic transactions to ensure database consistency.

After the PersistentBankServer creates its Connection object, it calls setAutoCommit( ) with the argument false to turn off autocommit mode. Then, for example, the openAccount( ) method groups three transactions into a single, atomic transaction: adding the account to the database, creating a table for the account history, and adding an initial entry into the history. If all three transactions are successful (i.e., they don’t throw any exceptions), openAccount( ) calls commit( ) to commit the transactions to the database. However, if any one of the transactions throws an exception, the catch clause takes care of calling rollback( ) to roll back any transactions that succeeded. All remote methods in PersistentBankServer use this technique to keep the account database consistent.

In addition to demonstrating the techniques of atomic transaction processing, the PersistentBankServer class provides further examples of using SQL queries to interact with a database. In order to run this example, you need to create a properties file named BankDB.props with database connection information, like those used in Chapter 18. Before ...

Get Java Examples in a Nutshell, 3rd 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.