Java Reference
In-Depth Information
Durability
With durability, state changes made by a transaction are permanently stored. Once an own-
er of a transaction has been informed that the transaction has succeeded, any changes made
by the transaction will survive system failure. This is typically accomplished with a trans-
action log—each of the changes is recorded and can be “replayed” to re-create the state that
existed prior to the failure. To put this in context, once the client has been notified that the
database transaction has completed, the data is safe even if the power plug is pulled on the
database. In the next two sections we'll tie transactions and these characteristics back to the
Java SE and EJB.
6.1.2. Transactions in Java
You now have a basic grasp of transactions and their importance. Transactions are a neces-
sity; otherwise the transient state would be exposed to other users and failures could wreak
havoc on data integrity. Lacking transactional support, you'd be forced to implement your
own locking, synchronization, and rollback logic. Such an effort would be exceedingly
complex, and working with transactions is complicated enough; therefore, implementing a
transaction system would be an epic endeavor. But practically all databases today support
transactions and are ACID-compliant. Let's now transition from the theoretical discussions
of transactions to the practical and see how you make transactions in Java against a data-
base.
To execute SQL and store procedures against a database, you use JDBC. JDBC abstracts
connectivity to databases so that you aren't forced to use a separate API for each database
vendor. Thus, you use the same Java code to execute an SQL statement against Oracle as
you would against DB2; the SQL obviously might be different. When working with JDBC,
you use the java.sql.DriverManager class to create a java.sql .Connec-
tion object. The Connection object represents a physical connection to the database.
On the connection object, you have a number of methods for executing calls against the
database. The following code demonstrates a sequence of SQL statements executed against
the ActionBazaar database:
Class.forName("org.postgresql.Driver");
Connection con =
DriverManager.getConnection("jdbc:postgresql:actionbazaar","user","pw");
Statement st = con.createStatement();
st.executeUpdate("insert into item ( item_id, bidstartdate, createddate ,
initialprice , itemname ) values ( 0 , current_date, current_date, 100.50 ,
Search WWH ::




Custom Search