jdaugherty commented on code in PR #15425: URL: https://github.com/apache/grails-core/pull/15425#discussion_r2835772810
########## grails-test-examples/hibernate5/grails-multiple-datasources/grails-app/controllers/datasources/SecondaryBookController.groovy: ########## @@ -0,0 +1,78 @@ +/* + * 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 datasources + +import ds2.Book +import org.hibernate.Session + +class SecondaryBookController { + + def withSessionTest() { + boolean sessionObtained = false + Book.secondary.withSession { Session session -> + sessionObtained = session != null + } + render "sessionObtained:${sessionObtained}" + } + + def crudViaWithSession() { + Book.withTransaction { + new Book(title: 'OSIV Test').save(flush: true) + } + int count = 0 + Book.secondary.withSession { Session session -> + count = Book.count() Review Comment: Isn't the OSIV primarly used when the gsp is rendered, which is *after* the controller returns its model? Shouldn't we add those scenarios + Geb to confirm no issues? ########## grails-data-hibernate5/grails-plugin/src/main/groovy/org/grails/plugin/hibernate/support/GrailsOpenSessionInViewInterceptor.java: ########## @@ -84,5 +179,25 @@ public void setHibernateDatastore(AbstractHibernateDatastore hibernateDatastore) this.hibernateFlushMode = FlushMode.valueOf(defaultFlushModeName); } setSessionFactory(hibernateDatastore.getSessionFactory()); + + if (hibernateDatastore instanceof HibernateDatastore) { + HibernateDatastore hibernateDs = (HibernateDatastore) hibernateDatastore; + for (ConnectionSource<SessionFactory, HibernateConnectionSourceSettings> connectionSource : hibernateDs.getConnectionSources().getAllConnectionSources()) { + String connectionName = connectionSource.getName(); + if (!ConnectionSource.DEFAULT.equals(connectionName)) { + AbstractHibernateDatastore childDatastore = hibernateDs.getDatastoreForConnection(connectionName); + FlushMode childFlushMode; + if (childDatastore.isOsivReadOnly()) { + childFlushMode = FlushMode.MANUAL; + } + else { + childFlushMode = FlushMode.valueOf(childDatastore.getDefaultFlushModeName()); + } + additionalSessionFactories.add( + new AdditionalSessionFactoryConfig(childDatastore.getSessionFactory(), childFlushMode) Review Comment: Let's also save the name & use that in the debug output so we can make it easier to troubleshoot if an issue arises? -- 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]
