apache / apache/grails-core

Hibernate Session Mode Not Preserved after Failed Validation

Open
#14,493 0 comments 0 reactions 0 assignees View on GitHub
relates-to: grails-data-hibernate5
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

We're trying to capture any failed validations for GORM objects created in a batch job, and persisting those as GORM objects. Ultimately we found that any validation error that occurred set the session flush mode from COMMIT to MANUAL, which was surprising.
In searching for the desired behavior, I saw an inconsistency.

* https://github.com/grails/grails-data-mapping/blob/aab03ddb501e8ad33568403c7f59a816c9d3ee62/grails-datastore-gorm/src/main/groovy/org/grails/datastore/gorm/GormValidationApi.groovy#L139 the previous flush mode is preserved in all cases
* https://github.com/grails/gorm-hibernate5/blob/14cf71464f8118d84c4732e0990951ef48b84264/grails-datastore-gorm-hibernate5/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateGormValidationApi.groovy#L91 the previous flush mode is only preserved if the object passed validation.

So which is the intended behavior? Personally I wouldn't expect the validate() method to permanently change the flush mode for the session, but there may be some use case I'm not considering.

### Task List

- [x] Steps to reproduce provided
- [x] Stacktrace (if present) provided
- [x] Example that reproduces the problem uploaded to Github
- [x] Full description of the issue provided (see below)

### Steps to Reproduce

1. Validate an invalid GORM object from within a transaction

### Expected Behaviour

The flush mode of the session stays as COMMIT

### Actual Behaviour

The flush mode of the session is changed to MANUAL

### Environment Information

- **Operating System**: Fedora Linux 33
- **GORM Version:** 7.0.6.RELEASE
- **Grails Version (if using Grails):** 4.0.5
- **JDK Version:**
```
openjdk version "1.8.0_265"
OpenJDK Runtime Environment Corretto-8.265.01.1 (build 1.8.0_265-b01)
OpenJDK 64-Bit Server VM Corretto-8.265.01.1 (build 25.265-b01, mixed mode)
```

### Example Application

- https://github.com/joemccall86/session-flush-mode-test

Contributor guide

Open the contributing guide

Research direction

Start with grails-datastore-gorm-hibernate5/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateGormValidationApi.groovy and compare its flush-mode handling with grails-datastore-gorm/src/main/groovy/org/grails/datastore/gorm/GormValidationApi.groovy. Reproduce validation of an invalid GORM object inside a transaction, then verify that the session remains in COMMIT mode after validation fails.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.