Repository navigation
Enlist JPA transaction in JpaMapStorageProvider.getStorage #11328
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
Merged
Merged
Changes from all commits
Commits
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
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
77 changes: 77 additions & 0 deletions
77
model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/JpaTransactionWrapper.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,77 @@ | ||
| /* | ||
| * Copyright 2022 Red Hat, Inc. and/or its affiliates | ||
| * and other contributors as indicated by the @author tags. | ||
| * | ||
| * Licensed 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.keycloak.models.map.storage.jpa; | ||
|
|
||
| import javax.persistence.EntityTransaction; | ||
| import javax.persistence.PersistenceException; | ||
|
|
||
| import org.jboss.logging.Logger; | ||
| import org.keycloak.connections.jpa.PersistenceExceptionConverter; | ||
| import org.keycloak.models.KeycloakTransaction; | ||
|
|
||
| /** | ||
| * Wraps an {@link EntityTransaction} as a {@link KeycloakTransaction} so it can be enlisted in {@link org.keycloak.models.KeycloakTransactionManager}. | ||
| * | ||
| * @author <a href="mailto:sguilhen@redhat.com">Stefan Guilhen</a> | ||
| */ | ||
| public class JpaTransactionWrapper implements KeycloakTransaction { | ||
|
|
||
| private static final Logger logger = Logger.getLogger(JpaTransactionWrapper.class); | ||
|
|
||
| private final EntityTransaction transaction; | ||
|
|
||
| public JpaTransactionWrapper(EntityTransaction transaction) { | ||
| this.transaction = transaction; | ||
| } | ||
|
|
||
| @Override | ||
| public void begin() { | ||
| logger.tracef("tx %d: begin", hashCode()); | ||
| this.transaction.begin(); | ||
| } | ||
|
|
||
| @Override | ||
| public void commit() { | ||
| try { | ||
| logger.tracef("tx %d: commit", hashCode()); | ||
| this.transaction.commit(); | ||
| } catch(PersistenceException pe) { | ||
| throw PersistenceExceptionConverter.convert(pe.getCause() != null ? pe.getCause() : pe); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void rollback() { | ||
| logger.tracef("tx %d: rollback", hashCode()); | ||
| this.transaction.rollback(); | ||
| } | ||
|
|
||
| @Override | ||
| public void setRollbackOnly() { | ||
| this.transaction.setRollbackOnly(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean getRollbackOnly() { | ||
| return this.transaction.getRollbackOnly(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isActive() { | ||
| return this.transaction.isActive(); | ||
| } | ||
| } |
95 changes: 95 additions & 0 deletions
95
...in/java/org/keycloak/models/map/storage/jpa/hibernate/listeners/JpaAutoFlushListener.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,95 @@ | ||
| /* | ||
| * Copyright 2022. Red Hat, Inc. and/or its affiliates | ||
| * and other contributors as indicated by the @author tags. | ||
| * | ||
| * Licensed 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.keycloak.models.map.storage.jpa.hibernate.listeners; | ||
|
|
||
| import org.hibernate.FlushMode; | ||
| import org.hibernate.HibernateException; | ||
| import org.hibernate.engine.spi.ActionQueue; | ||
| import org.hibernate.event.internal.DefaultAutoFlushEventListener; | ||
| import org.hibernate.event.spi.AutoFlushEvent; | ||
| import org.hibernate.event.spi.EventSource; | ||
| import org.hibernate.internal.CoreMessageLogger; | ||
| import org.jboss.logging.Logger; | ||
|
|
||
| /** | ||
| * Extends Hibernate's {@link DefaultAutoFlushEventListener} to always flush queued inserts to allow correct handling | ||
| * of orphans of that entities in the same transactions. | ||
| * If they wouldn't be flushed, they won't be orphaned (at least not in Hibernate 5.3.24.Final). | ||
| * This class copies over all functionality of the base class that can't be overwritten via inheritance. | ||
| */ | ||
| public class JpaAutoFlushListener extends DefaultAutoFlushEventListener { | ||
|
|
||
| public static final JpaAutoFlushListener INSTANCE = new JpaAutoFlushListener(); | ||
|
|
||
| private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, DefaultAutoFlushEventListener.class.getName()); | ||
|
|
||
| public void onAutoFlush(AutoFlushEvent event) throws HibernateException { | ||
| final EventSource source = event.getSession(); | ||
| try { | ||
| source.getEventListenerManager().partialFlushStart(); | ||
|
|
||
| if (flushMightBeNeeded(source)) { | ||
| // Need to get the number of collection removals before flushing to executions | ||
| // (because flushing to executions can add collection removal actions to the action queue). | ||
| final ActionQueue actionQueue = source.getActionQueue(); | ||
| final int oldSize = actionQueue.numberOfCollectionRemovals(); | ||
| flushEverythingToExecutions(event); | ||
| if (flushIsReallyNeeded(event, source)) { | ||
| LOG.trace("Need to execute flush"); | ||
| event.setFlushRequired(true); | ||
|
|
||
| // note: performExecutions() clears all collectionXxxxtion | ||
| // collections (the collection actions) in the session | ||
| performExecutions(source); | ||
| postFlush(source); | ||
|
|
||
| postPostFlush(source); | ||
|
|
||
| if (source.getFactory().getStatistics().isStatisticsEnabled()) { | ||
| source.getFactory().getStatistics().flush(); | ||
| } | ||
| } else { | ||
| LOG.trace("Don't need to execute flush"); | ||
| event.setFlushRequired(false); | ||
| actionQueue.clearFromFlushNeededCheck(oldSize); | ||
| } | ||
| } | ||
| } finally { | ||
| source.getEventListenerManager().partialFlushEnd( | ||
| event.getNumberOfEntitiesProcessed(), | ||
| event.getNumberOfEntitiesProcessed() | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private boolean flushIsReallyNeeded(AutoFlushEvent event, final EventSource source) { | ||
| return source.getHibernateFlushMode() == FlushMode.ALWAYS | ||
| // START OF FIX for auto-flush-mode on inserts that might later be deleted in same transaction | ||
| || source.getActionQueue().numberOfInsertions() > 0 | ||
| // END OF FIX | ||
| || source.getActionQueue().areTablesToBeUpdated(event.getQuerySpaces()); | ||
| } | ||
|
|
||
| private boolean flushMightBeNeeded(final EventSource source) { | ||
| return !source.getHibernateFlushMode().lessThan(FlushMode.AUTO) | ||
| && source.getDontFlushFromFind() == 0 | ||
| && (source.getPersistenceContext().getNumberOfManagedEntities() > 0 || | ||
| source.getPersistenceContext().getCollectionEntries().size() > 0); | ||
| } | ||
|
|
||
| } |
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
29 changes: 29 additions & 0 deletions
29
model/map-jpa/src/main/resources/META-INF/jpa-aggregate-changelog.xml
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,29 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
| Copyright 2022 Red Hat, Inc. and/or its affiliates | ||
| and other contributors as indicated by the @author tags. | ||
|
|
||
| Licensed 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. | ||
| --> | ||
|
|
||
|
|
||
| <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd"> | ||
| <!-- aggregate changelog for cockroachdb --> | ||
| <include file="META-INF/jpa-auth-sessions-changelog.xml"/> | ||
| <include file="META-INF/jpa-client-scopes-changelog.xml"/> | ||
| <include file="META-INF/jpa-clients-changelog.xml"/> | ||
| <include file="META-INF/jpa-groups-changelog.xml"/> | ||
| <include file="META-INF/jpa-realms-changelog.xml"/> | ||
| <include file="META-INF/jpa-roles-changelog.xml"/> | ||
| <include file="META-INF/jpa-user-login-failures-changelog.xml"/> | ||
| </databaseChangeLog> |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
8000 The reason will be displayed to describe this comment to others. Learn more.
Should this be under
synchronizedguard, like e.g. here?keycloak/model/jpa/src/main/java/org/keycloak/connections/jpa/DefaultJpaConnectionProviderFactory.java
Lines 160 to 162 in b356618
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about this one. I think this boils down to the question: can
getStoragebe called concurrently within the same session?