![]() |
|||
![]() ![]() |
![]() |
![]()
|
![]() |
Transaction Isolation LevelsANSI defines three standard levels of transaction isolation. Transaction isolation makes sense when concurrent transactions execute simultaneously. The ANSI specification defines restrictions on the kinds of action permitted in concurrent transactions so as to prevent dirty reads, nonrepeatable reads, and phantoms.
The higher levels include restrictions imposed by all the lower levels. In practice, compatibility with all the transaction isolation levels is achieved using locking techniques. Check your database documentation for information on these techniques and see how they can affect performances in a multiuser environment. As a general rule, the higher the transaction isolation level, the longer locks are held. Managing Transactions with JDBCJDBC always opens connections in autocommit mode. This opening mode means that each statement is executed as a separate transaction without needing to supply commit or rollback commands. In this default mode, it is not possible to perform rollbacks. JDBC provides methods to turn off autocommit mode, to set the transaction isolation level, and to commit or rollback transactions. JDBC transactions begin as soon as the autocommit mode is disabled. In this case, an implicit transaction is associated with the connection, and it is completed or aborted with commit and rollback methods. The commit or rollback starts a new implicit transaction. The commit and rollback make JDBC close all PreparedStatements, CallableStatements, and ResultSets opened during the transaction. Simple statement objects stay open. This is the default behavior and it may be disabled. The JDBC methods to manage transactions are these: Connection void setTransactionIsolation(int isolationlevel); int getTransactionIsolation(); void setAutoCommit(boolean autocommit); boolean getAutoCommit(); void commit(); void rollback(); void setAutoClose(boolean autoClose); boolean getAutoClose(); void setTransactionIsolation(int isolationlevel); This method sets the transaction isolation level. The possible JDBC transaction isolation levels are the following:
Example // transactions import java.sql.*; class SimpleExample { public static void main(String args[]) { String url = jdbc:odbc:mysource; try { Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); Connection myConnection = DriverManager.getConnection(url, javauser, hotjava); Statement firstStmt = myConnection.createStatement(); Statement secondStmt = myConnection.createStatement(); myConnection.setTransactionIsolation( Connection.TRANSACTION_SERIALIZABLE); myConnection.setAutoCommit(false); firstStmt.executeUpdate( DELETE emp_messages WHERE id IN (SELECT id FROM employees WHERE name = Jones)); firstStmt.close(); secondStmt.executeUpdate( DELETE employees WHERE name = Jones); secondStmt.close(); myConnection.commit(); myConnection.setTransactionIsolation( Connection.TRANSACTION_NONE); myConnection.close(); } catch(java.lang.Exception ex) { ex.printStackTrace(); } } }
|
![]() |
|