MongoDBDialect uses shared Parboiled parser across multiple threads, while the parser is NOT thread safe

Description

When executing multiple (same) native queries against MongoDB (using createNativeQuery), following error happens easily:

java.lang.IllegalArgumentException: Cannot peek beyond the bottom of the stack
at org.parboiled.MatcherContext.runMatcher(MatcherContext.java:367) ~[parboiled-core-1.1.8.jar:1.1.8]
at org.parboiled.matchers.SequenceMatcher.match(SequenceMatcher.java:46) ~[parboiled-core-1.1.8.jar:1.1.8]
at org.parboiled.parserunners.BasicParseRunner.match(BasicParseRunner.java:77) ~[parboiled-core-1.1.8.jar:1.1.8]

Parboiled parsers are not thread safe - https://github.com/sirthias/parboiled/wiki/Thread-Safety

Yet the MongoDialect uses shared, static parser for native queries:

The access to this property is not synchronized.

Wrapping query execution (in the app) in synchronized (globalLock) mitigates this issue instantly and completely - but it hurts performance a lot as no queries can be executed in parallel.

Environment

Hibernate 5.3.7
OpenJDK 11
Spring Boot 2.1
MongoDB 3.4

Status

Assignee

gitdude

Reporter

gitdude

Labels

None

Worked in

None

Feedback Requested

None

Feedback Requested By

None

backPortable

None

Community Help Wanted

None

Suitable for new contributors

None

Requires Release Note

None

backportDecision

None

backportReEvaluate

None

Components

Fix versions

Affects versions

5.4.0.Final

Priority

Blocker
Configure