jamesfredley commented on code in PR #16784:
URL: https://github.com/apache/grails-core/pull/16784#discussion_r4409717660


##########
grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/GrailsHibernateTemplate.java:
##########
@@ -153,6 +168,60 @@ public <T> T execute(Closure<T> callable) {
     public <T> T executeWithNewSession(final Closure<T> callable) {
         SessionHolder sessionHolder = (SessionHolder) 
TransactionSynchronizationManager.getResource(sessionFactory);
         SessionHolder previousHolder = sessionHolder;
+
+        // If the outer session is open but physically disconnected, acquire 
its physical connection
+        // before saving previousConnectionHolder and synchronizations, and 
before unbinding and entering
+        // the new session scope. Otherwise, lazy loading on outer session 
entities inside
+        // withNewSession { withTransaction { ... } } would borrow the inner 
transaction's
+        // thread-bound connection and be left with a closed connection when 
the inner transaction commits.
+        // This workaround is only taken when the connection provider uses 
TransactionAwareDataSourceProxy
+        // backed by LazyConnectionDataSourceProxy: con.getAutoCommit() allows 
LazyConnectionDataSourceProxy
+        // to bind the proxy without eagerly checking out a connection from 
the pool. When lazy wrapping is off
+        // or the data source is non-transaction-aware, acquiring the 
connection eagerly would check out a pooled
+        // connection unnecessarily and cause a single-connection pool to time 
out.
+        if (this.transactionAware && this.lazyConnection &&

Review Comment:
   This priming runs only when lazy wrapping is on. With dataSource.lazy=false 
and transactionAware still true, a disconnected outer session is not given its 
own connection. A lazy load inside withNewSession { withTransaction { ... } } 
then takes the inner transaction's connection, and the outer session keeps that 
connection after the inner transaction closes it. The idle outer session must 
not check out a pooled connection, and a real use of that session must not 
share the inner transaction's connection.



-- 
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]

Reply via email to