longwa opened a new issue, #14574:
URL: https://github.com/apache/grails-core/issues/14574
In testing multi-tenancy for our application I realized that any entity
which implements MultiTenant is not eligible for 2nd level caching at all in
Hibernate.
This is currently by design in `AbstractHibernateGormStaticApi.groovy` in
the `get(...)` method:
```
if(persistentEntity.isMultiTenant()) {
// for multi-tenant entities we process get(..) via a query
(D)hibernateTemplate.execute( { Session session ->
def criteria =
session.createCriteria(persistentEntity.javaClass)
criteria.add Restrictions.idEq(id)
firePreQueryEvent(session,criteria)
def result = (D) criteria.uniqueResult()
firePostQueryEvent(session, criteria, result)
return proxyHandler.unwrap( result )
} )
}
else {
// for non multi-tenant entities we process get(..) via the
second level cache
return (D)proxyHandler.unwrap(
hibernateTemplate.get(persistentEntity.javaClass, id)
)
}
```
Hibernate 5.1 and later support cache aware multi-tenancy since the
`QueryKey` class used for querying the second level cache is tenant aware (it
includes the `tenantIdentifier` in the key).
However, it seems like Grails isn't really configuring the hibernate
multi-tenancy (or not configuring it fully) since the `sessionOptions` on the
SessionFactory configured by Grails indicates tenancy of 'NONE' even when
multi-tenancy is enable for Grails. Perhaps this is to abstract multi-tenancy
to support more datastore types, I'm not sure.
This is a huge performance impact for our application as most of our domain
objects needs to exist in both tenants and therefore don't make good use of the
2nd level cache, causing thousands of extract queries for some things.
### Steps to Reproduce
1. Create a domain object that implements MultiTenant and make it cached
(nonstrict-read-write) in our case.
2. Fetch that object multiple times in different sessions and observe
queries each time
### Expected Behaviour
Ideally, Grails should configure and defer the tenancy to Hibernate and use
the configured `hibernateTemplate.get(...)` for everything. As long as
hibernate is "tenant-aware", it looks like it should respect the tenant when
pulling from the cache.
--
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]