apache / apache/grails-core

withNewSession behavior is inconsistent with the documentation and seems contrary to it's purpose

Open
#14,556 5 comments 2 reactions 1 assignee Claimed by @jdaugherty View on GitHub
relates-to: gorm
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

The docs for `withNewSession` say (emphasis added):

> Provides a way to execute code within the context of a new Hibernate session which *shares the same transactional (JDBC Connection)* resource as the currently bound session.

However, several comments by Graeme and observed behavior show that it is now opening a new connection by design:

https://github.com/grails/grails-core/issues/10599#issuecomment-296542627
https://github.com/grails/grails-core/issues/10503#issuecomment-283313398

It used to be the case that you needed to use `withNewSession` to isolate certain updates/flushes from the outer session. For instance, it was common to use it in a custom validator to prevent polluting or accidentally flushing the outer session. It was also common to use it in the `before*` type callback methods, if querying data, to prevent accidental flushes.

Some of that is remedied by using `FlushMode.COMMIT` as the default, but it still seems like `withNewSession` can't really be used that way at all anymore.

If it starts a new connection, then by definition it can't share the transactional context (presuming no XA or anything as far as I know), so the context is completely different.

I'm struggling to understand how this was a "bug in withNewSession" and not a fundamental change in what and how this method is used.

At a minimum, the documentation needs to change to reflect the change in behavior and possibly give some references to how or why it should be used.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.