jdaugherty commented on code in PR #15779: URL: https://github.com/apache/grails-core/pull/15779#discussion_r3561108601
########## grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/core/ThreadLocalSessionResolver.groovy: ########## @@ -0,0 +1,65 @@ +/* + * 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 + * + * https://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.grails.datastore.mapping.core + +import groovy.transform.CompileStatic + +/** + * A default thread-bound SessionResolver + * + * @author borinquenkid + * @since 8.0 + */ +@CompileStatic +class ThreadLocalSessionResolver<S extends Session> implements SessionResolver<S> { + + private final ThreadLocal<S> currentSession = new ThreadLocal<>() + private final ThreadLocal<Map<String, S>> qualifiedSessions = ThreadLocal.<Map<String, S>> withInitial { new HashMap<String, S>() } + + @Override + S resolve() { + return currentSession.get() + } + + @Override + S resolve(String qualifier) { + return qualifiedSessions.get().get(qualifier) + } + + @Override + void bind(S session) { + currentSession.set(session) + // Note: In a production scenario, we'd need to link the session's datastore qualifier here. Review Comment: The independent ThreadLocal source has been replaced with the shared SessionHolder, but nested resolver cleanup is still incomplete. `bind()` pushes onto the holder while `unbind()` removes the entire holder, so `bind(A); bind(B); unbind()` loses A instead of restoring it. I am leaving this thread open and adding a current-line comment. ########## grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/core/AbstractDatastore.java: ########## @@ -122,6 +174,36 @@ public void destroy() { public void setApplicationContext(ApplicationContext ctx) { applicationContext = ctx; + if (ctx instanceof ApplicationEventPublisher) { + this.applicationEventPublisher = (ApplicationEventPublisher) ctx; + } + else if (ctx == null && !(this.applicationEventPublisher instanceof DefaultApplicationEventPublisher)) { + this.applicationEventPublisher = new DefaultApplicationEventPublisher(); + } + } + + public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { + this.applicationEventPublisher = applicationEventPublisher; + } + + /** + * Adds an application listener to the datastore + * @param listener The listener + */ + public void addApplicationListener(ApplicationListener<?> listener) { + if (applicationEventPublisher instanceof ConfigurableApplicationContext) { + ((ConfigurableApplicationContext) applicationEventPublisher).addApplicationListener(listener); + } else if (applicationEventPublisher instanceof DefaultApplicationEventPublisher) { + ((DefaultApplicationEventPublisher) applicationEventPublisher).addApplicationListener(listener); + } + else { + try { + Method method = applicationEventPublisher.getClass().getMethod("addApplicationListener", ApplicationListener.class); Review Comment: Registration now follows the virtual getter, which fixes the separate-field problem for concrete configurable publishers. The generic `ApplicationEventPublisher` path still catches reflection failure and drops the listener, and the added test explicitly accepts that outcome. I am leaving this thread open and adding a current-line comment. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
