This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 6fb952f4fa6e CAMEL-27057: camel-jpa - skipLockedEntity skips a locked
entity with SKIP LOCKED on Hibernate, also on PostgreSQL (#30209)
6fb952f4fa6e is described below
commit 6fb952f4fa6ea6755916fa8fef091779bfca565f
Author: Federico Mariani <[email protected]>
AuthorDate: Mon Nov 16 22:54:04 2026 +0100
CAMEL-27057: camel-jpa - skipLockedEntity skips a locked entity with SKIP
LOCKED on Hibernate, also on PostgreSQL (#30209)
Co-authored-by: Claude <[email protected]>
---
.../org/apache/camel/catalog/components/jpa.json | 4 +-
.../apache/camel/catalog/docs/jpa-component.adoc | 32 ++-
.../org/apache/camel/component/jpa/jpa.json | 4 +-
.../camel-jpa/src/main/docs/jpa-component.adoc | 32 ++-
.../apache/camel/component/jpa/JpaConsumer.java | 127 ++++++++-
.../apache/camel/component/jpa/JpaEndpoint.java | 14 +-
.../jpa/JpaConsumerSkipLockedEntityIT.java | 220 +++++++++++++++
.../jpa/JpaConsumerSkipLockedEntityTest.java | 311 +++++++++++++++++++++
.../apache/camel/component/jpa/SkipLockedItem.java | 37 +++
.../component/jpa/SkipLockedVersionedItem.java | 41 +++
.../camel/component/jpa/skip-locked-item-orm.xml | 41 +++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 17 ++
.../endpoint/dsl/JpaEndpointBuilderFactory.java | 40 ++-
13 files changed, 884 insertions(+), 36 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/jpa.json
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/jpa.json
index 84c5a6a6ecff..5c32d1b5a210 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/jpa.json
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/jpa.json
@@ -60,11 +60,11 @@
"consumeLockEntity": { "index": 10, "kind": "parameter", "displayName":
"Consume Lock Entity", "group": "consumer", "label": "consumer", "required":
false, "type": "boolean", "javaType": "boolean", "deprecated": false,
"autowired": false, "secret": false, "defaultValue": true, "description":
"Specifies whether to set an exclusive lock on each entity bean while
processing the results from polling." },
"deleteHandler": { "index": 11, "kind": "parameter", "displayName":
"Delete Handler", "group": "consumer", "label": "consumer", "required": false,
"type": "object", "javaType":
"org.apache.camel.component.jpa.DeleteHandler<java.lang.Object>", "deprecated":
false, "autowired": false, "secret": false, "description": "To use a custom
DeleteHandler to delete the row after the consumer is done processing the
exchange" },
"lockModeType": { "index": 12, "kind": "parameter", "displayName": "Lock
Mode Type", "group": "consumer", "label": "consumer", "required": false,
"type": "enum", "javaType": "jakarta.persistence.LockModeType", "enum": [
"READ", "WRITE", "OPTIMISTIC", "OPTIMISTIC_FORCE_INCREMENT",
"PESSIMISTIC_READ", "PESSIMISTIC_WRITE", "PESSIMISTIC_FORCE_INCREMENT", "NONE"
], "deprecated": false, "autowired": false, "secret": false, "defaultValue":
"PESSIMISTIC_WRITE", "description": "To configure t [...]
- "lockTimeout": { "index": 13, "kind": "parameter", "displayName": "Lock
Timeout", "group": "consumer", "label": "consumer", "required": false, "type":
"duration", "javaType": "long", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": "0", "description": "How long the consumer
waits for the lock of an entity (see consumeLockEntity) that another
transaction holds. It is given to the JPA provider as the
jakarta.persistence.lock.timeout hint, which the provider app [...]
+ "lockTimeout": { "index": 13, "kind": "parameter", "displayName": "Lock
Timeout", "group": "consumer", "label": "consumer", "required": false, "type":
"duration", "javaType": "long", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": "0", "description": "How long the consumer
waits for the lock of an entity (see consumeLockEntity) that another
transaction holds. It is given to the JPA provider as the
jakarta.persistence.lock.timeout hint, which the provider app [...]
"maxMessagesPerPoll": { "index": 14, "kind": "parameter", "displayName":
"Max Messages Per Poll", "group": "consumer", "label": "consumer", "required":
false, "type": "integer", "javaType": "int", "deprecated": false, "autowired":
false, "secret": false, "description": "An integer value to define the maximum
number of messages to gather per poll. By default, no maximum is set. It can be
used to avoid polling many thousands of messages when starting up the server.
Set a value of 0 or [...]
"preDeleteHandler": { "index": 15, "kind": "parameter", "displayName":
"Pre Delete Handler", "group": "consumer", "label": "consumer", "required":
false, "type": "object", "javaType":
"org.apache.camel.component.jpa.DeleteHandler<java.lang.Object>", "deprecated":
false, "autowired": false, "secret": false, "description": "To use a custom
Pre-DeleteHandler to delete the row after the consumer has read the entity." },
"sendEmptyMessageWhenIdle": { "index": 16, "kind": "parameter",
"displayName": "Send Empty Message When Idle", "group": "consumer", "label":
"consumer", "required": false, "type": "boolean", "javaType": "boolean",
"deprecated": false, "autowired": false, "secret": false, "defaultValue":
false, "description": "If the polling consumer did not poll any files, you can
enable this option to send an empty message (no body) instead." },
- "skipLockedEntity": { "index": 17, "kind": "parameter", "displayName":
"Skip Locked Entity", "group": "consumer", "label": "consumer", "required":
false, "type": "boolean", "javaType": "boolean", "deprecated": false,
"autowired": false, "secret": false, "defaultValue": false, "description": "To
configure whether to use NOWAIT on lock and silently skip the entity." },
+ "skipLockedEntity": { "index": 17, "kind": "parameter", "displayName":
"Skip Locked Entity", "group": "consumer", "label": "consumer", "required":
false, "type": "boolean", "javaType": "boolean", "deprecated": false,
"autowired": false, "secret": false, "defaultValue": false, "description": "To
configure whether to skip an entity that another transaction has locked,
instead of waiting for its lock, so that several consumers can share a table.
With Hibernate and a pessimistic lockMode [...]
"transacted": { "index": 18, "kind": "parameter", "displayName":
"Transacted", "group": "consumer", "label": "consumer", "required": false,
"type": "boolean", "javaType": "boolean", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": false, "description": "Whether to run
the consumer in transacted mode, by which all messages will either commit or
rollback, when the entire batch has been processed. The default behavior
(false) is to commit all the previously succ [...]
"bridgeErrorHandler": { "index": 19, "kind": "parameter", "displayName":
"Bridge Error Handler", "group": "consumer (advanced)", "label":
"consumer,advanced", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "Allows for bridging the consumer to the
Camel routing Error Handler, which mean any exceptions (if possible) occurred
while the Camel consumer is trying to pickup incoming [...]
"exceptionHandler": { "index": 20, "kind": "parameter", "displayName":
"Exception Handler", "group": "consumer (advanced)", "label":
"consumer,advanced", "required": false, "type": "object", "javaType":
"org.apache.camel.spi.ExceptionHandler", "optionalPrefix": "consumer.",
"deprecated": false, "autowired": false, "secret": false, "description": "To
let the consumer use a custom ExceptionHandler. Notice if the option
bridgeErrorHandler is enabled then this option is not in use. By de [...]
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/jpa-component.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/jpa-component.adoc
index 891d1dd8ac65..e43bb2ae3a04 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/jpa-component.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/jpa-component.adoc
@@ -131,11 +131,11 @@ session `idle in transaction`, with its locks, until TCP
keepalive gives up on t
defaults of Linux. A socket timeout of the JDBC driver ends the poll, but not
its wait on the database: each poll leaves
one more session waiting for the lock.
-The consumer waits for at most `lockTimeout`, or not at all with
`skipLockedEntity=true`. The JPA provider applies
-`lockTimeout` if it can with the database: Hibernate 7.4, for one, sets
`lock_timeout` for the lock on PostgreSQL, so the
-database itself cancels the wait.
+The consumer waits for at most `lockTimeout`, or not at all with
`skipLockedEntity=true` (see below). The JPA
+provider applies `lockTimeout` if it can with the database: Hibernate 7.4, for
one, sets `lock_timeout` for the lock on
+PostgreSQL, so the database itself cancels the wait.
-What happens to a lock that the consumer does not get depends on the database:
+What happens to a lock that the consumer does not get in time depends on the
database:
* When the transaction can go on, the JPA provider reports a
`LockTimeoutException`: the consumer skips the entity and
routes the next one.
@@ -151,6 +151,30 @@ does not end, set a timeout on the database, such as
`idle_in_transaction_sessio
a session. Set it above the time a poll takes to route its entities: while the
route runs, the session of the poll is
idle in transaction too.
+==== Skipping the locked entities
+
+With `skipLockedEntity=true`, the consumer skips an entity that another
transaction holds, instead of waiting for its
+lock, and routes the next one. This lets several consumers share the same
table, with a `lockModeType` that locks
+the row exclusively, `PESSIMISTIC_WRITE` (the default) or
`PESSIMISTIC_FORCE_INCREMENT`: each consumer skips the
+entities that the others are consuming. `PESSIMISTIC_READ` does not keep the
consumers apart: it takes a shared lock
+(`FOR SHARE` on PostgreSQL), which another consumer gets too, so two consumers
can route the same entity.
+
+With Hibernate, and a pessimistic `lockModeType` (`PESSIMISTIC_WRITE`,
`PESSIMISTIC_READ` or
+`PESSIMISTIC_FORCE_INCREMENT`), the consumer locks the entity with a select of
the entity with `SKIP LOCKED` (with
+`PESSIMISTIC_WRITE` on PostgreSQL, `SELECT ... FOR NO KEY UPDATE OF ... SKIP
LOCKED`), where the database supports it:
+PostgreSQL, MySQL, Oracle and H2, among others. The select returns no row for
a locked entity, which is no error, so the poll goes on and
+commits the entities it consumed, also on PostgreSQL. An entity that another
consumer has deleted since the poll listed
+it (`consumeDelete=true`, the default) is skipped too. An entity that another
consumer has only updated
+(`consumeDelete=false`) is not skipped: its row is still there, and the select
returns it once the transaction of the
+other consumer has ended.
+
+Otherwise, with another JPA provider or an optimistic `lockModeType`, the
consumer gives the lock the
+`jakarta.persistence.lock.timeout` hint `0` (`NOWAIT`), and a lock that it
does not get is handled as one that times out
+(see above): on PostgreSQL the poll is rolled back. OpenJPA 4.1 does not apply
`NOWAIT`: it applies the lock timeout as
+the query timeout of JDBC, where `0` sets no timeout, so on PostgreSQL its
lock waits for the other transaction, as
+without `skipLockedEntity`. With OpenJPA, an entity that another consumer has
deleted since the poll listed it fails
+the lock with an `OptimisticLockException`, which rolls back the poll.
+
=== Configuring EntityManagerFactory
It's strongly advised to configure the JPA component to use a specific
diff --git
a/components/camel-jpa/src/generated/resources/META-INF/org/apache/camel/component/jpa/jpa.json
b/components/camel-jpa/src/generated/resources/META-INF/org/apache/camel/component/jpa/jpa.json
index 84c5a6a6ecff..5c32d1b5a210 100644
---
a/components/camel-jpa/src/generated/resources/META-INF/org/apache/camel/component/jpa/jpa.json
+++
b/components/camel-jpa/src/generated/resources/META-INF/org/apache/camel/component/jpa/jpa.json
@@ -60,11 +60,11 @@
"consumeLockEntity": { "index": 10, "kind": "parameter", "displayName":
"Consume Lock Entity", "group": "consumer", "label": "consumer", "required":
false, "type": "boolean", "javaType": "boolean", "deprecated": false,
"autowired": false, "secret": false, "defaultValue": true, "description":
"Specifies whether to set an exclusive lock on each entity bean while
processing the results from polling." },
"deleteHandler": { "index": 11, "kind": "parameter", "displayName":
"Delete Handler", "group": "consumer", "label": "consumer", "required": false,
"type": "object", "javaType":
"org.apache.camel.component.jpa.DeleteHandler<java.lang.Object>", "deprecated":
false, "autowired": false, "secret": false, "description": "To use a custom
DeleteHandler to delete the row after the consumer is done processing the
exchange" },
"lockModeType": { "index": 12, "kind": "parameter", "displayName": "Lock
Mode Type", "group": "consumer", "label": "consumer", "required": false,
"type": "enum", "javaType": "jakarta.persistence.LockModeType", "enum": [
"READ", "WRITE", "OPTIMISTIC", "OPTIMISTIC_FORCE_INCREMENT",
"PESSIMISTIC_READ", "PESSIMISTIC_WRITE", "PESSIMISTIC_FORCE_INCREMENT", "NONE"
], "deprecated": false, "autowired": false, "secret": false, "defaultValue":
"PESSIMISTIC_WRITE", "description": "To configure t [...]
- "lockTimeout": { "index": 13, "kind": "parameter", "displayName": "Lock
Timeout", "group": "consumer", "label": "consumer", "required": false, "type":
"duration", "javaType": "long", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": "0", "description": "How long the consumer
waits for the lock of an entity (see consumeLockEntity) that another
transaction holds. It is given to the JPA provider as the
jakarta.persistence.lock.timeout hint, which the provider app [...]
+ "lockTimeout": { "index": 13, "kind": "parameter", "displayName": "Lock
Timeout", "group": "consumer", "label": "consumer", "required": false, "type":
"duration", "javaType": "long", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": "0", "description": "How long the consumer
waits for the lock of an entity (see consumeLockEntity) that another
transaction holds. It is given to the JPA provider as the
jakarta.persistence.lock.timeout hint, which the provider app [...]
"maxMessagesPerPoll": { "index": 14, "kind": "parameter", "displayName":
"Max Messages Per Poll", "group": "consumer", "label": "consumer", "required":
false, "type": "integer", "javaType": "int", "deprecated": false, "autowired":
false, "secret": false, "description": "An integer value to define the maximum
number of messages to gather per poll. By default, no maximum is set. It can be
used to avoid polling many thousands of messages when starting up the server.
Set a value of 0 or [...]
"preDeleteHandler": { "index": 15, "kind": "parameter", "displayName":
"Pre Delete Handler", "group": "consumer", "label": "consumer", "required":
false, "type": "object", "javaType":
"org.apache.camel.component.jpa.DeleteHandler<java.lang.Object>", "deprecated":
false, "autowired": false, "secret": false, "description": "To use a custom
Pre-DeleteHandler to delete the row after the consumer has read the entity." },
"sendEmptyMessageWhenIdle": { "index": 16, "kind": "parameter",
"displayName": "Send Empty Message When Idle", "group": "consumer", "label":
"consumer", "required": false, "type": "boolean", "javaType": "boolean",
"deprecated": false, "autowired": false, "secret": false, "defaultValue":
false, "description": "If the polling consumer did not poll any files, you can
enable this option to send an empty message (no body) instead." },
- "skipLockedEntity": { "index": 17, "kind": "parameter", "displayName":
"Skip Locked Entity", "group": "consumer", "label": "consumer", "required":
false, "type": "boolean", "javaType": "boolean", "deprecated": false,
"autowired": false, "secret": false, "defaultValue": false, "description": "To
configure whether to use NOWAIT on lock and silently skip the entity." },
+ "skipLockedEntity": { "index": 17, "kind": "parameter", "displayName":
"Skip Locked Entity", "group": "consumer", "label": "consumer", "required":
false, "type": "boolean", "javaType": "boolean", "deprecated": false,
"autowired": false, "secret": false, "defaultValue": false, "description": "To
configure whether to skip an entity that another transaction has locked,
instead of waiting for its lock, so that several consumers can share a table.
With Hibernate and a pessimistic lockMode [...]
"transacted": { "index": 18, "kind": "parameter", "displayName":
"Transacted", "group": "consumer", "label": "consumer", "required": false,
"type": "boolean", "javaType": "boolean", "deprecated": false, "autowired":
false, "secret": false, "defaultValue": false, "description": "Whether to run
the consumer in transacted mode, by which all messages will either commit or
rollback, when the entire batch has been processed. The default behavior
(false) is to commit all the previously succ [...]
"bridgeErrorHandler": { "index": 19, "kind": "parameter", "displayName":
"Bridge Error Handler", "group": "consumer (advanced)", "label":
"consumer,advanced", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "Allows for bridging the consumer to the
Camel routing Error Handler, which mean any exceptions (if possible) occurred
while the Camel consumer is trying to pickup incoming [...]
"exceptionHandler": { "index": 20, "kind": "parameter", "displayName":
"Exception Handler", "group": "consumer (advanced)", "label":
"consumer,advanced", "required": false, "type": "object", "javaType":
"org.apache.camel.spi.ExceptionHandler", "optionalPrefix": "consumer.",
"deprecated": false, "autowired": false, "secret": false, "description": "To
let the consumer use a custom ExceptionHandler. Notice if the option
bridgeErrorHandler is enabled then this option is not in use. By de [...]
diff --git a/components/camel-jpa/src/main/docs/jpa-component.adoc
b/components/camel-jpa/src/main/docs/jpa-component.adoc
index 891d1dd8ac65..e43bb2ae3a04 100644
--- a/components/camel-jpa/src/main/docs/jpa-component.adoc
+++ b/components/camel-jpa/src/main/docs/jpa-component.adoc
@@ -131,11 +131,11 @@ session `idle in transaction`, with its locks, until TCP
keepalive gives up on t
defaults of Linux. A socket timeout of the JDBC driver ends the poll, but not
its wait on the database: each poll leaves
one more session waiting for the lock.
-The consumer waits for at most `lockTimeout`, or not at all with
`skipLockedEntity=true`. The JPA provider applies
-`lockTimeout` if it can with the database: Hibernate 7.4, for one, sets
`lock_timeout` for the lock on PostgreSQL, so the
-database itself cancels the wait.
+The consumer waits for at most `lockTimeout`, or not at all with
`skipLockedEntity=true` (see below). The JPA
+provider applies `lockTimeout` if it can with the database: Hibernate 7.4, for
one, sets `lock_timeout` for the lock on
+PostgreSQL, so the database itself cancels the wait.
-What happens to a lock that the consumer does not get depends on the database:
+What happens to a lock that the consumer does not get in time depends on the
database:
* When the transaction can go on, the JPA provider reports a
`LockTimeoutException`: the consumer skips the entity and
routes the next one.
@@ -151,6 +151,30 @@ does not end, set a timeout on the database, such as
`idle_in_transaction_sessio
a session. Set it above the time a poll takes to route its entities: while the
route runs, the session of the poll is
idle in transaction too.
+==== Skipping the locked entities
+
+With `skipLockedEntity=true`, the consumer skips an entity that another
transaction holds, instead of waiting for its
+lock, and routes the next one. This lets several consumers share the same
table, with a `lockModeType` that locks
+the row exclusively, `PESSIMISTIC_WRITE` (the default) or
`PESSIMISTIC_FORCE_INCREMENT`: each consumer skips the
+entities that the others are consuming. `PESSIMISTIC_READ` does not keep the
consumers apart: it takes a shared lock
+(`FOR SHARE` on PostgreSQL), which another consumer gets too, so two consumers
can route the same entity.
+
+With Hibernate, and a pessimistic `lockModeType` (`PESSIMISTIC_WRITE`,
`PESSIMISTIC_READ` or
+`PESSIMISTIC_FORCE_INCREMENT`), the consumer locks the entity with a select of
the entity with `SKIP LOCKED` (with
+`PESSIMISTIC_WRITE` on PostgreSQL, `SELECT ... FOR NO KEY UPDATE OF ... SKIP
LOCKED`), where the database supports it:
+PostgreSQL, MySQL, Oracle and H2, among others. The select returns no row for
a locked entity, which is no error, so the poll goes on and
+commits the entities it consumed, also on PostgreSQL. An entity that another
consumer has deleted since the poll listed
+it (`consumeDelete=true`, the default) is skipped too. An entity that another
consumer has only updated
+(`consumeDelete=false`) is not skipped: its row is still there, and the select
returns it once the transaction of the
+other consumer has ended.
+
+Otherwise, with another JPA provider or an optimistic `lockModeType`, the
consumer gives the lock the
+`jakarta.persistence.lock.timeout` hint `0` (`NOWAIT`), and a lock that it
does not get is handled as one that times out
+(see above): on PostgreSQL the poll is rolled back. OpenJPA 4.1 does not apply
`NOWAIT`: it applies the lock timeout as
+the query timeout of JDBC, where `0` sets no timeout, so on PostgreSQL its
lock waits for the other transaction, as
+without `skipLockedEntity`. With OpenJPA, an entity that another consumer has
deleted since the poll listed it fails
+the lock with an `OptimisticLockException`, which rolls back the poll.
+
=== Configuring EntityManagerFactory
It's strongly advised to configure the JPA component to use a specific
diff --git
a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConsumer.java
b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConsumer.java
index 34eab709571c..dfc91da668d9 100644
---
a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConsumer.java
+++
b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaConsumer.java
@@ -28,11 +28,15 @@ import java.util.concurrent.atomic.AtomicBoolean;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
+import jakarta.persistence.FlushModeType;
import jakarta.persistence.LockModeType;
import jakarta.persistence.OptimisticLockException;
import jakarta.persistence.PersistenceException;
import jakarta.persistence.PessimisticLockException;
import jakarta.persistence.Query;
+import jakarta.persistence.criteria.CriteriaBuilder;
+import jakarta.persistence.criteria.CriteriaQuery;
+import jakarta.persistence.criteria.Root;
import jakarta.persistence.metamodel.EntityType;
import org.apache.camel.Exchange;
@@ -52,6 +56,10 @@ public class JpaConsumer extends
ScheduledBatchPollingConsumer {
private static final Logger LOG =
LoggerFactory.getLogger(JpaConsumer.class);
private static final String LOCK_TIMEOUT =
"jakarta.persistence.lock.timeout";
+ // the lock timeout that Hibernate takes as SKIP LOCKED
(org.hibernate.Timeouts.SKIP_LOCKED_MILLI): the JPA
+ // specification has no such value, OpenJPA rejects it, and EclipseLink
takes it as a wait time
+ private static final int SKIP_LOCKED = -2;
+ private static final String HIBERNATE_SESSION = "org.hibernate.Session";
private static final Map<String, Object> NOWAIT;
private final EntityManagerFactory entityManagerFactory;
private final TransactionStrategy transactionStrategy;
@@ -68,6 +76,7 @@ public class JpaConsumer extends
ScheduledBatchPollingConsumer {
private boolean transacted;
private boolean skipLockedEntity;
private long lockTimeout;
+ private Boolean skipLockedSupported;
private final AtomicBoolean notEntityWarned = new AtomicBoolean();
static {
@@ -354,8 +363,14 @@ public class JpaConsumer extends
ScheduledBatchPollingConsumer {
}
/**
- * Sets whether to use NOWAIT on lock and silently skip the entity. This
allows different instances to process
- * entities at the same time but not processing the same entity.
+ * Sets whether to skip an entity that another transaction has locked,
instead of waiting for its lock. This allows
+ * different instances to process entities at the same time but not
processing the same entity.
+ * <p/>
+ * With Hibernate and a pessimistic lock mode, the entity is locked with
<tt>SKIP LOCKED</tt> (where the database
+ * supports it): an entity that another transaction holds, or that is no
longer in the database, is skipped and the
+ * transaction goes on, also on PostgreSQL, where a failed lock aborts the
transaction. Otherwise the lock is given
+ * the <tt>jakarta.persistence.lock.timeout</tt> hint 0 (NOWAIT, if the
JPA provider applies it), and the entity is
+ * skipped if the lock fails and the transaction can go on.
*/
public void setSkipLockedEntity(boolean skipLockedEntity) {
this.skipLockedEntity = skipLockedEntity;
@@ -401,6 +416,13 @@ public class JpaConsumer extends
ScheduledBatchPollingConsumer {
try {
LOG.debug("Acquiring exclusive lock on entity: {}", entity);
if (isSkipLockedEntity()) {
+ if (!lockSkipLocked(entity, entityManager)) {
+ LOG.debug("Skipping entity locked by another transaction,
or no longer in the database: {}", entity);
+ return false;
+ }
+ // after SKIP LOCKED this transaction holds the row lock, and
this applies the lock mode, such as
+ // PESSIMISTIC_FORCE_INCREMENT; otherwise NOWAIT fails the
lock of an entity that another transaction
+ // holds
entityManager.lock(entity, lockModeType, NOWAIT);
} else if (lockTimeout > 0) {
// the JPA hint is an int of millis
@@ -425,12 +447,107 @@ public class JpaConsumer extends
ScheduledBatchPollingConsumer {
* Whether the query result is an entity, and not a value such as a
selected column or an Object array.
*/
protected boolean isEntity(Object result, EntityManager entityManager) {
+ return findEntityType(result, entityManager) != null;
+ }
+
+ /**
+ * The entity type of the query result, the one of its class if there is
one (and not of a super class), or null if
+ * the result is not an entity.
+ */
+ private static EntityType<?> findEntityType(Object result, EntityManager
entityManager) {
+ if (result == null) {
+ return null;
+ }
+ EntityType<?> answer = null;
for (EntityType<?> type : entityManager.getMetamodel().getEntities()) {
- if (type.getJavaType() != null &&
type.getJavaType().isInstance(result)) {
- return true;
+ Class<?> javaType = type.getJavaType();
+ if (javaType == result.getClass()) {
+ return type;
+ } else if (answer == null && javaType != null &&
javaType.isInstance(result)) {
+ answer = type;
+ }
+ }
+ return answer;
+ }
+
+ /**
+ * Locks the entity with SKIP LOCKED, if the JPA provider supports it and
the lock mode locks the row: a select of
+ * the entity that skips its row if another transaction holds its lock.
Unlike a failed lock, a skipped row is no
+ * error, so the transaction goes on, also on a database where a failed
lock aborts the transaction (PostgreSQL).
+ *
+ * @return false if the entity is to be skipped: another transaction holds
its lock, or it is no longer in the
+ * database. True if it is now locked, or if SKIP LOCKED is not
used.
+ */
+ private boolean lockSkipLocked(Object entity, EntityManager entityManager)
{
+ LockModeType rowLock;
+ if (lockModeType == LockModeType.PESSIMISTIC_READ) {
+ rowLock = LockModeType.PESSIMISTIC_READ;
+ } else if (lockModeType == LockModeType.PESSIMISTIC_WRITE
+ || lockModeType == LockModeType.PESSIMISTIC_FORCE_INCREMENT) {
+ // Hibernate always locks the rows of a query with
PESSIMISTIC_FORCE_INCREMENT with NOWAIT
+ rowLock = LockModeType.PESSIMISTIC_WRITE;
+ } else {
+ // an optimistic lock mode locks no row
+ return true;
+ }
+ EntityType<?> type = findEntityType(entity, entityManager);
+ if (type == null || !isSkipLockedSupported(entityManager)) {
+ return true;
+ }
+ return !selectSkipLocked(type, entity, entityManager,
rowLock).isEmpty();
+ }
+
+ private static <T> List<T> selectSkipLocked(
+ EntityType<T> type, Object entity, EntityManager entityManager,
LockModeType rowLock) {
+ CriteriaBuilder builder = entityManager.getCriteriaBuilder();
+ CriteriaQuery<T> query = builder.createQuery(type.getJavaType());
+ Root<T> root = query.from(type);
+ query.select(root).where(builder.equal(root, entity));
+ return entityManager.createQuery(query)
+ .setLockMode(rowLock)
+ .setHint(LOCK_TIMEOUT, SKIP_LOCKED)
+ // a lock has no side effect: do not flush the changes of the
entities routed before
+ .setFlushMode(FlushModeType.COMMIT)
+ .getResultList();
+ }
+
+ /**
+ * Whether the JPA provider locks with SKIP LOCKED: Hibernate does, when
given the lock timeout -2.
+ */
+ private boolean isSkipLockedSupported(EntityManager entityManager) {
+ Boolean answer = skipLockedSupported;
+ if (answer == null) {
+ Object delegate;
+ try {
+ delegate = entityManager.getDelegate();
+ } catch (IllegalStateException | PersistenceException e) {
+ LOG.debug("Cannot tell whether the JPA provider is Hibernate,
locking with NOWAIT: {}", e.getMessage(), e);
+ delegate = null;
}
+ if (delegate == null) {
+ // not known, such as with a closed EntityManager: lock with
NOWAIT this time, and check again the next
+ // time, instead of keeping NOWAIT for the life of the consumer
+ return false;
+ }
+ answer = isHibernate(delegate);
+ skipLockedSupported = answer;
+ LOG.debug("skipLockedEntity locks with {}", answer ? "SKIP LOCKED"
: "NOWAIT");
+ }
+ return answer;
+ }
+
+ private static boolean isHibernate(Object delegate) {
+ ClassLoader classLoader = delegate.getClass().getClassLoader();
+ if (classLoader == null) {
+ return false;
+ }
+ try {
+ Class<?> session = Class.forName(HIBERNATE_SESSION, false,
classLoader);
+ return session.isInstance(delegate);
+ } catch (ClassNotFoundException | LinkageError e) {
+ // no Hibernate: lock with NOWAIT
+ return false;
}
- return false;
}
protected QueryFactory createQueryFactory() {
diff --git
a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
index 95314e54153a..86423fc19ad7 100644
---
a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
+++
b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaEndpoint.java
@@ -521,7 +521,12 @@ public class JpaEndpoint extends ScheduledPollEndpoint {
}
/**
- * To configure whether to use NOWAIT on lock and silently skip the entity.
+ * To configure whether to skip an entity that another transaction has
locked, instead of waiting for its lock, so
+ * that several consumers can share a table. With Hibernate and a
pessimistic lockModeType, the consumer locks the
+ * entity with SKIP LOCKED, where the database supports it: an entity that
another transaction holds, or that is no
+ * longer in the database, is skipped, and the poll goes on, also on
PostgreSQL. Otherwise the consumer gives the
+ * lock the timeout 0 (NOWAIT, if the JPA provider applies it), and a lock
that fails is handled as one that times
+ * out with lockTimeout.
*/
public void setSkipLockedEntity(boolean skipLockedEntity) {
this.skipLockedEntity = skipLockedEntity;
@@ -536,10 +541,9 @@ public class JpaEndpoint extends ScheduledPollEndpoint {
* is given to the JPA provider as the jakarta.persistence.lock.timeout
hint, which the provider applies if the
* database supports it: for example Hibernate on PostgreSQL sets
lock_timeout for the lock. The default 0 sets no
* timeout: the lock waits as long as the database lets it, which on
PostgreSQL is until the other transaction ends.
- * A lock that times out is handled like one that skipLockedEntity could
not get. On PostgreSQL it rolls back the
- * poll: the entities that the poll routed before the locked one are
routed again by each poll, for as long as the
- * other transaction holds the lock, so the route must be idempotent. Not
used with skipLockedEntity, which does not
- * wait at all. The value is in millis, and must not be negative.
+ * On PostgreSQL a lock that times out rolls back the poll: the entities
that the poll routed before the locked one
+ * are routed again by each poll, for as long as the other transaction
holds the lock, so the route must be
+ * idempotent. Not used with skipLockedEntity. The value is in millis, and
must not be negative.
*/
public void setLockTimeout(long lockTimeout) {
this.lockTimeout = lockTimeout;
diff --git
a/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaConsumerSkipLockedEntityIT.java
b/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaConsumerSkipLockedEntityIT.java
new file mode 100644
index 000000000000..637938031c03
--- /dev/null
+++
b/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaConsumerSkipLockedEntityIT.java
@@ -0,0 +1,220 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jpa;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.function.LongConsumer;
+
+import jakarta.persistence.EntityManager;
+import jakarta.persistence.EntityManagerFactory;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.infra.postgres.services.PostgresService;
+import org.apache.camel.test.infra.postgres.services.PostgresServiceFactory;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.springframework.jdbc.datasource.DriverManagerDataSource;
+import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
+import org.springframework.orm.jpa.persistenceunit.PersistenceManagedTypes;
+import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * With skipLockedEntity, competing consumers skip the entities that the
others hold. On PostgreSQL a failed lock aborts
+ * the transaction, so a NOWAIT lock cannot skip an entity: the poll would be
rolled back, and the entities it routed
+ * before the locked one routed again by the next poll. With Hibernate the
consumer locks with SKIP LOCKED instead.
+ */
+class JpaConsumerSkipLockedEntityIT extends CamelTestSupport {
+
+ @RegisterExtension
+ static PostgresService service =
PostgresServiceFactory.createSingletonService();
+
+ private LocalContainerEntityManagerFactoryBean entityManagerFactoryBean;
+ private EntityManagerFactory entityManagerFactory;
+ private final List<Long> routed = new ArrayList<>();
+ private LongConsumer whenRouted = id -> {
+ };
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ // Hibernate, set up the way Spring Boot does it
+ entityManagerFactoryBean = new
LocalContainerEntityManagerFactoryBean();
+ entityManagerFactoryBean.setDataSource(
+ new DriverManagerDataSource(service.jdbcUrl(),
service.userName(), service.password()));
+ entityManagerFactoryBean.setJpaVendorAdapter(new
HibernateJpaVendorAdapter());
+
entityManagerFactoryBean.setManagedTypes(PersistenceManagedTypes.of(SkipLockedItem.class.getName()));
+
entityManagerFactoryBean.setMappingResources("org/apache/camel/component/jpa/skip-locked-item-orm.xml");
+ entityManagerFactoryBean.setPersistenceUnitName("skipLockedEntity");
+
entityManagerFactoryBean.setJpaPropertyMap(Map.of("hibernate.hbm2ddl.auto",
"create-drop"));
+ entityManagerFactoryBean.afterPropertiesSet();
+ entityManagerFactory = entityManagerFactoryBean.getObject();
+
+ CamelContext context = super.createCamelContext();
+ JpaComponent jpa = new JpaComponent();
+ jpa.setEntityManagerFactory(entityManagerFactory);
+ context.addComponent("jpa", jpa);
+ return context;
+ }
+
+ @Override
+ protected void doPostTearDown() throws Exception {
+ if (entityManagerFactoryBean != null) {
+ entityManagerFactoryBean.destroy();
+ }
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("jpa://" + SkipLockedItem.class.getName()
+ + "?query=select x from SkipLockedItem x order by
x.id&skipLockedEntity=true&startScheduler=false")
+ .routeId("consumer")
+ .process(exchange -> {
+ long id =
exchange.getMessage().getBody(SkipLockedItem.class).getId();
+ routed.add(id);
+ whenRouted.accept(id);
+ })
+ .to("mock:result");
+ from("jpa://" + SkipLockedItem.class.getName()
+ + "?query=select x from SkipLockedItem x order by
x.id&skipLockedEntity=true&startScheduler=false"
+ + "&lockModeType=PESSIMISTIC_READ")
+ .routeId("consumerRead")
+ .process(exchange ->
routed.add(exchange.getMessage().getBody(SkipLockedItem.class).getId()));
+ }
+ };
+ }
+
+ @Test
+ void lockedEntityIsSkipped() throws Exception {
+ insertItems(1L, 2L, 3L);
+ JpaConsumer consumer = (JpaConsumer)
context.getRoute("consumer").getConsumer();
+
+ try (Connection other = openConnection()) {
+ // another consumer holds the lock of the second entity
+ other.setAutoCommit(false);
+ try (Statement st = other.createStatement()) {
+ st.execute("select id from skip_locked_item where id = 2 for
update");
+ }
+
+ // the poll skips the second entity, and consumes the others
+ consumer.poll();
+ assertEquals(List.of(1L, 3L), routed);
+ assertEquals(List.of(2L), itemIds());
+
+ // the next poll skips it again, and routes nothing again
+ consumer.poll();
+ assertEquals(List.of(1L, 3L), routed);
+ assertEquals(List.of(2L), itemIds());
+
+ other.rollback();
+ }
+
+ // once its lock is released, the next poll consumes the second entity
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(3);
+ consumer.poll();
+ mock.assertIsSatisfied();
+ assertEquals(List.of(1L, 3L, 2L), routed);
+ assertEquals(List.of(), itemIds());
+ }
+
+ @Test
+ void entityDeletedByAnotherConsumerIsSkipped() throws Exception {
+ insertItems(1L, 2L, 3L);
+ JpaConsumer consumer = (JpaConsumer)
context.getRoute("consumer").getConsumer();
+ // while the poll routes the first entity, another consumer deletes
the second one, which the poll has listed
+ whenRouted = id -> {
+ if (id == 1L) {
+ deleteItem(2L);
+ }
+ };
+
+ consumer.poll();
+ assertEquals(List.of(), itemIds());
+ assertEquals(List.of(1L, 3L), routed);
+ }
+
+ @Test
+ void lockedEntityIsSkippedWithPessimisticRead() throws Exception {
+ insertItems(1L, 2L, 3L);
+ JpaConsumer consumer = (JpaConsumer)
context.getRoute("consumerRead").getConsumer();
+
+ try (Connection other = openConnection()) {
+ other.setAutoCommit(false);
+ try (Statement st = other.createStatement()) {
+ st.execute("select id from skip_locked_item where id = 2 for
update");
+ }
+
+ consumer.poll();
+ assertEquals(List.of(2L), itemIds());
+ assertEquals(List.of(1L, 3L), routed);
+
+ other.rollback();
+ }
+ }
+
+ private void insertItems(Long... ids) {
+ EntityManager em = entityManagerFactory.createEntityManager();
+ try {
+ em.getTransaction().begin();
+ for (Long id : ids) {
+ em.persist(new SkipLockedItem(id));
+ }
+ em.getTransaction().commit();
+ } finally {
+ em.close();
+ }
+ }
+
+ private static void deleteItem(long id) {
+ try (Connection connection = openConnection();
+ Statement st = connection.createStatement()) {
+ st.execute("delete from skip_locked_item where id = " + id);
+ } catch (SQLException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
+ private static Connection openConnection() throws SQLException {
+ return DriverManager.getConnection(service.jdbcUrl(),
service.userName(), service.password());
+ }
+
+ private static List<Long> itemIds() throws SQLException {
+ List<Long> ids = new ArrayList<>();
+ try (Connection connection = openConnection();
+ Statement st = connection.createStatement();
+ ResultSet rs = st.executeQuery("select id from skip_locked_item
order by id")) {
+ while (rs.next()) {
+ ids.add(rs.getLong(1));
+ }
+ }
+ return ids;
+ }
+}
diff --git
a/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaConsumerSkipLockedEntityTest.java
b/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaConsumerSkipLockedEntityTest.java
new file mode 100644
index 000000000000..9bffcc71ca34
--- /dev/null
+++
b/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/JpaConsumerSkipLockedEntityTest.java
@@ -0,0 +1,311 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jpa;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.LongConsumer;
+
+import jakarta.persistence.EntityManager;
+import jakarta.persistence.EntityManagerFactory;
+import jakarta.persistence.LockModeType;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.hibernate.Session;
+import org.junit.jupiter.api.Test;
+import org.springframework.jdbc.datasource.DriverManagerDataSource;
+import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
+import org.springframework.orm.jpa.persistenceunit.PersistenceManagedTypes;
+import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.AdditionalAnswers.delegatesTo;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * skipLockedEntity with Hibernate locks with SKIP LOCKED, on H2 here.
JpaConsumerSkipLockedEntityIT shows the case of
+ * PostgreSQL, where a failed lock aborts the transaction.
+ */
+class JpaConsumerSkipLockedEntityTest extends CamelTestSupport {
+
+ private static final String URL =
"jdbc:h2:mem:skipLockedEntity;DB_CLOSE_DELAY=-1";
+ private static final Map<String, Object> NOWAIT =
Map.of("jakarta.persistence.lock.timeout", 0);
+
+ private LocalContainerEntityManagerFactoryBean entityManagerFactoryBean;
+ private EntityManagerFactory entityManagerFactory;
+ private final List<Long> routed = new ArrayList<>();
+ private LongConsumer whenRouted = id -> {
+ };
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ entityManagerFactoryBean = new
LocalContainerEntityManagerFactoryBean();
+ entityManagerFactoryBean.setDataSource(new
DriverManagerDataSource(URL, "sa", ""));
+ entityManagerFactoryBean.setJpaVendorAdapter(new
HibernateJpaVendorAdapter());
+ entityManagerFactoryBean.setManagedTypes(
+ PersistenceManagedTypes.of(SkipLockedItem.class.getName(),
SkipLockedVersionedItem.class.getName()));
+
entityManagerFactoryBean.setMappingResources("org/apache/camel/component/jpa/skip-locked-item-orm.xml");
+ entityManagerFactoryBean.setPersistenceUnitName("skipLockedEntity");
+
entityManagerFactoryBean.setJpaPropertyMap(Map.of("hibernate.hbm2ddl.auto",
"create-drop"));
+ entityManagerFactoryBean.afterPropertiesSet();
+ entityManagerFactory = entityManagerFactoryBean.getObject();
+
+ CamelContext context = super.createCamelContext();
+ JpaComponent jpa = new JpaComponent();
+ jpa.setEntityManagerFactory(entityManagerFactory);
+ context.addComponent("jpa", jpa);
+ return context;
+ }
+
+ @Override
+ protected void doPostTearDown() throws Exception {
+ if (entityManagerFactoryBean != null) {
+ entityManagerFactoryBean.destroy();
+ }
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("jpa://" + SkipLockedItem.class.getName()
+ + "?query=select x from SkipLockedItem x order by
x.id&skipLockedEntity=true&startScheduler=false")
+ .routeId("consumer")
+ .process(exchange -> {
+ long id =
exchange.getMessage().getBody(SkipLockedItem.class).getId();
+ routed.add(id);
+ whenRouted.accept(id);
+ });
+
+ from("jpa://" + SkipLockedVersionedItem.class.getName()
+ + "?query=select x from SkipLockedVersionedItem x order
by x.id&skipLockedEntity=true"
+ +
"&lockModeType=PESSIMISTIC_FORCE_INCREMENT&consumeDelete=false&startScheduler=false")
+ .routeId("versionedConsumer")
+ .process(exchange -> routed
+
.add(exchange.getMessage().getBody(SkipLockedVersionedItem.class).getId()));
+ }
+ };
+ }
+
+ @Test
+ void lockedEntityIsSkipped() throws Exception {
+ insertItems(1L, 2L, 3L);
+ JpaConsumer consumer = jpaConsumer();
+
+ try (Connection other = openConnection()) {
+ // another consumer holds the lock of the second entity
+ other.setAutoCommit(false);
+ try (Statement st = other.createStatement()) {
+ st.execute("select id from skip_locked_item where id = 2 for
update");
+ }
+
+ consumer.poll();
+ assertEquals(List.of(1L, 3L), routed);
+ assertEquals(List.of(2L), itemIds());
+
+ other.rollback();
+ }
+
+ consumer.poll();
+ assertEquals(List.of(1L, 3L, 2L), routed);
+ assertEquals(List.of(), itemIds());
+ }
+
+ @Test
+ void forceIncrementSkipsLockedEntityAndIncrementsVersion() throws
Exception {
+ // PESSIMISTIC_FORCE_INCREMENT selects the row with SKIP LOCKED as
PESSIMISTIC_WRITE does, and the lock that
+ // follows increments the version
+ insertVersionedItems(1L, 2L, 3L);
+ JpaConsumer consumer = (JpaConsumer)
context.getRoute("versionedConsumer").getConsumer();
+
+ try (Connection other = openConnection()) {
+ // another consumer holds the lock of the second entity
+ other.setAutoCommit(false);
+ try (Statement st = other.createStatement()) {
+ st.execute("select id from skip_locked_versioned_item where id
= 2 for update");
+ }
+
+ consumer.poll();
+ other.rollback();
+ }
+
+ assertEquals(List.of(1L, 3L), routed);
+ // consumeDelete=false keeps the entities: the routed ones are at
version 1, the skipped one is still at 0
+ assertEquals(Map.of(1L, 1L, 2L, 0L, 3L, 1L), versions());
+ }
+
+ @Test
+ void entityDeletedByAnotherConsumerIsSkipped() throws Exception {
+ insertItems(1L, 2L, 3L);
+ // while the poll routes the first entity, another consumer deletes
the second one, which the poll has listed
+ whenRouted = id -> {
+ if (id == 1L) {
+ deleteItem(2L);
+ }
+ };
+
+ jpaConsumer().poll();
+ assertEquals(List.of(), itemIds());
+ assertEquals(List.of(1L, 3L), routed);
+ }
+
+ @Test
+ void optimisticLockModeLocksWithNowait() throws Exception {
+ // an optimistic lock mode locks no row, so there is no row to skip
+ SkipLockedItem item = new SkipLockedItem(1L);
+ EntityManager em = entityManager(mock(Session.class));
+ JpaConsumer consumer = jpaConsumer();
+ consumer.setLockModeType(LockModeType.OPTIMISTIC);
+
+ assertTrue(consumer.lockEntity(item, em));
+ verify(em).lock(item, LockModeType.OPTIMISTIC, NOWAIT);
+ verify(em, never()).getCriteriaBuilder();
+ }
+
+ @Test
+ void otherJpaProvidersLockWithNowait() throws Exception {
+ // the JPA specification has no SKIP LOCKED: other providers reject
the lock timeout of Hibernate for it (OpenJPA),
+ // or would wait for the lock
+ SkipLockedItem item = new SkipLockedItem(1L);
+ EntityManager em = entityManager(new Object());
+
+ assertTrue(jpaConsumer().lockEntity(item, em));
+ verify(em).lock(item, LockModeType.PESSIMISTIC_WRITE, NOWAIT);
+ verify(em, never()).getCriteriaBuilder();
+ }
+
+ @Test
+ void providerCheckThatFailsIsCheckedAgain() throws Exception {
+ // the check of the JPA provider can fail, such as on a closed
EntityManager: the lock is NOWAIT that time, and
+ // the next lock checks again, instead of keeping NOWAIT for the life
of the consumer
+ insertItems(1L);
+ EntityManager real = entityManagerFactory.createEntityManager();
+ try {
+ real.getTransaction().begin();
+ SkipLockedItem item = real.find(SkipLockedItem.class, 1L);
+ EntityManager em = mock(EntityManager.class, delegatesTo(real));
+ doThrow(new IllegalStateException("EntityManager is
closed")).doAnswer(invocation -> real.getDelegate())
+ .when(em).getDelegate();
+ JpaConsumer consumer = jpaConsumer();
+
+ assertTrue(consumer.lockEntity(item, em));
+ verify(em).lock(item, LockModeType.PESSIMISTIC_WRITE, NOWAIT);
+ verify(em, never()).getCriteriaBuilder();
+
+ // Hibernate this time: SKIP LOCKED, and the answer is kept
+ assertTrue(consumer.lockEntity(item, em));
+ assertTrue(consumer.lockEntity(item, em));
+ verify(em, times(2)).getCriteriaBuilder();
+ verify(em, times(2)).getDelegate();
+ } finally {
+ if (real.getTransaction().isActive()) {
+ real.getTransaction().rollback();
+ }
+ real.close();
+ }
+ }
+
+ private JpaConsumer jpaConsumer() {
+ return (JpaConsumer) context.getRoute("consumer").getConsumer();
+ }
+
+ private EntityManager entityManager(Object delegate) {
+ EntityManager em = mock(EntityManager.class);
+
when(em.getMetamodel()).thenReturn(entityManagerFactory.getMetamodel());
+ when(em.getDelegate()).thenReturn(delegate);
+ return em;
+ }
+
+ private void insertItems(Long... ids) {
+ EntityManager em = entityManagerFactory.createEntityManager();
+ try {
+ em.getTransaction().begin();
+ for (Long id : ids) {
+ em.persist(new SkipLockedItem(id));
+ }
+ em.getTransaction().commit();
+ } finally {
+ em.close();
+ }
+ }
+
+ private void insertVersionedItems(Long... ids) {
+ EntityManager em = entityManagerFactory.createEntityManager();
+ try {
+ em.getTransaction().begin();
+ for (Long id : ids) {
+ em.persist(new SkipLockedVersionedItem(id));
+ }
+ em.getTransaction().commit();
+ } finally {
+ em.close();
+ }
+ }
+
+ private static Map<Long, Long> versions() throws SQLException {
+ Map<Long, Long> versions = new HashMap<>();
+ try (Connection connection = openConnection();
+ Statement st = connection.createStatement();
+ ResultSet rs = st.executeQuery("select id, version from
skip_locked_versioned_item")) {
+ while (rs.next()) {
+ versions.put(rs.getLong(1), rs.getLong(2));
+ }
+ }
+ return versions;
+ }
+
+ private static void deleteItem(long id) {
+ try (Connection connection = openConnection();
+ Statement st = connection.createStatement()) {
+ st.execute("delete from skip_locked_item where id = " + id);
+ } catch (SQLException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
+ private static Connection openConnection() throws SQLException {
+ return DriverManager.getConnection(URL, "sa", "");
+ }
+
+ private static List<Long> itemIds() throws SQLException {
+ List<Long> ids = new ArrayList<>();
+ try (Connection connection = openConnection();
+ Statement st = connection.createStatement();
+ ResultSet rs = st.executeQuery("select id from skip_locked_item
order by id")) {
+ while (rs.next()) {
+ ids.add(rs.getLong(1));
+ }
+ }
+ return ids;
+ }
+}
diff --git
a/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/SkipLockedItem.java
b/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/SkipLockedItem.java
new file mode 100644
index 000000000000..707406358e2a
--- /dev/null
+++
b/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/SkipLockedItem.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jpa;
+
+/**
+ * The entity of the skipLockedEntity tests with Hibernate, mapped in
skip-locked-item-orm.xml and not with annotations:
+ * the OpenJPA persistence units of the other tests scan the test classes for
annotated entities, and this one is not
+ * enhanced for OpenJPA.
+ */
+public class SkipLockedItem {
+ private Long id;
+
+ public SkipLockedItem() {
+ }
+
+ public SkipLockedItem(Long id) {
+ this.id = id;
+ }
+
+ public Long getId() {
+ return id;
+ }
+}
diff --git
a/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/SkipLockedVersionedItem.java
b/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/SkipLockedVersionedItem.java
new file mode 100644
index 000000000000..13cb54d4e65f
--- /dev/null
+++
b/components/camel-jpa/src/test/java/org/apache/camel/component/jpa/SkipLockedVersionedItem.java
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jpa;
+
+/**
+ * The versioned entity of the skipLockedEntity tests with Hibernate, mapped
in skip-locked-item-orm.xml as
+ * {@link SkipLockedItem}.
+ */
+public class SkipLockedVersionedItem {
+ private Long id;
+ private long version;
+
+ public SkipLockedVersionedItem() {
+ }
+
+ public SkipLockedVersionedItem(Long id) {
+ this.id = id;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public long getVersion() {
+ return version;
+ }
+}
diff --git
a/components/camel-jpa/src/test/resources/org/apache/camel/component/jpa/skip-locked-item-orm.xml
b/components/camel-jpa/src/test/resources/org/apache/camel/component/jpa/skip-locked-item-orm.xml
new file mode 100644
index 000000000000..744453c42ccd
--- /dev/null
+++
b/components/camel-jpa/src/test/resources/org/apache/camel/component/jpa/skip-locked-item-orm.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+-->
+<!-- the entity of the skipLockedEntity tests, mapped here and not with
annotations: the OpenJPA persistence units of
+ the other tests scan the test classes for annotated entities, and this
one is not enhanced for OpenJPA -->
+<entity-mappings xmlns="https://jakarta.ee/xml/ns/persistence/orm"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence/orm
https://jakarta.ee/xml/ns/persistence/orm/orm_3_1.xsd"
+ version="3.1">
+ <entity class="org.apache.camel.component.jpa.SkipLockedItem"
name="SkipLockedItem"
+ access="FIELD">
+ <table name="skip_locked_item"/>
+ <attributes>
+ <id name="id"/>
+ </attributes>
+ </entity>
+ <entity class="org.apache.camel.component.jpa.SkipLockedVersionedItem"
name="SkipLockedVersionedItem"
+ access="FIELD">
+ <table name="skip_locked_versioned_item"/>
+ <attributes>
+ <id name="id"/>
+ <version name="version"/>
+ </attributes>
+ </entity>
+</entity-mappings>
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index f241dbdfff56..20595e730ecf 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -181,6 +181,23 @@ a failure, as the JPA specification leaves them after a
rollback. A route that h
`onException` and `handled(true)`, or `doCatch`) and goes on using them can no
longer load their lazy associations,
and the next JPA step of the exchange uses a new `EntityManager`.
+=== camel-jpa - skipLockedEntity skips a locked entity with SKIP LOCKED on
Hibernate
+
+With `skipLockedEntity=true`, Hibernate and a pessimistic `lockModeType` (the
default `PESSIMISTIC_WRITE`), the consumer
+now locks each entity with a select of the entity with `SKIP LOCKED`, instead
of `EntityManager.lock` with the
+`jakarta.persistence.lock.timeout` hint `0` (`NOWAIT`). The select returns no
row for an entity that another transaction
+holds, and the consumer skips the entity.
+
+On PostgreSQL, the consumer now skips the locked entity and commits the poll.
Before, the failed `NOWAIT` lock aborted the
+transaction, the poll was rolled back, and the entities that it had routed
before the locked one were routed again by
+the next poll, for as long as the other transaction held the lock.
+
+On every database, an entity that another consumer has deleted
(`consumeDelete=true`, the default) since the poll
+listed it is now skipped.
+Before, its lock failed with an `OptimisticLockException`, and the poll was
rolled back.
+
+Other JPA providers, and the optimistic lock modes, are not affected.
+
=== camel-openfga - writeTuples and the read operations carry a relationship
condition
`writeTuples` now honours a relationship condition set on a tuple in the
message body: a `ClientTupleKey` built with
diff --git
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/JpaEndpointBuilderFactory.java
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/JpaEndpointBuilderFactory.java
index c9e0b587a116..efc13cc67cb8 100644
---
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/JpaEndpointBuilderFactory.java
+++
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/JpaEndpointBuilderFactory.java
@@ -373,13 +373,12 @@ public interface JpaEndpointBuilderFactory {
* provider applies if the database supports it: for example Hibernate
* on PostgreSQL sets lock_timeout for the lock. The default 0 sets no
* timeout: the lock waits as long as the database lets it, which on
- * PostgreSQL is until the other transaction ends. A lock that times
out
- * is handled like one that skipLockedEntity could not get. On
- * PostgreSQL it rolls back the poll: the entities that the poll routed
+ * PostgreSQL is until the other transaction ends. On PostgreSQL a lock
+ * that times out rolls back the poll: the entities that the poll
routed
* before the locked one are routed again by each poll, for as long as
* the other transaction holds the lock, so the route must be
- * idempotent. Not used with skipLockedEntity, which does not wait at
- * all. The value is in millis, and must not be negative.
+ * idempotent. Not used with skipLockedEntity. The value is in millis,
+ * and must not be negative.
*
* The option is a: <code>long</code> type.
*
@@ -400,13 +399,12 @@ public interface JpaEndpointBuilderFactory {
* provider applies if the database supports it: for example Hibernate
* on PostgreSQL sets lock_timeout for the lock. The default 0 sets no
* timeout: the lock waits as long as the database lets it, which on
- * PostgreSQL is until the other transaction ends. A lock that times
out
- * is handled like one that skipLockedEntity could not get. On
- * PostgreSQL it rolls back the poll: the entities that the poll routed
+ * PostgreSQL is until the other transaction ends. On PostgreSQL a lock
+ * that times out rolls back the poll: the entities that the poll
routed
* before the locked one are routed again by each poll, for as long as
* the other transaction holds the lock, so the route must be
- * idempotent. Not used with skipLockedEntity, which does not wait at
- * all. The value is in millis, and must not be negative.
+ * idempotent. Not used with skipLockedEntity. The value is in millis,
+ * and must not be negative.
*
* The option will be converted to a <code>long</code> type.
*
@@ -519,8 +517,15 @@ public interface JpaEndpointBuilderFactory {
return this;
}
/**
- * To configure whether to use NOWAIT on lock and silently skip the
- * entity.
+ * To configure whether to skip an entity that another transaction has
+ * locked, instead of waiting for its lock, so that several consumers
+ * can share a table. With Hibernate and a pessimistic lockModeType,
the
+ * consumer locks the entity with SKIP LOCKED, where the database
+ * supports it: an entity that another transaction holds, or that is no
+ * longer in the database, is skipped, and the poll goes on, also on
+ * PostgreSQL. Otherwise the consumer gives the lock the timeout 0
+ * (NOWAIT, if the JPA provider applies it), and a lock that fails is
+ * handled as one that times out with lockTimeout.
*
* The option is a: <code>boolean</code> type.
*
@@ -535,8 +540,15 @@ public interface JpaEndpointBuilderFactory {
return this;
}
/**
- * To configure whether to use NOWAIT on lock and silently skip the
- * entity.
+ * To configure whether to skip an entity that another transaction has
+ * locked, instead of waiting for its lock, so that several consumers
+ * can share a table. With Hibernate and a pessimistic lockModeType,
the
+ * consumer locks the entity with SKIP LOCKED, where the database
+ * supports it: an entity that another transaction holds, or that is no
+ * longer in the database, is skipped, and the poll goes on, also on
+ * PostgreSQL. Otherwise the consumer gives the lock the timeout 0
+ * (NOWAIT, if the JPA provider applies it), and a lock that fails is
+ * handled as one that times out with lockTimeout.
*
* The option will be converted to a <code>boolean</code> type.
*