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

只愿长相守 提交于 2019-12-23 12:35:37

问题


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.


回答1:


  • 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.



来源:https://stackoverflow.com/questions/28337106/how-to-set-isolation-level-in-transactional-read-uncommitted-i-am-using-ecli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!