J2EE / JPA:控制事务隔离

时间:2014-01-28 19:39:17

标签: java java-ee jpa transactions glassfish

我想在J2EE环境中为同一个持久化上下文使用不同的事务隔离级别。例如:

UserTransaction ut=...;
EntityManagerFactory emf=...;
EntityManager em=emf.createEntityManager;

ut.begin();
em.joinTransaction(); => use RepeatableRead isolation here
...
ut.commit();

ut.begin();
em.joinTransaction(); => use Serializable isolation here
...
ut.commit();

我发现无法实现这一目标。当EM首先获取数据库连接时,它将从池中获取并立即获得当前的XA事务。之后,再也无法更改该数据库事务的事务隔离。

任何想法?

1 个答案:

答案 0 :(得分:0)

我正在使用Glassfish / EclipseLink / MySQL。我找到的解决方案是自定义MySQL DataSource以返回XAResource周围的包装器,该包装器在启动XA事务之前设置事务隔离。这样,可以在开始事务之前设置所需的事务隔离。

public class CustomDataSource extends MysqlXADataSource {

private static ThreadLocal<Integer> isolation=new ThreadLocal<Integer>(){
    protected Integer initialValue() {
        return Connection.TRANSACTION_REPEATABLE_READ;
    };
};

public static void setTransactionIsolation(int i){
    isolation.set(i);
}

private static class XAConnectionWrapper implements XAConnection {

    private XAConnection delegate;

    public XAConnectionWrapper(XAConnection delegate) {
        this.delegate = delegate;
    }

    ... delegate methods

    @Override
    public XAResource getXAResource() throws SQLException {
        return new XAResourceWrapper(delegate.getXAResource());
    }

    @Override
    public boolean equals(Object obj) {
        return delegate.equals(obj);
    }

    @Override
    public int hashCode() {
        return delegate.hashCode();
    }
}

private static class XAResourceWrapper implements XAResource {
    private XAResource delegate;
    private Field field;

    ... delegate methods ...

    public void start(Xid xid, int flags) throws XAException {
        try {
            ConnectionImpl connection = (ConnectionImpl) field
                    .get(delegate);
            connection
                    .setTransactionIsolation(isolation.get());
        } catch (IllegalArgumentException | IllegalAccessException
                | SQLException e) {
            throw new RuntimeException(e);
        }
        delegate.start(xid, flags);
    }

    public XAResourceWrapper(XAResource delegate) {
        try {
            field = MysqlXAConnection.class
                    .getDeclaredField("underlyingConnection");
            field.setAccessible(true);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        }
        this.delegate = delegate;
    }

    @Override
    public boolean equals(Object obj) {
        return delegate.equals(obj);
    }

    @Override
    public int hashCode() {
        return delegate.hashCode();
    }
}

@Override
public XAConnection getXAConnection() throws SQLException {
    XAConnection conn = super.getXAConnection();

    return new XAConnectionWrapper(conn);
}

@Override
public XAConnection getXAConnection(String u, String p) throws SQLException {
    return new XAConnectionWrapper(super.getXAConnection(u, p));
}
}