Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set isolation level in @Transactional "READ_UNCOMMITTED". I am using EclipseLink 2.5.1-RC1

I have a requirement to start new Transaction within an ongoing Transaction so that an exception in 2nd transaction will rollback only new transaction not the old one.

This I am doing by setting propagation attribute in 2nd transaction like this:

@Transactional(propagation = Propagation.REQUIRES_NEW)

This created a new Transaction, but the new Transaction needs to read some uncommitted data of the first transaction (dirty read), and also update that data. This I am trying to do by setting isolation attribute as :

@Transactional(propagation = Propagation.REQUIRES_NEW, isolation=Isolation.READ_UNCOMMITTED)

This throws an Exception - InvalidIsolationLevelException, saying "Standard JPA does not support custom isolation levels - use a special JpaDialect for your JPA implementation".

Can any help me to implement JpaDialect? I am using Eclipse Link 2.5.1 .

Or can I some how close the first transaction before starting a new transaction? Since First transaction is closed, the Second will have no problem reading the data committed by First Transaction.

like image 438
Noor Khan Avatar asked Nov 09 '22 19:11

Noor Khan


1 Answers

  • In JPA you can try somehting like this, not much certain on how to achieve similar in EclipseLink/Spring.

    But the overall concept might remain same, get the underlying database connection & set appropriate isolation level.

    java.sql.Connection connection = entityManager.unwrap(java.sql.Connection.class);
    connection.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); 
    //-- Or with some other constant something like Isolation.READ_UNCOMMITTED
    

    Afterwards, you might also want to reset the isolation level back to the default.

  • If you don't want to make changes, then you might need to implement JpaDialect overriding methods to accommodate the change for isolation level in transaction.

    You can refer here which describes implementation for Hibernate, can try similar for EclipseLink.

like image 56
Nayan Wadekar Avatar answered Jan 04 '23 03:01

Nayan Wadekar