一尘不染

Hibernate, spring, JPS & isolation -不支持自定义隔离

spring

我一直在尝试:

@Transactional(isolation=Isolation.SERIALIZABLE, 
               rollbackFor={Exception.class}, 
               propagation=Propagation.REQUIRES_NEW)

关于我的服务方式,但spring抱怨说:

Standard JPA does not support custom isolation levels - use a special JpaDialect

我该如何解决?


阅读 428

收藏
2020-04-16

共1个答案

一尘不染

JPA不支持自定义隔离级别。你可以扩展HibernateJpaDialect类并覆盖与连接有关的方法,以便可以在Connection

这是我写的但尚未测试的东西:

public class HibernateExtendedJpaDialect extends HibernateJpaDialect {

    @Override
    public Object beginTransaction(EntityManager entityManager,
            TransactionDefinition definition) throws PersistenceException,
            SQLException, TransactionException {

        Session session = (Session) entityManager.getDelegate();
        DataSourceUtils.prepareConnectionForTransaction(session.connection(), definition);

        entityManager.getTransaction().begin();

        return prepareTransaction(entityManager, definition.isReadOnly(), definition.getName());
    }

}

然后将其定义为你的属性EntityManagerFactory:

<property name="jpaDialect">
    <bean class="com.company.util.HibernateExtendedJpaDialect" />
</property>
2020-04-16