-
-
Notifications
You must be signed in to change notification settings - Fork 102
[4.2] Implemented Reactive versions of PostAction, PreAction and JdbcSelectWithActions #2732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dreab8
wants to merge
5
commits into
hibernate:main
Choose a base branch
from
dreab8:issue#2651_4.2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bc08181
[#2651] Implemented Reactive versions of `PostAction`, `PreAction` an…
dreab8 e8cc891
[#2651] Removed @Disabled from FindByIdWithLockTest.testFindUpgradeN…
dreab8 15305e1
[#2651] Add test for Lock Timeout
dreab8 304f3cf
[#2651] Add notNullableToOneAssociationMissingKey IllegalStateExcepti…
dreab8 75d5e59
[#2651] Improved AssertionFailure message in ReactiveTableLock#reacti…
dreab8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
315 changes: 315 additions & 0 deletions
315
...src/main/java/org/hibernate/reactive/sql/exec/internal/ReactiveJdbcSelectWithActions.java
This file contains hidden or 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,315 @@ | ||
| /* Hibernate, Relational Persistence for Idiomatic Java | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright: Red Hat Inc. and Hibernate Authors | ||
| */ | ||
| package org.hibernate.reactive.sql.exec.internal; | ||
|
|
||
| import org.hibernate.LockOptions; | ||
| import org.hibernate.dialect.lock.spi.LockTimeoutType; | ||
| import org.hibernate.dialect.lock.spi.LockingSupport; | ||
| import org.hibernate.internal.util.collections.CollectionHelper; | ||
| import org.hibernate.reactive.logging.impl.Log; | ||
| import org.hibernate.reactive.logging.impl.LoggerFactory; | ||
| import org.hibernate.reactive.pool.ReactiveConnection; | ||
| import org.hibernate.reactive.sql.exec.internal.lock.ReactiveConnectionLockTimeoutStrategyBuilder; | ||
| import org.hibernate.reactive.sql.exec.internal.lock.ReactiveFollowOnLockingAction; | ||
| import org.hibernate.reactive.sql.exec.internal.lock.ReactiveLockTimeoutHandler; | ||
| import org.hibernate.reactive.sql.exec.spi.ReactiveJdbcSelect; | ||
| import org.hibernate.reactive.sql.exec.spi.ReactivePostAction; | ||
| import org.hibernate.reactive.sql.exec.spi.ReactivePreAction; | ||
| import org.hibernate.sql.ast.spi.LockingClauseStrategy; | ||
| import org.hibernate.sql.ast.tree.select.QuerySpec; | ||
| import org.hibernate.sql.exec.internal.JdbcOperationQuerySelect; | ||
| import org.hibernate.sql.exec.internal.JdbcSelectWithActions; | ||
| import org.hibernate.sql.exec.spi.ExecutionContext; | ||
| import org.hibernate.sql.exec.spi.JdbcSelect; | ||
| import org.hibernate.sql.exec.spi.JdbcSelectWithActionsBuilder; | ||
| import org.hibernate.sql.exec.spi.LoadedValuesCollector; | ||
| import org.hibernate.sql.exec.spi.PostAction; | ||
| import org.hibernate.sql.exec.spi.PreAction; | ||
| import org.hibernate.sql.exec.spi.SecondaryAction; | ||
| import org.hibernate.sql.exec.spi.StatementAccess; | ||
|
|
||
| import java.lang.invoke.MethodHandles; | ||
| import java.sql.Connection; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.concurrent.CompletionStage; | ||
|
|
||
| import static org.hibernate.reactive.util.impl.CompletionStages.loop; | ||
| import static org.hibernate.reactive.util.impl.CompletionStages.nullFuture; | ||
|
|
||
| /** | ||
| * Reactive version of {@link JdbcSelectWithActions} | ||
| */ | ||
| public class ReactiveJdbcSelectWithActions extends JdbcSelectWithActions implements ReactiveJdbcSelect { | ||
| public static final Log LOG = LoggerFactory.make( Log.class, MethodHandles.lookup() ); | ||
|
|
||
| @Override | ||
| public void performPreActions( | ||
| StatementAccess jdbcStatementAccess, | ||
| Connection jdbcConnection, | ||
| ExecutionContext executionContext) { | ||
| throw LOG.nonReactiveMethodCall( "reactivePerformPreActions()" ); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void performPostAction( | ||
| boolean succeeded, | ||
| StatementAccess jdbcStatementAccess, | ||
| Connection jdbcConnection, | ||
| ExecutionContext executionContext) { | ||
| throw LOG.nonReactiveMethodCall( "reactivePerformPostActions()" ); | ||
| } | ||
|
|
||
| public ReactiveJdbcSelectWithActions( | ||
| JdbcOperationQuerySelect primaryOperation, | ||
| LoadedValuesCollector loadedValuesCollector, | ||
| PreAction[] preActions, | ||
| PostAction[] postActions) { | ||
| super( primaryOperation, loadedValuesCollector, preActions, postActions ); | ||
| } | ||
|
|
||
| public ReactiveJdbcSelectWithActions( | ||
| JdbcOperationQuerySelect primaryAction, | ||
| LoadedValuesCollector loadedValuesCollector) { | ||
| super( primaryAction, loadedValuesCollector ); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletionStage<Void> reactivePerformPreActions( | ||
| ReactiveConnection connection, | ||
| ExecutionContext executionContext) { | ||
| if ( preActions == null ) { | ||
| return nullFuture(); | ||
| } | ||
|
|
||
| return loop( preActions, preAction -> | ||
| ( (ReactivePreAction) preAction ).reactivePerformPreAction( connection, executionContext ) | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletionStage<Void> reactivePerformPostActions( | ||
| boolean succeeded, | ||
| ReactiveConnection connection, | ||
| ExecutionContext executionContext) { | ||
| if ( postActions != null ) { | ||
| return loop( | ||
| postActions, postAction -> { | ||
| if ( succeeded || postAction.shouldRunAfterFail() ) { | ||
| return ( (ReactivePostAction) postAction ).reactivePerformReactivePostAction( | ||
| connection, | ||
| executionContext | ||
| ); | ||
| } | ||
| return nullFuture(); | ||
| } | ||
| ).thenAccept( unused -> { | ||
| if ( loadedValuesCollector != null ) { | ||
| loadedValuesCollector.clear(); | ||
| } | ||
| } ); | ||
| } | ||
| else { | ||
| if ( loadedValuesCollector != null ) { | ||
| loadedValuesCollector.clear(); | ||
| } | ||
| return nullFuture(); | ||
| } | ||
| } | ||
|
|
||
| public static class Builder implements JdbcSelectWithActionsBuilder { | ||
| private JdbcOperationQuerySelect primaryAction; | ||
| private LoadedValuesCollector loadedValuesCollector; | ||
| protected List<PreAction> preActions; | ||
| protected List<PostAction> postActions; | ||
| protected LockTimeoutType lockTimeoutType; | ||
| protected LockingSupport lockingSupport; | ||
| protected LockOptions lockOptions; | ||
| protected QuerySpec lockingTarget; | ||
| protected LockingClauseStrategy lockingClauseStrategy; | ||
| boolean isFollonOnLockStrategy; | ||
|
|
||
| @Override | ||
| public Builder setPrimaryAction(JdbcSelect primaryAction) { | ||
| assert primaryAction instanceof JdbcOperationQuerySelect; | ||
| this.primaryAction = (JdbcOperationQuerySelect) primaryAction; | ||
| return this; | ||
| } | ||
|
|
||
| @SuppressWarnings("UnusedReturnValue") | ||
| @Override | ||
| public Builder setLoadedValuesCollector(LoadedValuesCollector loadedValuesCollector) { | ||
| this.loadedValuesCollector = loadedValuesCollector; | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public Builder setLockTimeoutType(LockTimeoutType lockTimeoutType) { | ||
| this.lockTimeoutType = lockTimeoutType; | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public Builder setLockingSupport(LockingSupport lockingSupport) { | ||
| this.lockingSupport = lockingSupport; | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public Builder setLockOptions(LockOptions lockOptions) { | ||
| this.lockOptions = lockOptions; | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public Builder setLockingTarget(QuerySpec lockingTarget) { | ||
| this.lockingTarget = lockingTarget; | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public Builder setLockingClauseStrategy(LockingClauseStrategy lockingClauseStrategy) { | ||
| this.lockingClauseStrategy = lockingClauseStrategy; | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public Builder setIsFollowOnLockStrategy(boolean isFollonOnLockStrategy) { | ||
| this.isFollonOnLockStrategy = isFollonOnLockStrategy; | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public JdbcSelect build() { | ||
| if ( lockTimeoutType == LockTimeoutType.CONNECTION ) { | ||
| addSecondaryActionPair( | ||
| new ReactiveLockTimeoutHandler( | ||
| lockOptions.getTimeout(), | ||
| ReactiveConnectionLockTimeoutStrategyBuilder.build( lockingSupport.getConnectionLockTimeoutStrategy() ) | ||
| ) | ||
| ); | ||
| } | ||
| if ( isFollonOnLockStrategy ) { | ||
| ReactiveFollowOnLockingAction.apply( lockOptions, lockingTarget, lockingClauseStrategy, this ); | ||
| } | ||
|
|
||
| if ( preActions == null && postActions == null ) { | ||
| assert loadedValuesCollector == null; | ||
| return primaryAction; | ||
| } | ||
| final PreAction[] preActions = toPreActionArray( this.preActions ); | ||
| final PostAction[] postActions = toPostActionArray( this.postActions ); | ||
| return new ReactiveJdbcSelectWithActions( primaryAction, loadedValuesCollector, preActions, postActions ); | ||
| } | ||
|
|
||
| /** | ||
| * Appends the {@code actions} to the growing list of pre-actions, | ||
| * executed (in order) after all currently registered actions. | ||
| * | ||
| * @return {@code this}, for method chaining. | ||
| */ | ||
| @Override | ||
| public Builder appendPreAction(PreAction... actions) { | ||
| if ( preActions == null ) { | ||
| preActions = new ArrayList<>(); | ||
| } | ||
| Collections.addAll( preActions, actions ); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Prepends the {@code actions} to the growing list of pre-actions | ||
| * | ||
| * @return {@code this}, for method chaining. | ||
| */ | ||
| @Override | ||
| public Builder prependPreAction(PreAction... actions) { | ||
| if ( preActions == null ) { | ||
| preActions = new ArrayList<>(); | ||
| } | ||
| // todo (DatabaseOperation) : should we invert the order of the incoming actions? | ||
| Collections.addAll( preActions, actions ); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Appends the {@code actions} to the growing list of post-actions | ||
| * | ||
| * @return {@code this}, for method chaining. | ||
| */ | ||
| @Override | ||
| public Builder appendPostAction(PostAction... actions) { | ||
| if ( postActions == null ) { | ||
| postActions = new ArrayList<>(); | ||
| } | ||
| Collections.addAll( postActions, actions ); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Prepends the {@code actions} to the growing list of post-actions | ||
| * | ||
| * @return {@code this}, for method chaining. | ||
| */ | ||
| @Override | ||
| public Builder prependPostAction(PostAction... actions) { | ||
| if ( postActions == null ) { | ||
| postActions = new ArrayList<>(); | ||
| } | ||
| // todo (DatabaseOperation) : should we invert the order of the incoming actions? | ||
| Collections.addAll( postActions, actions ); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Adds a secondary action pair. | ||
| * Assumes the {@code action} implements both {@linkplain PreAction} and {@linkplain PostAction}. | ||
| * | ||
| * @return {@code this}, for method chaining. | ||
| * | ||
| * @apiNote Prefer {@linkplain #addSecondaryActionPair(PreAction, PostAction)} to avoid | ||
| * the casts needed here. | ||
| * @see #prependPreAction | ||
| * @see #appendPostAction | ||
| */ | ||
| @Override | ||
| public Builder addSecondaryActionPair(SecondaryAction action) { | ||
| return addSecondaryActionPair( (PreAction) action, (PostAction) action ); | ||
| } | ||
|
|
||
| /** | ||
| * Adds a PreAction/PostAction pair. | ||
| * | ||
| * @return {@code this}, for method chaining. | ||
| * | ||
| * @see #prependPreAction | ||
| * @see #appendPostAction | ||
| */ | ||
| @Override | ||
| public Builder addSecondaryActionPair(PreAction preAction, PostAction postAction) { | ||
| prependPreAction( preAction ); | ||
| appendPostAction( postAction ); | ||
| return this; | ||
| } | ||
|
|
||
| static PreAction[] toPreActionArray(List<PreAction> actions) { | ||
| if ( CollectionHelper.isEmpty( actions ) ) { | ||
| return null; | ||
| } | ||
| return actions.toArray( new PreAction[0] ); | ||
| } | ||
|
|
||
| static PostAction[] toPostActionArray(List<PostAction> actions) { | ||
| if ( CollectionHelper.isEmpty( actions ) ) { | ||
| return null; | ||
| } | ||
| return actions.toArray( new PostAction[0] ); | ||
| } | ||
|
|
||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.