JDBC transaction level
Transactions are separated by locks to solve the problem related to "multiple threads request the same data. Most mainstream databases support different types of locks. Therefore, JDBC APIs support different types of transactions, which are assigned or determined by the Connection object. The following transaction levels can be obtained in the jdbc api:
TRANSACTION_NONE indicates that transactions are not supported.
TRANSACTION_READ_UNCOMMITTED indicates that the change of another transaction can be seen before the transaction is committed. In this way, dirty reads, non-repeated reads, and virtual reads are allowed.
TRANSACTION_READ_COMMITTED indicates that uncommitted data cannot be read. This level still allows non-repeated reads and virtual reads.
TRANSACTION_REPEATABLE_READ indicates that the transaction can read the same data again without failure, but the virtual read will still appear.
TRANSACTION_SERIALIZABLE is the highest transaction level, which prevents dirty reads, non-repeated reads, and virtual reads.
You may wonder why not all transactions run in the TRANSACTION_SERIALIZABLE mode to ensure the highest degree of data integrity? The problem is that the higher the transaction protection level, the higher the performance loss, similar to the problem related to processing multi-threaded programming.
Assuming that your database and JDBC driver support this feature, you can explicitly set the transaction level you want to assign a Connection object:
Con. setTransactionLevel (TRANSACTION_SERIALIZABLE );
You can also determine the level of the current transaction:
If (con. getTransactionLevel () = TRANSACTION_SERIALIZABLE)
System. out. println ("Highest Transaction Level in operation .");