Comments (7)
JpaRepositoryFactory
isn't tied to multi-tenancy identifiers. Upon application startup we need to obtain the EntityManager
for various initialization tasks, verification of queries and introspection of managed types to properly start up the repository infrastructure.
At some point, this has to happen. Since repositories aren't tied to tenant information, what would be an alternative?
from spring-data-jpa.
I understand and agree with your point. I just think there must be another way to retrieve the extractor
The line causing problem at his point is
Class<?> entityManagerType = em.getDelegate().getClass();
which actually makes a connection to the database (which forces the developer to specify a tenant identifier for the connection to be successful to the datasource).
from spring-data-jpa.
Since you're already looking into the issue, care to investigate an approach that would work for you?
from spring-data-jpa.
I briefly thought about switching to checking the EMF for its type rather than the delegate, but it turns out both Hibernate's implementation of ….getEntityManagerFactory()
and ….getDelegate()
call checkOpen()
. I am assuming that that call is triggering the opening of a connection, as getDelegate()
simply returns this
.
To me there are two aspects to this:
- Unless there's a way for us to detect the actual JPA persistence provider from an
EntityManager
instance, it's gonna be hard for us to fix the problem. We could inspect Spring configuration setup, but that would couple our detection logic to more to expecting developers to set up their JPA infrastructure through it. While that's probably given for most of our user base, it might not be the case for all. - If we were able to delay the call to the
EntityManager
in the constructor of the repository factory, would that really help? As Mark pointed out, at some point we will have to interact with theEntityManager
to validate queries. If any of those steps requires a connection to be established (I don't know whether that's true or not) you'd still have to answer the question of how to obtain a tenant identifier from the initialization process in general. How would you like to provide that?
We could still give the EMF inspection a try though as Spring wraps the EM into a proxy that delegates calls to getEntityManagerFactory()
to return the Spring-managed EMF directly.
from spring-data-jpa.
Hello Olivier,
Regarding the second point, I don't think you can the call to the EntityManager. This means that you would need to delay it until the first call to the persistence layer done by the application.
Regarding your first point, the problem is getDelegate() which is implementation specific. I checked for Hibernate and EclipseLink - both call checkOpen() to simply open a connection before returning 'this'.
Hibernate:
@Override public Object getDelegate() { checkOpen(); return this; }
EclipseLink:
@Override public Object getDelegate() { try { verifyOpen(); return this; } catch (RuntimeException e) { setRollbackOnly(); throw e; } }
Honestly, I think we need to update the JPA spec to add em.getDelegateClass() method. This would be much much cleaner.
from spring-data-jpa.
I briefly tried to use em.getMetaModel() to try resolve the issue. but those also make a call to checkOpen() - both in EclipseLink as well as Hibernate. That's a dead end.
from spring-data-jpa.
For the time being we do not have a workaround that would work reliably, hence closing that ticket as it isn't actionable.
from spring-data-jpa.
Related Issues (20)
- Ordering JPA statement with SUM function HOT 1
- Upgrade to Hibernate 6.4.6.Final
- hibernate6.5 fix @Query doesn't check constructor field type issue HOT 1
- JPQL and HQL parser issue with class projection when FQDN includes "exp" HOT 1
- `HqlParserUnitTests` is flaky
- Spring Data JPA generates incorrect query when using pagination and semicolon in native query HOT 3
- @Query("delete from xxxxx") SQLGrammarException: could not execute statement HOT 9
- @Query("delete from xxxxx") SQLGrammarException: could not execute statement HOT 1
- Wrong handling of positional INOUT parameters when extracting output parameters HOT 9
- entity basePackages in third-party jars HOT 2
- Add `camelCase` to `snake_case` fallback for native query projections HOT 6
- 3.2.x Auditing behavior fails with composite keys using @IdClass but succeeds with @Embeddable HOT 3
- Unexpected Quoting on Column Name after Spring Boot 3 Migration HOT 3
- findById ignores @SQLRestriction annotation when performed in the same transaction as the save HOT 8
- The getId method was not called HOT 2
- Upgrade to Hibernate 6.5.1 HOT 1
- Remove duplicate plugin setup from parent pom.
- Invalid value for NanoOfSecond between 12am and 1am HOT 2
- Sorting of Pageable.unpaged(sort) is ignored by JPA Repository HOT 2
- Different `property` is used in `Sort.Order` method HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spring-data-jpa.