CMPUT 391 Database Management Systems & Concurrency Control - - CMPUT 391 Database Management Systems Department of Computing Science University of Alberta
Outline Transaction Isolation & Consistency Isolation Levels (SQL92 & Oracle) Read committed Serializable Read-only Oracle Locks In JDBC CMPUT 391 Database Management Systems 2
What Is A Transaction? A transaction maintains consistency of the database and the correspondence between the state of the database and the real-world ACID (Atomicity, Consistency, Isolation, and Durability) essential properties of transactions CMPUT 391 Database Management Systems 3
Isolation & Consistency Preventable Phenomena - Dirty Read - Unrepeatable Read - Phantom reads Tradeoff between serializability & application throughput CMPUT 391 Database Management Systems 4
Transaction Support in SQL-92 Isolation levels Isolation Level Dirty Read Unrepeatable Read Phantom Read Read uncommitted Maybe Maybe Maybe Read committed No Maybe Maybe Repeatable Reads No No Maybe Serializable No No No CMPUT 391 Database Management Systems 5
Oracle Transaction Isolation 3 Isolation Levels Read committed (default) Unrepeatable and phantom read possible No dirty read Serializable No dirty, unrepeatable and phantom reads Read-only Do not allow INSERT, UPDATE, and DELETE statements CMPUT 391 Database Management Systems 6
Setting Oracle Isolation Level Application developers choose appropriate isolation levels For different transactions Depending on the application and workload At the beginning of a transaction CMPUT 391 Database Management Systems 7
Oracle Syntax SET TRANSACTION ISOLATION LEVEL READ COMMITTED; SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; SET TRANSACTION ISOLATION LEVEL READ ONLY; CMPUT 391 Database Management Systems 8
Read Committed Isolation Used where few transactions are likely to conflict Permits unrepeatable and phantom reads Provides higher potential throughput CMPUT 391 Database Management Systems 9
Serializable Isolation Used with large databases and short transactions that update only a few rows where the chance that two concurrent transactions will modify the same rows is relatively low where relatively long-running transactions are primarily read-only CMPUT 391 Database Management Systems 10
Serializable Isolation When a serializable transaction fails, the application has following options commit the work executed to that point Execute additional (but different) statements Roll back the entire transaction CMPUT 391 Database Management Systems 11
Oracle Locks Two modes of locking Exclusive lock mode Share lock mode Automatically obtains necessary locks and usually requires no user action But does allow the user to lock data manually CMPUT 391 Database Management Systems 12
In JDBC By default, after each SQL statement is executed the changes are automatically committed to the database In JDBC, one can turn auto-commit off to group two or more statements together into a transaction Connection.setAutoCommit(false) Guarantees that inserts / updates / deletes done inside a transaction are all or none (Atomicity) Call commit to permanently record the changes to the database after executing a group of statements Connection.commit() Call rollback if an error occurs Connection.rollback() CMPUT 391 Database Management Systems 13
: Connection Methods getautocommit() / setautocommit() By default, a connection is set to auto-commit Retrieves or sets the auto-commit mode commit() Force all changes since the last call to commit to become permanent Any database locks currently held by this Connection object are released rollback() Drops all changes since the previous call to commit Releases any database locks held by this Connection object CMPUT 391 Database Management Systems 14
: Java Code Connection connection = DriverManager.getConnection( url, username, passwd); connection.setautocommit(false); try { statement.executeupdate(...); statement.executeupdate(...); connection.commit(); } catch (SQLException sqle) { try { connection.rollback(); } catch (SQLException sqle1) { // report problem } } finally { try { connection.close(); } catch (SQLException sqle2) { } } either both or none will be executed CMPUT 391 Database Management Systems 15
Exception Handling- SQL Exceptions Nearly every JDBC method can throw a SQLException in response to a data access error If more than one error occurs, they are chained together SQL exceptions contain: Description of the error: getmessage() The SQLState (Open Group SQL specification) identifying the exception: getsqlstate() A vendor-specific integer, error code: geterrorcode() A chain to the next SQLException: getnextexception() CMPUT 391 Database Management Systems 16
SQL Exceptions Example try { // JDBC statements } catch (SQLException sqle) { while (sqle!= null) { System.out.println( Message: + sqle.getmessage()); System.out.println( SQLState: + sqle.getsqlstate()); System.out.println( Vendor Error: + sqle.geterrorcode()); sqle.printstacktrace(system.out); sqle = sqle.getnextexception(); } } CMPUT 391 Database Management Systems 17
References Oracle9i Database Concepts Release 2 (9.2) http://downloadwest.oracle.com/docs/cd/b10501_01/se rver.920/a96524/c21cnsis.htm Using in JDBC http://java.sun.com/docs/books/tutorial/jdb c/basics/transactions.html CMPUT 391 Database Management Systems 18