Databases Reference
In-Depth Information
Example 5-21 Handling BatchUpdateException
catch (BatchUpdateException buex) {
int [] updateCounts = buex.getUpdateCounts();
for (int i = 0; i < updateCounts.length; i++) {
System.err.println(" Statement " + i + ":" +
updateCounts[i]);
System.err.println(" Message: " + buex.getMessage());
System.err.println(" SQLSTATE: " + buex.getSQLState());
System.err.println(" Error code: " + buex.getErrorCode());
SQLException ex = buex.getNextException();
while (ex != null) {
System.err.println("SQL exception:");
System.err.println(" Message: " + ex.getMessage());
System.err.println(" SQLSTATE: " + ex.getSQLState());
System.err.println(" Error code: " + ex.getErrorCode());
ex = ex.getNextException();
}
}
5.10 Transactions
Transaction are used to access data concurrently with consistency and by
maintaining data integrity. You can view a database transaction as a set of
interactions with the database system independent of other transactions, which
are either completed or aborted. This set of interactions is called unit of work . A
unit of work can be defined as a set of SQL statements, which need to be
executed successfully to complete a task. Any failure from these SQL statements
should lead to failure of the transaction (unit of work), and the database system
should be restored to the state where it is before starting the transaction.
A single transaction can contain a set of queries, which reads and writes to the
database. To complete a transaction, an application should make sure that all of
the queries are executed successfully before ending the transaction. This is
required to maintain the integrity of the data. For example, for an order, if the
customer bought x quantity of the product y, the inventory details for product y
should be decreased by the x amount to complete the purchase order
transaction. If the purchase order is successful without updating the inventory,
the data in the inventory details becomes inconsistent.
Search WWH ::




Custom Search