如何在@Transactional"READ_UNCOMMITTED"中设置隔离级别.我正在使用EclipseLink 2.5.1-RC1 [英] How to set isolation level in @Transactional "READ_UNCOMMITTED". I am using EclipseLink 2.5.1-RC1

查看:352
本文介绍了如何在@Transactional"READ_UNCOMMITTED"中设置隔离级别.我正在使用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(传播=传播.REQUIRES_NEW)

@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(传播=传播.REQUIRES_NEW,隔离=隔离.READ_UNCOMMITTED)

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

这将引发异常-InvalidIsolationLevelException,说标准JPA不支持自定义隔离级别-为您的JPA实现使用特殊的JpaDialect".

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

能帮我实现JpaDialect吗?我正在使用Eclipse Link 2.5.1.

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.

推荐答案

  • 在JPA中,您可以尝试类似的方式,但是对于如何在EclipseLink/Spring中实现相似性并不确定.

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

      如果您不想进行更改,则可能需要实现JpaDialect覆盖方法以适应事务中隔离级别的更改.

      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.

      您可以参考此处,其中描述了实现Hibernate,可以尝试类似的EclipseLink.

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

      这篇关于如何在@Transactional"READ_UNCOMMITTED"中设置隔离级别.我正在使用EclipseLink 2.5.1-RC1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆