Transaction isolation levels supported in JDBC

Transaction isolation level is a value that determines the level at which inconsistent data is allowed in a transaction, that is, the degree of isolation of one transaction from another. A higher level of isolation improves the accuracy of the data, but it can reduce the number of concurrent transactions. On the other hand, a lower level of isolation allows more concurrent transactions, but reduces the accuracy of the data.

During the use of transactions, to ensure data integrity, the DBMS uses locks to block other access to the data involved in the transaction. Such locks are necessary to prevent:

  • "dirty" read - reading data, added or modified by a transaction, which later will not be confirmed (rolled back);
  • non-repeatable read - when re-reading within one transaction, previously read data is changed;
  • phantom reads - a situation when, when reading repeatedly within the same transaction, the same sample gives different sets of rows.

Transaction isolation levels are defined as constants of the java.sql.Connection interface:

  • TRANSACTION_NONE - the driver does not support transactions;
  • TRANSACTION_READ_UNCOMMITTED - allows transactions to see unsaved data changes: allows dirty, unchecked and phantom reads;
  • TRANSACTION_READ_COMMITTED - any change made in a transaction is not visible outside of it until it is saved: prevents dirty reads, but allows unchecked and phantom reads;
  • TRANSACTION_REPEATABLE_READ - prohibits dirty and unchecked, phantom reads are allowed;
  • TRANSACTION_SERIALIZABLE - Dirty, unchecked, and phantom reads are prohibited.

The database server may not support all isolation levels. The java.sql.DatabaseMetaData interface provides information about the transaction isolation levels supported by a given DBMS.

The transaction isolation level used by the DBMS can be set using the setTransactionIsolation() method of the java.sql.Connection object. The getTransactionIsolation() method will help you get information about the applied isolation level.


Read also:


Comments

Popular posts from this blog

Methods for reading XML in Java

XML, well-formed XML and valid XML

ArrayList and LinkedList in Java, memory usage and speed