# 2nd level cache with multiple sources

**URL:** https://discourse.hibernate.org/t/2nd-level-cache-with-multiple-sources/8041
**Category:** Hibernate ORM
**Created:** [July 26, 2023, 3:57pm UTC](https://discourse.hibernate.org/t/2nd-level-cache-with-multiple-sources/8041 "2023-07-26T15:57:05Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Anton](https://avatars.discourse-cdn.com/v4/letter/a/d9b06d/32.png) [@Anton](https://discourse.hibernate.org/u/Anton)
#### Post date: [July 26, 2023, 3:57pm UTC](https://discourse.hibernate.org/t/2nd-level-cache-with-multiple-sources/8041/1 "2023-07-26T15:57:05Z")

</div>

Hello,

I need some help because I have added 2 data sources to my project which actually seemed to be working fine, so I decided to add ehcache as a 2nd level cache and discovered that for the data source which is annotated as `@Primary`, query caching isn’t working, even though there are hits - it generates selects every time. For the alternative datasource it works just fine. How did I found it out - just removed `@Primary` annotation from one data source and added to another and the situation got inverted. I guess it has something to do with the SessionFactories, but I don’t know what kind of configuration should be done nor I can find decent info about it.

PS: there are only 2 entities for each data source, very basic, with no relations and `@Cacheable` + `@org.hibernate.annotations.Cache` annotations. Also there are 2 repositories which do extend JPARepository and both have `@QueryHints` annotations in order to cache queries (queries are very simple - looking for an object by single value, which are not the PK). In terms of project Im using ehcache 3+, Hibernate 6+, SpringBoot 3+

Thanks in advance

---

<div class="post-metadata">

### Author: ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)
#### Post date: [July 26, 2023, 4:55pm UTC](https://discourse.hibernate.org/t/2nd-level-cache-with-multiple-sources/8041/2 "2023-07-26T16:55:17Z")

</div>

This is a Hibernate forum, not a Spring forum. Since caching works fine for one persistence unit but not the other, the problem is certainly in your configuration, but you should better ask these questions in Spring forums.

---

<div class="post-metadata">

### Author: ![Anton](https://avatars.discourse-cdn.com/v4/letter/a/d9b06d/32.png) [@Anton](https://discourse.hibernate.org/u/Anton)
#### Post date: [July 26, 2023, 10:07pm UTC](https://discourse.hibernate.org/t/2nd-level-cache-with-multiple-sources/8041/3 "2023-07-26T22:07:00Z")

</div>

Already did, but no replies so far =( Ok, thanks, will keep investigating.
