O'Reilly logo

Java Examples in a Nutshell, 3rd Edition by David Flanagan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required