-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support for OPTIMISTIC + OPTIMISTIC_FORCE_INCREMENT LockModes
These LockModes force a version check or upgrade right at the end of the transaction. This required building infrastructure for reactive before/after transaction completion events. Fixes #201
- Loading branch information
Showing
12 changed files
with
344 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...rc/main/java/org/hibernate/reactive/engine/ReactiveAfterTransactionCompletionProcess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive.engine; | ||
|
||
import org.hibernate.engine.spi.SharedSessionContractImplementor; | ||
|
||
import java.util.concurrent.CompletionStage; | ||
|
||
/** | ||
* Contract representing some process that needs to occur during after transaction completion. | ||
* | ||
* @author Steve Ebersole | ||
*/ | ||
public interface ReactiveAfterTransactionCompletionProcess { | ||
/** | ||
* Perform whatever processing is encapsulated here after completion of the transaction. | ||
* | ||
* @param success Did the transaction complete successfully? True means it did. | ||
* @param session The session on which the transaction is completing. | ||
*/ | ||
CompletionStage<Void> doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session); | ||
} |
24 changes: 24 additions & 0 deletions
24
...c/main/java/org/hibernate/reactive/engine/ReactiveBeforeTransactionCompletionProcess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive.engine; | ||
|
||
import org.hibernate.engine.spi.SessionImplementor; | ||
|
||
import java.util.concurrent.CompletionStage; | ||
|
||
/** | ||
* Contract representing some process that needs to occur during before transaction completion. | ||
* | ||
* @author Steve Ebersole | ||
*/ | ||
public interface ReactiveBeforeTransactionCompletionProcess { | ||
/** | ||
* Perform whatever processing is encapsulated here before completion of the transaction. | ||
* | ||
* @param session The session on which the transaction is preparing to complete. | ||
*/ | ||
CompletionStage<Void> doBeforeTransactionCompletion(SessionImplementor session); | ||
} |
59 changes: 59 additions & 0 deletions
59
...c/main/java/org/hibernate/reactive/engine/impl/ReactiveEntityIncrementVersionProcess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive.engine.impl; | ||
|
||
import org.hibernate.LockMode; | ||
import org.hibernate.LockOptions; | ||
import org.hibernate.engine.spi.EntityEntry; | ||
import org.hibernate.engine.spi.SessionImplementor; | ||
import org.hibernate.reactive.engine.ReactiveBeforeTransactionCompletionProcess; | ||
import org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister; | ||
import org.hibernate.reactive.util.impl.CompletionStages; | ||
|
||
import java.util.concurrent.CompletionStage; | ||
|
||
/** | ||
* A BeforeTransactionCompletionProcess impl to verify and increment an entity version as party | ||
* of before-transaction-completion processing | ||
* | ||
* @author Scott Marlow | ||
* @author Gavin King | ||
*/ | ||
public class ReactiveEntityIncrementVersionProcess implements ReactiveBeforeTransactionCompletionProcess { | ||
private final Object object; | ||
|
||
/** | ||
* Constructs an EntityIncrementVersionProcess for the given entity. | ||
* | ||
* @param object The entity instance | ||
*/ | ||
public ReactiveEntityIncrementVersionProcess(Object object) { | ||
this.object = object; | ||
} | ||
|
||
/** | ||
* Perform whatever processing is encapsulated here before completion of the transaction. | ||
* | ||
* @param session The session on which the transaction is preparing to complete. | ||
*/ | ||
@Override | ||
public CompletionStage<Void> doBeforeTransactionCompletion(SessionImplementor session) { | ||
final EntityEntry entry = session.getPersistenceContext().getEntry( object ); | ||
// Don't increment version for an entity that is not in the PersistenceContext; | ||
if ( entry == null ) { | ||
return CompletionStages.voidFuture(); | ||
} | ||
|
||
return ( (ReactiveEntityPersister) entry.getPersister() ) | ||
.lockReactive( | ||
entry.getId(), | ||
entry.getVersion(), | ||
object, | ||
new LockOptions(LockMode.PESSIMISTIC_FORCE_INCREMENT), | ||
session | ||
); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
.../src/main/java/org/hibernate/reactive/engine/impl/ReactiveEntityVerifyVersionProcess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive.engine.impl; | ||
|
||
import org.hibernate.dialect.lock.OptimisticEntityLockException; | ||
import org.hibernate.engine.spi.EntityEntry; | ||
import org.hibernate.engine.spi.SessionImplementor; | ||
import org.hibernate.pretty.MessageHelper; | ||
import org.hibernate.reactive.engine.ReactiveBeforeTransactionCompletionProcess; | ||
import org.hibernate.reactive.persister.entity.impl.ReactiveEntityPersister; | ||
import org.hibernate.reactive.util.impl.CompletionStages; | ||
|
||
import java.util.concurrent.CompletionStage; | ||
|
||
/** | ||
* A BeforeTransactionCompletionProcess impl to verify an entity version as part of | ||
* before-transaction-completion processing | ||
* | ||
* @author Scott Marlow | ||
* @author Gavin King | ||
*/ | ||
public class ReactiveEntityVerifyVersionProcess implements ReactiveBeforeTransactionCompletionProcess { | ||
private final Object object; | ||
|
||
/** | ||
* Constructs an EntityVerifyVersionProcess | ||
* | ||
* @param object The entity instance | ||
*/ | ||
public ReactiveEntityVerifyVersionProcess(Object object) { | ||
this.object = object; | ||
} | ||
|
||
@Override | ||
public CompletionStage<Void> doBeforeTransactionCompletion(SessionImplementor session) { | ||
final EntityEntry entry = session.getPersistenceContext().getEntry( object ); | ||
// Don't check version for an entity that is not in the PersistenceContext; | ||
if ( entry == null ) { | ||
return CompletionStages.voidFuture(); | ||
} | ||
|
||
final ReactiveEntityPersister persister = (ReactiveEntityPersister) entry.getPersister(); | ||
//TODO: optimize this to only fetch the version number! | ||
return persister.reactiveGetDatabaseSnapshot( entry.getId(), session ) | ||
.thenApply( snapshot -> snapshot[ persister.getVersionProperty() ] ) | ||
.thenAccept( latestVersion -> { | ||
if ( !entry.getVersion().equals( latestVersion ) ) { | ||
throw new OptimisticEntityLockException( | ||
object, | ||
"Newer version [" + latestVersion + | ||
"] of entity [" + MessageHelper.infoString( entry.getEntityName(), entry.getId() ) + | ||
"] found in database" | ||
); | ||
} | ||
} ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.