Search code examples
javatransactions

Why is TRANSACTION_READ_COMMITTED the default in Java?


The transaction tutorial explains in the section Using Transactions to Preserve Data Integrity:

The default transaction isolation level depends on your DBMS. For example, for Java DB, it is TRANSACTION_READ_COMMITTED.

For my Postgresql database it is the same. But this does not seem to be the best, because only TRANSACTION_SERIALIZABLE prevents all inconsistencies:

Isolation Level             │Transactions │Dirty Reads   │Non-Repeatable Reads│Phantom Reads
=============================================================================================
TRANSACTION_NONE            │Not supported│Not applicable│Not applicable      │Not applicable
TRANSACTION_READ_COMMITTED  │Supported    │Prevented     │Allowed             │Allowed
TRANSACTION_READ_UNCOMMITTED│Supported    │Allowed       │Allowed             │Allowed
TRANSACTION_REPEATABLE_READ │Supported    │Prevented     │Prevented           │Allowed
TRANSACTION_SERIALIZABLE    │Supported    │Prevented     │Prevented           │Prevented

Why is not the best option the default?


Solution

  • There is an inverse relationship between transaction security and performance.
    If you choose the best isolation level, you also get the worst performance.
    You have to make a trade-off between the two.

    See this benchmark for more details.