codeconsole opened a new pull request, #16369:
URL: https://github.com/apache/grails-core/pull/16369

   Builds on #16368, which adds `GormRegistry.withConnectionScope`. The first 
six commits here are #16368's and drop out once it merges; this PR's change is 
the last commit.
   
   `Book.moreBooks.withTransaction { }` opens the `moreBooks` session and 
transaction, but the calls on `Book` inside it used the default connection:
   
   | Implementation | `book.save()` inside `Book.moreBooks.withTransaction { }` 
|
   |---|---|
   | MongoDB | written to the default database |
   | Neo4j | written to the default server, or `NoTransactionException` |
   | Hibernate 5 and 7 | `HibernateException: No Session found for current 
thread` |
   
   They now use the connection the block opened:
   
   ```groovy
   ZipCode.auditing.withTransaction {
       def zipCode = ZipCode.get(42)    // read from 'auditing'
       zipCode.code = '99501'
       zipCode.save()                   // written to 'auditing', in its 
transaction
   }
   ```
   
   `withSession`, `withNewSession`, `withStatelessSession`, `withTransaction` 
and `withNewTransaction` on a named connection's API run their closure in the 
same connection scope `withConnection` uses. An operation inside the block that 
names a connection keeps it, and a qualifier that is not a connection name, 
such as a DISCRIMINATOR tenant id from `withTenant`, is left alone.
   
   This changes behaviour for code that reached the default connection from 
inside such a block by calling the class directly. The refresh-lock specs in 
both Hibernate modules did, and now name the default connection instead:
   
   ```groovy
   Book.secondary.withTransaction {
       Book.'default'.withSession { Book.count() }    // counts the default 
database
   }
   ```
   
   A session or transaction opened through the default connection's API undoes 
the enclosing block for its duration, so the calls on the class inside it reach 
the default connection again.
   
   The Grails guide's multiple datasources section and the Hibernate 5, 
Hibernate 7, MongoDB and Neo4j guides describe it. The Grails 8 upgrade guide 
(section 72), the MongoDB and Neo4j upgrade notes, and the MongoDB and Neo4j 
release notes cover the change.
   


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