Java – spring managed transaction, eclipse link JPA, custom isolation level

I doubt it's embarrassing. I made a mistake in a terrible way, but please wait for me patiently

I have a spring application with spring managed transactions It uses eclipse link JPA I have a method that executes findbynativequery () followed by merge () I need to do this at the true serial transaction isolation level I tried to add @ transactional (isolation = isolation. Serializable)

This doesn't work because org springframework. orm. jpa. vendor. Eclipse linkjpadialect #beatransaction does not support any transaction isolation level, but the default value is Then I tried to use unitofwork inside elcipselink and start / write my own transaction, but then I received an error:

"java.lang.IllegalStateException : Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead

Of course that makes sense... But what should I do?

Solution

I've tried this, but I'm not entirely sure about the solution I got the code from this blog and adapted it into eclipse link This is the code:

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;
    }
}

I found that serializable isolation is recorded when the transaction starts, but it needs to be properly tested to confirm its effectiveness

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>