Extending locking options with EclipseLink JPQL
JPA specification allows only a common subset of entity locking options usually implemented across the databases. When posed with a need to choose a more specific locking option, you have to fall back to the native queries. But when you already have some complex JPQL code, is there no way to employ vendor-specific locking?
EclipseLink implements database-specific behavior under the org
Let's see how EclipseLink handles this. We will try to extend the locking with Oracle-specific SELECT FOR UPDATE OF
and
SKIP LOCKED
clauses.
JPQL SELECT FOR UPDATE OF / SKIP LOCKED
To get into the implementation of the query, we can unwrap it onto the internal interface. All read queries using objects in the EclipseLink use the ObjectLevelReadQuery class. However, note one thing about the EclipseLink internals before you start fiddling with the underlying query. Queries can be shared. To prevent side effects, clone the read query and update the reference in the wrapper.
import org.eclipse.persistence.expressions.ExpressionBuilder;
import org.eclipse.persistence.internal.expressions.ForUpdateOfClause;
import org.eclipse.persistence.internal.jpa.QueryImpl;
import org.eclipse.persistence.queries.ObjectLevelReadQuery;
import javax.persistence.Query;
public class OracleForUpdateOfClause extends ForUpdateOfClause {
//...
private ExpressionBuilder clone(Query query) {
QueryImpl queryImpl = query.unwrap(QueryImpl.class);
ObjectLevelReadQuery objectLevelReadQuery = (ObjectLevelReadQuery) query
.unwrap(ObjectLevelReadQuery.class).clone();
queryImpl.setDatabaseQuery(objectLevelReadQuery);
objectLevelReadQuery.setLockingClause(this);
return objectLevelReadQuery.getExpressionBuilder();
}
}
The interface of ObjectLevelReadQuery provides a way to insert the locking clause. This clause is a kind of a builder interface that prints the locking part. By default, the ForUpdateClause is used here. This implementation supports the standard lock, a wait timeout, and a no-wait clause.
Furthermore, we have the ForUpdateOfClause. This one, however, does not support the wait and no-wait clauses but
implements the LOCK FOR <column>
clause. By extending this class, you can add support also for the SKIP LOCKED
clause.
import org.eclipse.persistence.internal.expressions.ExpressionSQLPrinter;
import org.eclipse.persistence.internal.expressions.ForUpdateOfClause;
import org.eclipse.persistence.internal.expressions.SQLSelectStatement;
import org.eclipse.persistence.queries.ObjectBuildingQuery;
import javax.persistence.Query;
public class OracleForUpdateOfClause extends ForUpdateOfClause {
public static final short LOCK_SKIP_LOCKED = Short.MAX_VALUE;
private Integer waitTimeout;
public OracleForUpdateOfClause() {
}
public OracleForUpdateOfClause(short lockMode) {
setLockMode(lockMode);
}
public OracleForUpdateOfClause(Integer waitTimeout) {
this.waitTimeout = waitTimeout;
setLockMode(ObjectBuildingQuery.LOCK);
}
public void printSQL(ExpressionSQLPrinter printer, SQLSelectStatement statement) {
super.printSQL(printer, statement);
if (getLockMode() == ObjectBuildingQuery.LOCK && waitTimeout != null) {
printer.printString(" WAIT " + waitTimeout);
} else if (getLockMode() == LOCK_SKIP_LOCKED) {
printer.printString(" SKIP LOCKED");
}
}
//...
}
To reference the correct fields from particular relation of the query, I suggest using the expressions prepared in the query builder. This reduces the effort of finding out the correct table alias for the resulting query. Now the last thing is to add the clause before the query execution.
import org.eclipse.persistence.expressions.Expression;
import org.eclipse.persistence.expressions.ExpressionBuilder;
import org.eclipse.persistence.internal.expressions.ForUpdateOfClause;
import javax.persistence.Query;
public class OracleForUpdateOfClause extends ForUpdateOfClause {
//...
public void selectQueryForUpdateOf(Query query) {
ExpressionBuilder expressionBuilder = clone(query);
getLockedExpressions().add(expressionBuilder);
}
public void selectQueryForUpdateOf(Query query, String ofRelation) {
ExpressionBuilder expressionBuilder = clone(query);
for (Expression expression : expressionBuilder.derivedExpressions) {
if (ofRelation.equals(expression.getName())) {
getLockedExpressions().add(expression);
break;
}
}
}
//...
}
Finally, if we test this behavior with the logging turned on, you will see the new locking clauses. You can now compare this with locking of all selected rows:
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.LockModeType;
import javax.persistence.PersistenceException;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import java.util.function.Consumer;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertTrue;
@SpringBootTest
public class SelectForUpdateOfTest {
@PersistenceUnit
private EntityManagerFactory entityManagerFactory;
@Test
public void testSelectForUpdate() {
invokeInTransaction((entityManager) -> {
entityManager.createQuery("SELECT s FROM Stock s JOIN FETCH s.product " +
"WHERE s.product.id = 1")
.setLockMode(LockModeType.PESSIMISTIC_WRITE)
.getSingleResult();
// SELECT t1.ID, t1.TOTAL, t1.product_id, t0.ID, t0.NAME
// FROM PRODUCT t0, STOCK t1
// WHERE ((t1.product_id = ?) AND (t0.ID = t1.product_id)) FOR UPDATE
PersistenceException exception = Assertions.assertThrows(PersistenceException.class,
() -> invokeInTransaction((secondEntityManager) -> {
Query query = secondEntityManager.createQuery("SELECT p FROM Product p " +
"WHERE p.id = 1");
OracleForUpdateOfClause clause = new OracleForUpdateOfClause(5);
clause.selectQueryForUpdateOf(query);
query.getSingleResult();
// SELECT ID, NAME FROM PRODUCT WHERE (ID = ?) FOR UPDATE OF ID WAIT 5
}));
assertThat(exception.getMessage(),
containsString("ORA-30006: resource busy; acquire with WAIT timeout expired"));
});
}
private void invokeInTransaction(Consumer<EntityManager> transaction) {
EntityManager em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
transaction.accept(em);
em.getTransaction().commit();
}
}
Next, for the same joined result, you can lock the rows from one table in one query and rows from the other table in another query without any contention.
The SKIP LOCKED
part also works fine:
//...
@SpringBootTest
public class SelectForUpdateOfTest {
//...
@Test
public void testSelectForUpdate_LockDifferentJoinedTables() {
invokeInTransaction((entityManager) -> {
Query query = entityManager.createQuery(
"SELECT s FROM Stock s JOIN FETCH s.product WHERE s.product.id = 1"
);
OracleForUpdateOfClause clause = new OracleForUpdateOfClause(5);
clause.selectQueryForUpdateOf(query, "product");
query.getSingleResult();
// SELECT t1.ID, t1.TOTAL, t1.product_id, t0.ID, t0.NAME
// FROM PRODUCT t0, STOCK t1
// WHERE ((t1.product_id = ?) AND (t0.ID = t1.product_id)) FOR UPDATE OF t0.ID WAIT 5
invokeInTransaction((secondEntityManager) -> {
Query secondQuery = secondEntityManager.createQuery(
"SELECT s FROM Stock s JOIN FETCH s.product WHERE s.product.id = 1"
);
OracleForUpdateOfClause secondClause = new OracleForUpdateOfClause(5);
secondClause.selectQueryForUpdateOf(secondQuery);
secondQuery.getSingleResult();
// SELECT t1.ID, t1.TOTAL, t1.product_id, t0.ID, t0.NAME
// FROM PRODUCT t0, STOCK t1
// WHERE ((t1.product_id = ?) AND (t0.ID = t1.product_id))
// FOR UPDATE OF t1.ID WAIT 5
});
invokeInTransaction((secondEntityManager) -> {
Query secondQuery = secondEntityManager.createQuery(
"SELECT s FROM Stock s JOIN FETCH s.product WHERE s.product.id = 1"
);
OracleForUpdateOfClause secondClause =
new OracleForUpdateOfClause(OracleForUpdateOfClause.LOCK_SKIP_LOCKED);
secondClause.selectQueryForUpdateOf(secondQuery, "product");
assertTrue(secondQuery.getResultList().isEmpty());
// SELECT t1.ID, t1.TOTAL, t1.product_id, t0.ID, t0.NAME
// FROM PRODUCT t0, STOCK t1
// WHERE ((t1.product_id = ?) AND (t0.ID = t1.product_id))
// FOR UPDATE OF t0.ID SKIP LOCKED
});
});
}
}