Skip to content

Commit e840772

Browse files
committed
Fixing 2nd level cache, finally
1 parent ea8c6c5 commit e840772

File tree

7 files changed

+53
-16
lines changed

7 files changed

+53
-16
lines changed

core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/PersistenceContext.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@
186186
import org.apache.syncope.core.persistence.jpa.dao.repo.UserRepoExtImpl;
187187
import org.apache.syncope.core.persistence.jpa.dao.repo.WAConfigRepo;
188188
import org.apache.syncope.core.persistence.jpa.entity.task.JPATaskUtilsFactory;
189+
import org.apache.syncope.core.persistence.jpa.hibernate.DomainJCacheRegionFactory;
190+
import org.apache.syncope.core.persistence.jpa.hibernate.Jackson3JsonFormatMapper;
189191
import org.apache.syncope.core.persistence.jpa.spring.CommonEntityManagerFactoryConf;
190192
import org.apache.syncope.core.persistence.jpa.spring.DomainRoutingEntityManagerFactory;
191193
import org.apache.syncope.core.persistence.jpa.spring.MultiJarAwarePersistenceUnitPostProcessor;
@@ -237,6 +239,7 @@ public CommonEntityManagerFactoryConf commonEMFConf(
237239
jpaPropertyMap.put("hibernate.javax.cache.provider", props.getCacheProvider());
238240
Optional.ofNullable(props.getCacheURI()).
239241
ifPresent(cacheURI -> jpaPropertyMap.put("hibernate.javax.cache.uri", cacheURI));
242+
jpaPropertyMap.put("hibernate.cache.region.factory_class", DomainJCacheRegionFactory.class.getName());
240243

241244
// until https://hibernate.atlassian.net/browse/HHH-19890 https://github.com/hibernate/hibernate-orm/pull/11357
242245
jpaPropertyMap.put("hibernate.type.json_format_mapper", Jackson3JsonFormatMapper.class.getName());

core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/dao/repo/NotificationRepo.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,4 @@
2424

2525
public interface NotificationRepo
2626
extends ListCrudRepository<JPANotification, String>, NotificationRepoExt, NotificationDAO {
27-
2827
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.syncope.core.persistence.jpa.hibernate;
20+
21+
import org.hibernate.cache.jcache.internal.JCacheRegionFactory;
22+
23+
public class DomainJCacheRegionFactory extends JCacheRegionFactory {
24+
25+
private static final long serialVersionUID = -6813667559684397717L;
26+
27+
@Override
28+
protected void releaseFromUse() {
29+
// nothing to release, there might be other Domains using the cacheManager instance
30+
}
31+
}

core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/Jackson3JsonFormatMapper.java renamed to core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/hibernate/Jackson3JsonFormatMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.apache.syncope.core.persistence.jpa;
19+
package org.apache.syncope.core.persistence.jpa.hibernate;
2020

2121
import java.lang.reflect.Type;
2222
import java.util.List;

core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/spring/CommonEntityManagerFactoryConf.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.sql.Connection;
2323
import java.util.HashMap;
2424
import java.util.Map;
25+
import java.util.Optional;
2526
import java.util.concurrent.ConcurrentHashMap;
2627
import javax.sql.DataSource;
2728
import org.apache.syncope.core.persistence.api.DomainHolder;
@@ -98,8 +99,6 @@ public void setPersistenceUnitPostProcessors(final PersistenceUnitPostProcessor.
9899
}
99100

100101
public void setJpaPropertyMap(final Map<String, ?> jpaProperties) {
101-
if (jpaProperties != null) {
102-
this.jpaPropertyMap.putAll(jpaProperties);
103-
}
102+
Optional.ofNullable(jpaProperties).ifPresent(jpaPropertyMap::putAll);
104103
}
105104
}

core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/spring/DomainEntityManagerFactoryBean.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.syncope.core.persistence.jpa.ConnectorManagerRemoteCommitListener;
2828
import org.apache.syncope.core.persistence.jpa.entity.JPAConnInstance;
2929
import org.apache.syncope.core.persistence.jpa.entity.JPAExternalResource;
30+
import org.hibernate.cache.spi.support.RegionNameQualifier;
3031
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
3132

3233
/**
@@ -42,15 +43,13 @@ public class DomainEntityManagerFactoryBean extends LocalContainerEntityManagerF
4243
public void setCommonEntityManagerFactoryConf(final CommonEntityManagerFactoryConf commonEMFConf) {
4344
super.setJpaPropertyMap(commonEMFConf.getJpaPropertyMap());
4445

45-
if (commonEMFConf.getPackagesToScan() != null) {
46-
super.setPackagesToScan(commonEMFConf.getPackagesToScan());
47-
}
46+
Optional.ofNullable(commonEMFConf.getPackagesToScan()).
47+
ifPresent(super::setPackagesToScan);
4848

4949
super.setValidationMode(commonEMFConf.getValidationMode());
5050

51-
if (commonEMFConf.getPersistenceUnitPostProcessors() != null) {
52-
super.setPersistenceUnitPostProcessors(commonEMFConf.getPersistenceUnitPostProcessors());
53-
}
51+
Optional.ofNullable(commonEMFConf.getPersistenceUnitPostProcessors()).
52+
ifPresent(super::setPersistenceUnitPostProcessors);
5453
}
5554

5655
public void setConnectorManagerRemoteCommitListener(
@@ -64,13 +63,15 @@ protected void postProcessEntityManagerFactory(final EntityManagerFactory emf, f
6463
super.postProcessEntityManagerFactory(emf, pui);
6564

6665
Optional.ofNullable(Caching.getCachingProvider().getCacheManager().
67-
getCache(JPAConnInstance.class.getName())).
66+
getCache(RegionNameQualifier.INSTANCE.qualify(
67+
pui.getPersistenceUnitName(), JPAConnInstance.class.getName()))).
6868
ifPresent(cache -> cache.registerCacheEntryListener(
6969
new MutableCacheEntryListenerConfiguration<Object, Object>(
7070
FactoryBuilder.factoryOf(connectorManagerRemoteCommitListener),
7171
null, false, false)));
7272
Optional.ofNullable(Caching.getCachingProvider().getCacheManager().
73-
getCache(JPAExternalResource.class.getName())).
73+
getCache(RegionNameQualifier.INSTANCE.qualify(
74+
pui.getPersistenceUnitName(), JPAExternalResource.class.getName()))).
7475
ifPresent(cache -> cache.registerCacheEntryListener(
7576
new MutableCacheEntryListenerConfiguration<Object, Object>(
7677
FactoryBuilder.factoryOf(connectorManagerRemoteCommitListener),

core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/spring/DomainRoutingEntityManagerFactory.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,15 @@ public DomainRoutingEntityManagerFactory(final CommonEntityManagerFactoryConf co
6464
protected void addToJpaPropertyMap(
6565
final DomainEntityManagerFactoryBean emf,
6666
final JpaVendorAdapter vendorAdapter,
67-
final String dbSchema) {
67+
final String dbSchema,
68+
final String domain) {
6869

6970
emf.getJpaPropertyMap().putAll(vendorAdapter.getJpaPropertyMap());
7071

7172
Optional.ofNullable(dbSchema).
7273
ifPresent(s -> emf.getJpaPropertyMap().put("hibernate.default_schema", s));
74+
75+
emf.getJpaPropertyMap().put("hibernate.cache.region_prefix", domain);
7376
}
7477

7578
public void master(
@@ -93,7 +96,8 @@ public void master(
9396
addToJpaPropertyMap(
9497
emf,
9598
vendorAdapter,
96-
props.getDomain().getFirst().getDbSchema());
99+
props.getDomain().getFirst().getDbSchema(),
100+
SyncopeConstants.MASTER_DOMAIN);
97101

98102
emf.afterPropertiesSet();
99103

@@ -117,7 +121,7 @@ public void domain(
117121
emf.setCommonEntityManagerFactoryConf(commonEMFConf);
118122
emf.setConnectorManagerRemoteCommitListener(new ConnectorManagerRemoteCommitListener(this, domain.getKey()));
119123

120-
addToJpaPropertyMap(emf, vendorAdapter, domain.getDbSchema());
124+
addToJpaPropertyMap(emf, vendorAdapter, domain.getDbSchema(), domain.getKey());
121125

122126
emf.afterPropertiesSet();
123127

0 commit comments

Comments
 (0)