我怀疑这是令人尴尬的,我以一种可怕的方式做错了,但请耐心等待我.
我有一个Spring应用程序与Spring管理的事务.
它使用EclipseLink JPA.
我有一个方法,它执行findByNativeQuery()后跟merge().我需要在真正的SERIAL事务隔离级别中实现这一点.
我尝试添加
@Transactional(隔离= Isolation.SERIALIZABLE)
这不起作用,因为org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect #beaTransaction
不支持任何事务隔离级别,但默认值.
那么我尝试使用ElcipseLink的UnitOfWork内部并开始/编写我自己的事务,但后来我收到一个错误:
"java.lang.IllegalStateException : Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead
这当然有道理……但我该怎么办?
解决方法
我试过这个,但我不完全确定解决方案.我从
this blog获取了代码并将其改编为EclipseLink.这是代码:
package com.byteslounge.spring.tx.dialect; import java.sql.sqlException; import javax.persistence.EntityManager; import javax.persistence.PersistenceException; import org.eclipse.persistence.sessions.UnitOfWork; import org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect; import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.TransactionException; public class CustomEclipseLinkJpaDialect extends EclipseLinkJpaDialect { private static final long serialVersionUID = 1L; private boolean lazyDatabaseTransaction = false; @Override public void setLazyDatabaseTransaction(boolean lazyDatabaseTransaction) { this.lazyDatabaseTransaction = lazyDatabaseTransaction; } @Override public Object beginTransaction(final EntityManager entityManager,final TransactionDefinition definition) throws PersistenceException,sqlException,TransactionException { UnitOfWork uow = (UnitOfWork) getSession(entityManager); uow.getLogin().setTransactionIsolation(definition.getIsolationLevel()); entityManager.getTransaction().begin(); if (!definition.isReadOnly() && !lazyDatabaseTransaction) { uow.beginEarlyTransaction(); } return null; } }
我发现事务启动时会记录SERIALIZABLE隔离,但需要对其进行正确测试以确认其有效.