├── .gitignore ├── LICENSE ├── README.md ├── checkstyle ├── ClassHeader.txt ├── checkstyle.xml └── suppressions.xml ├── findbugs └── findbugs-exclude.xml ├── hazelcast-hibernate3 ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hazelcast │ │ │ └── hibernate │ │ │ ├── AbstractHazelcastCacheRegionFactory.java │ │ │ ├── CacheEnvironment.java │ │ │ ├── HazelcastCacheRegionFactory.java │ │ │ ├── HazelcastLocalCacheRegionFactory.java │ │ │ ├── HazelcastTimestamper.java │ │ │ ├── RegionCache.java │ │ │ ├── VersionAwareMapMergePolicy.java │ │ │ ├── access │ │ │ ├── AbstractAccessDelegate.java │ │ │ ├── AccessDelegate.java │ │ │ ├── NonStrictReadWriteAccessDelegate.java │ │ │ ├── ReadOnlyAccessDelegate.java │ │ │ ├── ReadWriteAccessDelegate.java │ │ │ └── package-info.java │ │ │ ├── distributed │ │ │ ├── AbstractRegionCacheEntryProcessor.java │ │ │ ├── IMapRegionCache.java │ │ │ ├── LockEntryProcessor.java │ │ │ ├── UnlockEntryProcessor.java │ │ │ ├── UpdateEntryProcessor.java │ │ │ └── package-info.java │ │ │ ├── instance │ │ │ ├── HazelcastAccessor.java │ │ │ ├── HazelcastClientLoader.java │ │ │ ├── HazelcastInstanceFactory.java │ │ │ ├── HazelcastInstanceLoader.java │ │ │ ├── IHazelcastInstanceLoader.java │ │ │ └── package-info.java │ │ │ ├── local │ │ │ ├── CleanupService.java │ │ │ ├── Invalidation.java │ │ │ ├── LocalRegionCache.java │ │ │ ├── Timestamp.java │ │ │ ├── TimestampsRegionCache.java │ │ │ └── package-info.java │ │ │ ├── package-info.java │ │ │ ├── region │ │ │ ├── AbstractGeneralRegion.java │ │ │ ├── AbstractHazelcastRegion.java │ │ │ ├── AbstractTransactionalDataRegion.java │ │ │ ├── CollectionRegionAccessStrategyAdapter.java │ │ │ ├── EntityRegionAccessStrategyAdapter.java │ │ │ ├── HazelcastCollectionRegion.java │ │ │ ├── HazelcastEntityRegion.java │ │ │ ├── HazelcastQueryResultsRegion.java │ │ │ ├── HazelcastRegion.java │ │ │ ├── HazelcastTimestampsRegion.java │ │ │ └── package-info.java │ │ │ └── serialization │ │ │ ├── Expirable.java │ │ │ ├── ExpiryMarker.java │ │ │ ├── Hibernate3CacheEntrySerializer.java │ │ │ ├── Hibernate3CacheEntrySerializerHook.java │ │ │ ├── Hibernate3CacheKeySerializer.java │ │ │ ├── Hibernate3CacheKeySerializerHook.java │ │ │ ├── HibernateDataSerializerHook.java │ │ │ ├── MarkerWrapper.java │ │ │ ├── Value.java │ │ │ └── package-info.java │ └── resources │ │ ├── META-INF │ │ └── services │ │ │ ├── com.hazelcast.DataSerializerHook │ │ │ └── com.hazelcast.SerializerHook │ │ └── hazelcast-community-license.txt │ └── test │ ├── java │ └── com │ │ └── hazelcast │ │ └── hibernate │ │ ├── CacheHitMissNonStrictTest.java │ │ ├── CacheHitMissReadOnlyTest.java │ │ ├── CacheHitMissReadWriteTest.java │ │ ├── CollectionCacheTest.java │ │ ├── CustomPropertiesTest.java │ │ ├── HibernateSlowTestSupport.java │ │ ├── HibernateStatisticsTestSupport.java │ │ ├── HibernateTestSupport.java │ │ ├── LocalRegionFactoryDefaultTest.java │ │ ├── LocalRegionFactorySlowTest.java │ │ ├── NativeClientTest.java │ │ ├── RegionFactoryDefaultSlowTest.java │ │ ├── RegionFactoryDefaultTest.java │ │ ├── RegionFactoryQueryTest.java │ │ ├── VersionAwareMapMergePolicyTest.java │ │ ├── access │ │ └── ReadWriteAccessDelegateTest.java │ │ ├── distributed │ │ ├── LockEntryProcessorTest.java │ │ ├── UnlockEntryProcessorTest.java │ │ └── UpdateEntryProcessorTest.java │ │ ├── entity │ │ ├── DummyEntity.java │ │ └── DummyProperty.java │ │ ├── instance │ │ └── HazelcastMockInstanceLoader.java │ │ ├── local │ │ ├── LocalRegionCacheTest.java │ │ └── TimestampsRegionCacheTest.java │ │ ├── region │ │ ├── HazelcastQueryResultsRegionTest.java │ │ └── HazelcastTimestampsRegionTest.java │ │ └── serialization │ │ ├── ExpiryMarkerTest.java │ │ ├── HibernateSerializationHookAvailableTest.java │ │ ├── HibernateSerializationHookNonAvailableTest.java │ │ └── ValueTest.java │ └── resources │ ├── com │ └── hazelcast │ │ └── hibernate │ │ └── entity │ │ ├── DummyEntity.hbm.xml │ │ └── DummyProperty.hbm.xml │ ├── hazelcast-client-custom.xml │ ├── hazelcast-custom.xml │ ├── logging.properties │ ├── test-hibernate-client.cfg.xml │ └── test-hibernate.cfg.xml ├── hazelcast-hibernate4 ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── hazelcast │ │ │ └── hibernate │ │ │ ├── AbstractHazelcastCacheRegionFactory.java │ │ │ ├── CacheEnvironment.java │ │ │ ├── HazelcastCacheRegionFactory.java │ │ │ ├── HazelcastLocalCacheRegionFactory.java │ │ │ ├── HazelcastTimestamper.java │ │ │ ├── RegionCache.java │ │ │ ├── VersionAwareMapMergePolicy.java │ │ │ ├── access │ │ │ ├── AbstractAccessDelegate.java │ │ │ ├── AccessDelegate.java │ │ │ ├── NonStrictReadWriteAccessDelegate.java │ │ │ ├── ReadOnlyAccessDelegate.java │ │ │ ├── ReadWriteAccessDelegate.java │ │ │ └── package-info.java │ │ │ ├── distributed │ │ │ ├── AbstractRegionCacheEntryProcessor.java │ │ │ ├── IMapRegionCache.java │ │ │ ├── LockEntryProcessor.java │ │ │ ├── UnlockEntryProcessor.java │ │ │ ├── UpdateEntryProcessor.java │ │ │ └── package-info.java │ │ │ ├── instance │ │ │ ├── HazelcastAccessor.java │ │ │ ├── HazelcastClientLoader.java │ │ │ ├── HazelcastInstanceFactory.java │ │ │ ├── HazelcastInstanceLoader.java │ │ │ ├── IHazelcastInstanceLoader.java │ │ │ └── package-info.java │ │ │ ├── local │ │ │ ├── CleanupService.java │ │ │ ├── Invalidation.java │ │ │ ├── LocalRegionCache.java │ │ │ ├── Timestamp.java │ │ │ ├── TimestampsRegionCache.java │ │ │ └── package-info.java │ │ │ ├── package-info.java │ │ │ ├── region │ │ │ ├── AbstractGeneralRegion.java │ │ │ ├── AbstractHazelcastRegion.java │ │ │ ├── AbstractTransactionalDataRegion.java │ │ │ ├── CollectionRegionAccessStrategyAdapter.java │ │ │ ├── EntityRegionAccessStrategyAdapter.java │ │ │ ├── HazelcastCollectionRegion.java │ │ │ ├── HazelcastEntityRegion.java │ │ │ ├── HazelcastNaturalIdRegion.java │ │ │ ├── HazelcastQueryResultsRegion.java │ │ │ ├── HazelcastRegion.java │ │ │ ├── HazelcastTimestampsRegion.java │ │ │ ├── NaturalIdRegionAccessStrategyAdapter.java │ │ │ └── package-info.java │ │ │ └── serialization │ │ │ ├── Expirable.java │ │ │ ├── ExpiryMarker.java │ │ │ ├── Hibernate41CacheEntrySerializer.java │ │ │ ├── Hibernate42CacheEntrySerializer.java │ │ │ ├── Hibernate4CacheEntrySerializerHook.java │ │ │ ├── Hibernate4CacheKeySerializer.java │ │ │ ├── Hibernate4CacheKeySerializerHook.java │ │ │ ├── HibernateDataSerializerHook.java │ │ │ ├── MarkerWrapper.java │ │ │ ├── Value.java │ │ │ └── package-info.java │ └── resources │ │ ├── META-INF │ │ └── services │ │ │ ├── com.hazelcast.DataSerializerHook │ │ │ └── com.hazelcast.SerializerHook │ │ └── hazelcast-community-license.txt │ └── test │ ├── java │ └── com │ │ └── hazelcast │ │ └── hibernate │ │ ├── CacheHitMissNonStrictTest.java │ │ ├── CacheHitMissReadOnlyTest.java │ │ ├── CacheHitMissReadWriteTest.java │ │ ├── CollectionCacheTest.java │ │ ├── CustomPropertiesTest.java │ │ ├── HibernateSlowTestSupport.java │ │ ├── HibernateStatisticsTestSupport.java │ │ ├── HibernateTestSupport.java │ │ ├── LocalRegionFactoryDefaultTest.java │ │ ├── LocalRegionFactorySlowTest.java │ │ ├── NativeClientTest.java │ │ ├── NaturalIdTest.java │ │ ├── RegionFactoryDefaultSlowTest.java │ │ ├── RegionFactoryDefaultTest.java │ │ ├── RegionFactoryQueryTest.java │ │ ├── VersionAwareMapMergePolicyTest.java │ │ ├── access │ │ └── ReadWriteAccessDelegateTest.java │ │ ├── distributed │ │ ├── LockEntryProcessorTest.java │ │ ├── UnlockEntryProcessorTest.java │ │ └── UpdateEntryProcessorTest.java │ │ ├── entity │ │ ├── AnnotatedEntity.java │ │ ├── DummyEntity.java │ │ └── DummyProperty.java │ │ ├── instance │ │ └── HazelcastMockInstanceLoader.java │ │ ├── local │ │ ├── LocalRegionCacheTest.java │ │ └── TimestampsRegionCacheTest.java │ │ ├── region │ │ ├── HazelcastQueryResultsRegionTest.java │ │ └── HazelcastTimestampsRegionTest.java │ │ └── serialization │ │ ├── ExpiryMarkerTest.java │ │ ├── HibernateSerializationHookAvailableTest.java │ │ ├── HibernateSerializationHookNonAvailableTest.java │ │ └── ValueTest.java │ └── resources │ ├── com │ └── hazelcast │ │ └── hibernate │ │ └── entity │ │ ├── DummyEntity.hbm.xml │ │ └── DummyProperty.hbm.xml │ ├── hazelcast-client-custom.xml │ ├── hazelcast-custom.xml │ ├── test-hibernate-client.cfg.xml │ └── test-hibernate.cfg.xml ├── images ├── HZLocalCacheRgnFactory.jpg └── NoteSmall.jpg └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | over/ 2 | target/ 3 | .project 4 | .classpath 5 | .settings/ 6 | .idea/ 7 | .patch 8 | .surefire-* 9 | *.iml 10 | *.ipr 11 | *.iws 12 | .DS_Store 13 | reports/ 14 | .directory 15 | atlassian-ide-plugin.xml 16 | performance*.log 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### please refer to [hazelcast-hibernate-3-and-4 branch](https://github.com/hazelcast/hazelcast-hibernate/tree/hazelcast-hibernate3-and-4) for hibernate 3 and 4 support 2 | 3 | 4 | -------------------------------------------------------------------------------- /checkstyle/ClassHeader.txt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | -------------------------------------------------------------------------------- /checkstyle/suppressions.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /findbugs/findbugs-exclude.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/pom.xml: -------------------------------------------------------------------------------- 1 | 15 | 4.0.0 16 | 17 | hazelcast-hibernate3 18 | hazelcast-hibernate3 19 | jar 20 | 21 | 22 | com.hazelcast 23 | hazelcast-hibernate 24 | 3.8.5-SNAPSHOT 25 | ../pom.xml 26 | 27 | 28 | 29 | 30 | ${project.parent.basedir} 31 | 32 | 3.6.10.Final 33 | 34 | 35 | 36 | 37 | org.hibernate 38 | hibernate-core 39 | ${hibernate.core.version} 40 | provided 41 | 42 | 43 | slf4j-api 44 | org.slf4j 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/HazelcastCacheRegionFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate; 17 | 18 | import com.hazelcast.core.HazelcastInstance; 19 | import com.hazelcast.hibernate.distributed.IMapRegionCache; 20 | import com.hazelcast.hibernate.region.HazelcastCollectionRegion; 21 | import com.hazelcast.hibernate.region.HazelcastEntityRegion; 22 | import com.hazelcast.hibernate.region.HazelcastTimestampsRegion; 23 | import org.hibernate.cache.CacheDataDescription; 24 | import org.hibernate.cache.CacheException; 25 | import org.hibernate.cache.CollectionRegion; 26 | import org.hibernate.cache.EntityRegion; 27 | import org.hibernate.cache.RegionFactory; 28 | import org.hibernate.cache.TimestampsRegion; 29 | 30 | import java.util.Properties; 31 | 32 | /** 33 | * Simple RegionFactory implementation to return Hazelcast based Region implementations 34 | */ 35 | public class HazelcastCacheRegionFactory extends AbstractHazelcastCacheRegionFactory implements RegionFactory { 36 | 37 | @SuppressWarnings("unused") 38 | public HazelcastCacheRegionFactory() { 39 | } 40 | 41 | @SuppressWarnings("unused") 42 | public HazelcastCacheRegionFactory(final HazelcastInstance instance) { 43 | super(instance); 44 | } 45 | 46 | @SuppressWarnings("unused") 47 | public HazelcastCacheRegionFactory(final Properties properties) { 48 | super(properties); 49 | } 50 | 51 | @Override 52 | public CollectionRegion buildCollectionRegion(final String regionName, final Properties properties, 53 | final CacheDataDescription metadata) throws CacheException { 54 | return new HazelcastCollectionRegion(instance, regionName, properties, metadata, 55 | new IMapRegionCache(regionName, instance, properties, metadata)); 56 | } 57 | 58 | @Override 59 | public EntityRegion buildEntityRegion(final String regionName, final Properties properties, 60 | final CacheDataDescription metadata) throws CacheException { 61 | return new HazelcastEntityRegion(instance, regionName, properties, metadata, 62 | new IMapRegionCache(regionName, instance, properties, metadata)); 63 | } 64 | 65 | @Override 66 | public TimestampsRegion buildTimestampsRegion(final String regionName, final Properties properties) 67 | throws CacheException { 68 | return new HazelcastTimestampsRegion(instance, regionName, properties, 69 | new IMapRegionCache(regionName, instance, properties, null)); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/HazelcastLocalCacheRegionFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate; 17 | 18 | import com.hazelcast.core.HazelcastInstance; 19 | import com.hazelcast.hibernate.local.LocalRegionCache; 20 | import com.hazelcast.hibernate.local.TimestampsRegionCache; 21 | import com.hazelcast.hibernate.region.HazelcastCollectionRegion; 22 | import com.hazelcast.hibernate.region.HazelcastEntityRegion; 23 | import com.hazelcast.hibernate.region.HazelcastTimestampsRegion; 24 | import org.hibernate.cache.CacheDataDescription; 25 | import org.hibernate.cache.CacheException; 26 | import org.hibernate.cache.CollectionRegion; 27 | import org.hibernate.cache.EntityRegion; 28 | import org.hibernate.cache.RegionFactory; 29 | import org.hibernate.cache.TimestampsRegion; 30 | 31 | import java.util.Properties; 32 | 33 | /** 34 | * Simple RegionFactory implementation to return Hazelcast based local Region implementations 35 | */ 36 | public class HazelcastLocalCacheRegionFactory extends AbstractHazelcastCacheRegionFactory implements RegionFactory { 37 | 38 | @SuppressWarnings("unused") 39 | public HazelcastLocalCacheRegionFactory() { 40 | } 41 | 42 | @SuppressWarnings("unused") 43 | public HazelcastLocalCacheRegionFactory(final HazelcastInstance instance) { 44 | super(instance); 45 | } 46 | 47 | @SuppressWarnings("unused") 48 | public HazelcastLocalCacheRegionFactory(final Properties properties) { 49 | super(properties); 50 | } 51 | 52 | @Override 53 | public CollectionRegion buildCollectionRegion(final String regionName, final Properties properties, 54 | final CacheDataDescription metadata) throws CacheException { 55 | final HazelcastCollectionRegion region = new HazelcastCollectionRegion(instance, 56 | regionName, properties, metadata, new LocalRegionCache(regionName, instance, metadata)); 57 | cleanupService.registerCache(region.getCache()); 58 | return region; 59 | } 60 | 61 | @Override 62 | public EntityRegion buildEntityRegion(final String regionName, final Properties properties, 63 | final CacheDataDescription metadata) throws CacheException { 64 | final HazelcastEntityRegion region = new HazelcastEntityRegion(instance, 65 | regionName, properties, metadata, new LocalRegionCache(regionName, instance, metadata)); 66 | cleanupService.registerCache(region.getCache()); 67 | return region; 68 | } 69 | 70 | @Override 71 | public TimestampsRegion buildTimestampsRegion(final String regionName, final Properties properties) 72 | throws CacheException { 73 | return new HazelcastTimestampsRegion(instance, regionName, properties, 74 | new TimestampsRegionCache(regionName, instance)); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/HazelcastTimestamper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate; 17 | 18 | import com.hazelcast.config.Config; 19 | import com.hazelcast.config.MapConfig; 20 | import com.hazelcast.core.HazelcastInstance; 21 | import com.hazelcast.logging.Logger; 22 | 23 | /** 24 | * Helper class to create timestamps and calculate timeouts based on either Hazelcast 25 | * configuration of by requesting values on the cluster. 26 | */ 27 | public final class HazelcastTimestamper { 28 | 29 | private static final int SEC_TO_MS = 1000; 30 | 31 | private HazelcastTimestamper() { 32 | } 33 | 34 | public static long nextTimestamp(HazelcastInstance instance) { 35 | if (instance == null) { 36 | throw new RuntimeException("No Hazelcast instance!"); 37 | } else if (instance.getCluster() == null) { 38 | throw new RuntimeException("Hazelcast instance has no cluster!"); 39 | } 40 | 41 | // System time in ms. 42 | return instance.getCluster().getClusterTime(); 43 | } 44 | 45 | public static int getTimeout(HazelcastInstance instance, String regionName) { 46 | try { 47 | final MapConfig cfg = instance.getConfig().findMapConfig(regionName); 48 | if (cfg.getTimeToLiveSeconds() > 0) { 49 | // TTL in ms. 50 | return cfg.getTimeToLiveSeconds() * SEC_TO_MS; 51 | } 52 | } catch (UnsupportedOperationException e) { 53 | // HazelcastInstance is instance of HazelcastClient. 54 | Logger.getLogger(HazelcastTimestamper.class).finest(e); 55 | } 56 | return CacheEnvironment.getDefaultCacheTimeoutInMillis(); 57 | } 58 | 59 | public static long getMaxOperationTimeout(HazelcastInstance instance) { 60 | String maxOpTimeoutProp = null; 61 | try { 62 | Config config = instance.getConfig(); 63 | maxOpTimeoutProp = config.getProperty(CacheEnvironment.HAZELCAST_OPERATION_TIMEOUT); 64 | } catch (UnsupportedOperationException e) { 65 | // HazelcastInstance is instance of HazelcastClient. 66 | Logger.getLogger(HazelcastTimestamper.class).finest(e); 67 | } 68 | if (maxOpTimeoutProp != null) { 69 | return Long.parseLong(maxOpTimeoutProp); 70 | } 71 | return Long.MAX_VALUE; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/RegionCache.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate; 17 | 18 | import org.hibernate.cache.access.SoftLock; 19 | 20 | import java.util.Map; 21 | 22 | /** 23 | * This interface defines an internal cached region implementation as well as a mechanism 24 | * to unmap the cache to an underlying Map data-structure 25 | */ 26 | public interface RegionCache { 27 | 28 | Object get(final Object key, final long txTimestamp); 29 | 30 | boolean insert(final Object key, final Object value, final Object currentVersion); 31 | 32 | boolean put(final Object key, final Object value, final long txTimestamp, final Object version); 33 | 34 | boolean update(final Object key, final Object newValue, final Object newVersion, final SoftLock lock); 35 | 36 | boolean remove(final Object key); 37 | 38 | SoftLock tryLock(final Object key, final Object version); 39 | 40 | void unlock(final Object key, final SoftLock lock); 41 | 42 | boolean contains(final Object key); 43 | 44 | void clear(); 45 | 46 | long size(); 47 | 48 | long getSizeInMemory(); 49 | 50 | Map asMap(); 51 | 52 | } 53 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/VersionAwareMapMergePolicy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate; 17 | 18 | import com.hazelcast.core.EntryView; 19 | import com.hazelcast.map.merge.MapMergePolicy; 20 | import com.hazelcast.nio.ObjectDataInput; 21 | import com.hazelcast.nio.ObjectDataOutput; 22 | import org.hibernate.cache.entry.CacheEntry; 23 | 24 | import java.io.IOException; 25 | 26 | /** 27 | * A merge policy implementation to handle split brain remerges based on the timestamps stored in 28 | * the values. 29 | */ 30 | public class VersionAwareMapMergePolicy implements MapMergePolicy { 31 | 32 | public Object merge(String mapName, EntryView mergingEntry, EntryView existingEntry) { 33 | final Object existingValue = existingEntry != null ? existingEntry.getValue() : null; 34 | final Object mergingValue = mergingEntry.getValue(); 35 | if (existingValue != null && existingValue instanceof CacheEntry 36 | && mergingValue != null && mergingValue instanceof CacheEntry) { 37 | 38 | final CacheEntry existingCacheEntry = (CacheEntry) existingValue; 39 | final CacheEntry mergingCacheEntry = (CacheEntry) mergingValue; 40 | final Object mergingVersionObject = mergingCacheEntry.getVersion(); 41 | final Object existingVersionObject = existingCacheEntry.getVersion(); 42 | if (mergingVersionObject != null && existingVersionObject != null 43 | && mergingVersionObject instanceof Comparable && existingVersionObject instanceof Comparable) { 44 | 45 | final Comparable mergingVersion = (Comparable) mergingVersionObject; 46 | final Comparable existingVersion = (Comparable) existingVersionObject; 47 | 48 | if (mergingVersion.compareTo(existingVersion) > 0) { 49 | return mergingValue; 50 | } else { 51 | return existingValue; 52 | } 53 | } 54 | } 55 | return mergingValue; 56 | } 57 | 58 | @Override 59 | public void writeData(ObjectDataOutput out) throws IOException { 60 | } 61 | 62 | @Override 63 | public void readData(ObjectDataInput in) throws IOException { 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/access/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | /** 17 | * Provides access interfaces/classes for Hibernate. 18 | * such as AccessDelegate, ReadWriteAccessDelegate. 19 | */ 20 | package com.hazelcast.hibernate.access; 21 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/distributed/AbstractRegionCacheEntryProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.distributed; 17 | 18 | 19 | import com.hazelcast.hibernate.serialization.Expirable; 20 | import com.hazelcast.hibernate.serialization.HibernateDataSerializerHook; 21 | 22 | import com.hazelcast.map.EntryBackupProcessor; 23 | import com.hazelcast.map.EntryProcessor; 24 | import com.hazelcast.nio.serialization.IdentifiedDataSerializable; 25 | 26 | import java.util.Map; 27 | 28 | /** 29 | * An abstract implementation of {@link EntryProcessor} which acts on a hibernate region cache 30 | * {@link com.hazelcast.core.IMap} 31 | */ 32 | 33 | public abstract class AbstractRegionCacheEntryProcessor implements EntryProcessor, 34 | EntryBackupProcessor, IdentifiedDataSerializable { 35 | 36 | @Override 37 | public int getFactoryId() { 38 | return HibernateDataSerializerHook.F_ID; 39 | } 40 | 41 | @Override 42 | public void processBackup(Map.Entry entry) { 43 | process(entry); 44 | } 45 | 46 | @Override 47 | public EntryBackupProcessor getBackupProcessor() { 48 | return this; 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/distributed/LockEntryProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.distributed; 17 | 18 | import com.hazelcast.hibernate.serialization.Expirable; 19 | import com.hazelcast.hibernate.serialization.ExpiryMarker; 20 | import com.hazelcast.hibernate.serialization.HibernateDataSerializerHook; 21 | import com.hazelcast.nio.ObjectDataInput; 22 | import com.hazelcast.nio.ObjectDataOutput; 23 | 24 | import java.io.IOException; 25 | import java.util.Map; 26 | 27 | /** 28 | * A concrete implementation of {@link com.hazelcast.map.EntryProcessor} which soft-locks 29 | * a region cached entry 30 | */ 31 | public class LockEntryProcessor extends AbstractRegionCacheEntryProcessor { 32 | 33 | private String nextMarkerId; 34 | private long timeout; 35 | private Object version; 36 | 37 | public LockEntryProcessor() { 38 | } 39 | 40 | public LockEntryProcessor(String nextMarkerId, long timeout, Object version) { 41 | this.nextMarkerId = nextMarkerId; 42 | this.timeout = timeout; 43 | this.version = version; 44 | } 45 | 46 | @Override 47 | public Expirable process(Map.Entry entry) { 48 | Expirable expirable = entry.getValue(); 49 | 50 | if (expirable == null) { 51 | expirable = new ExpiryMarker(version, timeout, nextMarkerId); 52 | } else { 53 | expirable = expirable.markForExpiration(timeout, nextMarkerId); 54 | } 55 | 56 | entry.setValue(expirable); 57 | 58 | return expirable; 59 | } 60 | 61 | @Override 62 | public void writeData(ObjectDataOutput out) throws IOException { 63 | out.writeUTF(nextMarkerId); 64 | out.writeLong(timeout); 65 | out.writeObject(version); 66 | } 67 | 68 | @Override 69 | public void readData(ObjectDataInput in) throws IOException { 70 | nextMarkerId = in.readUTF(); 71 | timeout = in.readLong(); 72 | version = in.readObject(); 73 | } 74 | 75 | @Override 76 | public int getId() { 77 | return HibernateDataSerializerHook.LOCK; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/distributed/UnlockEntryProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.distributed; 17 | 18 | import com.hazelcast.hibernate.serialization.Expirable; 19 | import com.hazelcast.hibernate.serialization.ExpiryMarker; 20 | import com.hazelcast.hibernate.serialization.HibernateDataSerializerHook; 21 | import com.hazelcast.nio.ObjectDataInput; 22 | import com.hazelcast.nio.ObjectDataOutput; 23 | 24 | import java.io.IOException; 25 | import java.util.Map; 26 | 27 | /** 28 | * A concrete implementation of {@link com.hazelcast.map.EntryProcessor} which unlocks 29 | * a soft-locked region cached entry 30 | */ 31 | public class UnlockEntryProcessor extends AbstractRegionCacheEntryProcessor { 32 | 33 | private ExpiryMarker lock; 34 | private String nextMarkerId; 35 | private long timestamp; 36 | 37 | public UnlockEntryProcessor() { 38 | } 39 | 40 | public UnlockEntryProcessor(ExpiryMarker lock, String nextMarkerId, long timestamp) { 41 | this.lock = lock; 42 | this.nextMarkerId = nextMarkerId; 43 | this.timestamp = timestamp; 44 | } 45 | 46 | @Override 47 | public Void process(Map.Entry entry) { 48 | Expirable expirable = entry.getValue(); 49 | 50 | if (expirable != null) { 51 | if (expirable.matches(lock)) { 52 | expirable = ((ExpiryMarker) expirable).expire(timestamp); 53 | } else if (expirable.getValue() != null) { 54 | // It's a value. Expire the value immediately. This prevents 55 | // in-flight transactions from adding stale values to the cache 56 | expirable = new ExpiryMarker(null, timestamp, nextMarkerId).expire(timestamp); 57 | } else { 58 | // else its a different marker. Leave the it as is 59 | return null; 60 | } 61 | entry.setValue(expirable); 62 | } 63 | 64 | return null; 65 | } 66 | 67 | @Override 68 | public void writeData(ObjectDataOutput out) throws IOException { 69 | out.writeObject(lock); 70 | out.writeUTF(nextMarkerId); 71 | out.writeLong(timestamp); 72 | } 73 | 74 | @Override 75 | public void readData(ObjectDataInput in) throws IOException { 76 | lock = in.readObject(); 77 | nextMarkerId = in.readUTF(); 78 | timestamp = in.readLong(); 79 | } 80 | 81 | @Override 82 | public int getId() { 83 | return HibernateDataSerializerHook.UNLOCK; 84 | } 85 | 86 | } 87 | 88 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/distributed/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | /** 17 | * Provides distributed class for Hibernate. 18 | * such as IMapRegionCache. 19 | */ 20 | package com.hazelcast.hibernate.distributed; 21 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/instance/HazelcastInstanceFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.instance; 17 | 18 | import com.hazelcast.hibernate.CacheEnvironment; 19 | import org.hibernate.cache.CacheException; 20 | 21 | import java.util.Properties; 22 | 23 | /** 24 | * A factory class to build up implementations of {@link com.hazelcast.hibernate.instance.IHazelcastInstanceLoader} 25 | * that returns a {@link com.hazelcast.core.HazelcastInstance} depending on configuration either backed by Hazelcast 26 | * client or node implementation. 27 | */ 28 | public final class HazelcastInstanceFactory { 29 | 30 | private static final String HZ_CLIENT_LOADER_CLASSNAME = "com.hazelcast.hibernate.instance.HazelcastClientLoader"; 31 | private static final String HZ_INSTANCE_LOADER_CLASSNAME = "com.hazelcast.hibernate.instance.HazelcastInstanceLoader"; 32 | 33 | private HazelcastInstanceFactory() { 34 | } 35 | 36 | public static IHazelcastInstanceLoader createInstanceLoader(Properties props) throws CacheException { 37 | try { 38 | IHazelcastInstanceLoader instanceLoader = (IHazelcastInstanceLoader) props. 39 | get("com.hazelcast.hibernate.instance.loader"); 40 | if (instanceLoader != null) { 41 | return instanceLoader; 42 | } 43 | Class loaderClass = getInstanceLoaderClass(props); 44 | instanceLoader = (IHazelcastInstanceLoader) loaderClass.newInstance(); 45 | instanceLoader.configure(props); 46 | return instanceLoader; 47 | } catch (Exception e) { 48 | throw new CacheException(e); 49 | } 50 | } 51 | 52 | private static Class getInstanceLoaderClass(Properties props) throws ClassNotFoundException { 53 | ClassLoader cl = HazelcastInstanceFactory.class.getClassLoader(); 54 | if (props != null && CacheEnvironment.isNativeClient(props)) { 55 | return cl.loadClass(HZ_CLIENT_LOADER_CLASSNAME); 56 | } else { 57 | return cl.loadClass(HZ_INSTANCE_LOADER_CLASSNAME); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/instance/IHazelcastInstanceLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.instance; 17 | 18 | import com.hazelcast.core.HazelcastInstance; 19 | import org.hibernate.cache.CacheException; 20 | 21 | import java.util.Properties; 22 | 23 | /** 24 | * Factory interface to build Hazelcast instances and configure them depending 25 | * on configuration. 26 | */ 27 | public interface IHazelcastInstanceLoader { 28 | 29 | /** 30 | * Applies a set of properties to the factory 31 | * 32 | * @param props properties to apply 33 | */ 34 | void configure(Properties props); 35 | 36 | /** 37 | * Create a new {@link com.hazelcast.core.HazelcastInstance} or loads an already 38 | * existing instances by it's name. 39 | * 40 | * @return new or existing HazelcastInstance (either client or node mode) 41 | * @throws CacheException all exceptions wrapped to CacheException 42 | */ 43 | HazelcastInstance loadInstance() throws CacheException; 44 | 45 | /** 46 | * Tries to shutdown a HazelcastInstance 47 | * 48 | * @throws CacheException all exceptions wrapped to CacheException 49 | */ 50 | void unloadInstance() throws CacheException; 51 | } 52 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/instance/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | /** 17 | * Provides instance interfaces/classes for Hibernate. 18 | */ 19 | package com.hazelcast.hibernate.instance; 20 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/local/CleanupService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.local; 17 | 18 | 19 | import com.hazelcast.instance.OutOfMemoryErrorDispatcher; 20 | 21 | import java.util.concurrent.Executors; 22 | import java.util.concurrent.ScheduledExecutorService; 23 | import java.util.concurrent.ThreadFactory; 24 | import java.util.concurrent.TimeUnit; 25 | 26 | /** 27 | * An internal service to clean cache regions 28 | */ 29 | public final class CleanupService { 30 | 31 | private static final long FIXED_DELAY = 60; 32 | private static final long FIXED_DELAY1 = 60; 33 | 34 | private final String name; 35 | private final ScheduledExecutorService executor; 36 | 37 | public CleanupService(final String name) { 38 | this.name = name; 39 | executor = Executors.newSingleThreadScheduledExecutor(new CleanupThreadFactory()); 40 | } 41 | 42 | public void registerCache(final LocalRegionCache cache) { 43 | executor.scheduleWithFixedDelay(new Runnable() { 44 | public void run() { 45 | cache.cleanup(); 46 | } 47 | }, FIXED_DELAY, FIXED_DELAY1, TimeUnit.SECONDS); 48 | } 49 | 50 | public void stop() { 51 | executor.shutdownNow(); 52 | } 53 | 54 | /** 55 | * Internal ThreadFactory to create cleanup threads 56 | */ 57 | private class CleanupThreadFactory implements ThreadFactory { 58 | 59 | @Override 60 | public Thread newThread(final Runnable r) { 61 | final Thread thread = new CleanupThread(r, name + ".hibernate.cleanup"); 62 | thread.setDaemon(true); 63 | return thread; 64 | } 65 | } 66 | 67 | /** 68 | * Runnable thread adapter to capture exceptions and notify Hazelcast about them 69 | */ 70 | private static final class CleanupThread extends Thread { 71 | 72 | private CleanupThread(final Runnable target, final String name) { 73 | super(target, name); 74 | } 75 | 76 | public void run() { 77 | try { 78 | super.run(); 79 | } catch (OutOfMemoryError e) { 80 | OutOfMemoryErrorDispatcher.onOutOfMemory(e); 81 | } 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/local/Invalidation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.local; 17 | 18 | import com.hazelcast.hibernate.serialization.HibernateDataSerializerHook; 19 | import com.hazelcast.nio.ObjectDataInput; 20 | import com.hazelcast.nio.ObjectDataOutput; 21 | import com.hazelcast.nio.serialization.IdentifiedDataSerializable; 22 | 23 | import java.io.IOException; 24 | 25 | 26 | /** 27 | * An invalidation messages 28 | */ 29 | public class Invalidation implements IdentifiedDataSerializable { 30 | 31 | private Object key; 32 | private Object version; 33 | 34 | public Invalidation() { 35 | } 36 | 37 | public Invalidation(final Object key, final Object version) { 38 | this.key = key; 39 | this.version = version; 40 | } 41 | 42 | public Object getKey() { 43 | return key; 44 | } 45 | 46 | public Object getVersion() { 47 | return version; 48 | } 49 | 50 | @Override 51 | public void writeData(final ObjectDataOutput out) throws IOException { 52 | out.writeObject(key); 53 | out.writeObject(version); 54 | } 55 | 56 | @Override 57 | public void readData(final ObjectDataInput in) throws IOException { 58 | key = in.readObject(); 59 | version = in.readObject(); 60 | } 61 | 62 | @Override 63 | public int getFactoryId() { 64 | return HibernateDataSerializerHook.F_ID; 65 | } 66 | 67 | @Override 68 | public int getId() { 69 | return HibernateDataSerializerHook.INVALIDATION; 70 | } 71 | 72 | @Override 73 | public String toString() { 74 | return "Invalidation{key=" + key + ", version=" + version + '}'; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/local/Timestamp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.local; 17 | 18 | import com.hazelcast.hibernate.serialization.HibernateDataSerializerHook; 19 | import com.hazelcast.nio.ObjectDataInput; 20 | import com.hazelcast.nio.ObjectDataOutput; 21 | import com.hazelcast.nio.serialization.IdentifiedDataSerializable; 22 | 23 | import java.io.IOException; 24 | 25 | /** 26 | * Hazelcast compatible implementation of a timestamp for internal eviction 27 | */ 28 | public class Timestamp implements IdentifiedDataSerializable { 29 | 30 | private Object key; 31 | private long timestamp; 32 | 33 | public Timestamp() { 34 | } 35 | 36 | public Timestamp(final Object key, final long timestamp) { 37 | this.key = key; 38 | this.timestamp = timestamp; 39 | } 40 | 41 | public Object getKey() { 42 | return key; 43 | } 44 | 45 | public long getTimestamp() { 46 | return timestamp; 47 | } 48 | 49 | @Override 50 | public void writeData(final ObjectDataOutput out) throws IOException { 51 | out.writeObject(key); 52 | out.writeLong(timestamp); 53 | } 54 | 55 | @Override 56 | public void readData(final ObjectDataInput in) throws IOException { 57 | key = in.readObject(); 58 | timestamp = in.readLong(); 59 | } 60 | 61 | @Override 62 | public int getFactoryId() { 63 | return HibernateDataSerializerHook.F_ID; 64 | } 65 | 66 | @Override 67 | public int getId() { 68 | return HibernateDataSerializerHook.TIMESTAMP; 69 | } 70 | 71 | @Override 72 | public String toString() { 73 | return "Timestamp" + "{key=" + key + ", timestamp=" + timestamp + '}'; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/local/TimestampsRegionCache.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.local; 17 | 18 | import com.hazelcast.core.HazelcastInstance; 19 | import com.hazelcast.hibernate.RegionCache; 20 | import com.hazelcast.hibernate.serialization.Expirable; 21 | import com.hazelcast.hibernate.serialization.Value; 22 | 23 | /** 24 | * A timestamp based local RegionCache 25 | */ 26 | public class TimestampsRegionCache extends LocalRegionCache implements RegionCache { 27 | 28 | public TimestampsRegionCache(final String name, final HazelcastInstance hazelcastInstance) { 29 | super(name, hazelcastInstance, null); 30 | } 31 | 32 | @Override 33 | public boolean put(Object key, Object value, long txTimestamp, Object version) { 34 | // use the value in txTimestamp as the timestamp instead of the value, since 35 | // hibernate pre-invalidates with a large value, and then invalidates with 36 | //the actual time, which can cause queries to not be cached. 37 | boolean succeed = super.put(key, value, txTimestamp, version); 38 | if (succeed) { 39 | maybeNotifyTopic(key, value, version); 40 | } 41 | return succeed; 42 | } 43 | 44 | @Override 45 | protected void maybeInvalidate(final Object messageObject) { 46 | final Timestamp ts = (Timestamp) messageObject; 47 | final Object key = ts.getKey(); 48 | 49 | if (key == null) { 50 | // Invalidate the entire region cache. 51 | cache.clear(); 52 | return; 53 | } 54 | 55 | for (;;) { 56 | final Expirable value = cache.get(key); 57 | final Long current = value != null ? (Long) value.getValue() : null; 58 | if (current != null) { 59 | if (ts.getTimestamp() > current) { 60 | if (cache.replace(key, value, new Value(value.getVersion(), nextTimestamp(), ts.getTimestamp()))) { 61 | return; 62 | } 63 | } else { 64 | return; 65 | } 66 | } else { 67 | if (cache.putIfAbsent(key, new Value(null, nextTimestamp(), ts.getTimestamp())) == null) { 68 | return; 69 | } 70 | } 71 | } 72 | } 73 | 74 | @Override 75 | protected Object createMessage(final Object key, final Object value, final Object currentVersion) { 76 | return new Timestamp(key, (Long) value); 77 | } 78 | 79 | @Override 80 | public void clear() { 81 | cache.clear(); 82 | maybeNotifyTopic(null, -1L, null); 83 | } 84 | 85 | final void cleanup() { 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/local/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | /** 17 | * Provides local classes for Hibernate. 18 | * such as TimeStamp,CleanupService. 19 | */ 20 | package com.hazelcast.hibernate.local; 21 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | /** 17 | * Contains interfaces/classes related to Hibernate. 18 | */ 19 | package com.hazelcast.hibernate; 20 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/region/AbstractGeneralRegion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.region; 17 | 18 | import com.hazelcast.core.HazelcastInstance; 19 | import com.hazelcast.core.OperationTimeoutException; 20 | import com.hazelcast.hibernate.RegionCache; 21 | import com.hazelcast.logging.Logger; 22 | import org.hibernate.cache.CacheException; 23 | import org.hibernate.cache.GeneralDataRegion; 24 | 25 | import java.util.Properties; 26 | 27 | /** 28 | * Basic implementation of a IMap based cache without any special security checks 29 | * 30 | * @author Leo Kim (lkim@limewire.com) 31 | * @param implementation type of RegionCache 32 | */ 33 | abstract class AbstractGeneralRegion extends AbstractHazelcastRegion 34 | implements GeneralDataRegion { 35 | 36 | private final Cache cache; 37 | 38 | protected AbstractGeneralRegion(final HazelcastInstance instance, final String name 39 | , final Properties props, final Cache cache) { 40 | super(instance, name, props); 41 | this.cache = cache; 42 | } 43 | 44 | @Override 45 | public void evict(final Object key) throws CacheException { 46 | try { 47 | getCache().remove(key); 48 | } catch (OperationTimeoutException e) { 49 | Logger.getLogger(AbstractGeneralRegion.class).finest(e); 50 | } 51 | } 52 | 53 | @Override 54 | public void evictAll() throws CacheException { 55 | try { 56 | getCache().clear(); 57 | } catch (OperationTimeoutException e) { 58 | Logger.getLogger(AbstractGeneralRegion.class).finest(e); 59 | } 60 | } 61 | 62 | @Override 63 | public Object get(final Object key) throws CacheException { 64 | try { 65 | return getCache().get(key, nextTimestamp()); 66 | } catch (OperationTimeoutException e) { 67 | return null; 68 | } 69 | } 70 | 71 | @Override 72 | public void put(final Object key, final Object value) throws CacheException { 73 | try { 74 | getCache().put(key, value, nextTimestamp(), null); 75 | } catch (OperationTimeoutException e) { 76 | Logger.getLogger(AbstractGeneralRegion.class).finest(e); 77 | } 78 | } 79 | 80 | public Cache getCache() { 81 | return cache; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/region/AbstractTransactionalDataRegion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.region; 17 | 18 | import com.hazelcast.core.HazelcastInstance; 19 | import com.hazelcast.hibernate.RegionCache; 20 | import org.hibernate.cache.CacheDataDescription; 21 | import org.hibernate.cache.TransactionalDataRegion; 22 | 23 | import java.util.Properties; 24 | 25 | /** 26 | * Abstract base class of all regions 27 | * 28 | * @author Leo Kim (lkim@limewire.com) 29 | * @param implementation type of RegionCache 30 | */ 31 | public abstract class AbstractTransactionalDataRegion extends AbstractHazelcastRegion 32 | implements 33 | TransactionalDataRegion { 34 | 35 | private final CacheDataDescription metadata; 36 | private final Cache cache; 37 | 38 | protected AbstractTransactionalDataRegion(final HazelcastInstance instance, final String regionName, 39 | final Properties props, final CacheDataDescription metadata, final Cache cache) { 40 | super(instance, regionName, props); 41 | this.metadata = metadata; 42 | this.cache = cache; 43 | } 44 | 45 | /** 46 | * @see org.hibernate.cache.TransactionalDataRegion#getCacheDataDescription() 47 | */ 48 | @Override 49 | public CacheDataDescription getCacheDataDescription() { 50 | return metadata; 51 | } 52 | 53 | /** 54 | * @see org.hibernate.cache.TransactionalDataRegion#isTransactionAware() 55 | */ 56 | @Override 57 | public boolean isTransactionAware() { 58 | return false; 59 | } 60 | 61 | @Override 62 | public Cache getCache() { 63 | return cache; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/region/HazelcastCollectionRegion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.region; 17 | 18 | import com.hazelcast.core.HazelcastInstance; 19 | import com.hazelcast.hibernate.RegionCache; 20 | import com.hazelcast.hibernate.access.NonStrictReadWriteAccessDelegate; 21 | import com.hazelcast.hibernate.access.ReadOnlyAccessDelegate; 22 | import com.hazelcast.hibernate.access.ReadWriteAccessDelegate; 23 | import org.hibernate.cache.CacheDataDescription; 24 | import org.hibernate.cache.CacheException; 25 | import org.hibernate.cache.CollectionRegion; 26 | import org.hibernate.cache.access.AccessType; 27 | import org.hibernate.cache.access.CollectionRegionAccessStrategy; 28 | 29 | import java.util.Properties; 30 | 31 | /** 32 | * An collection region implementation based upon Hazelcast IMap with basic concurrency / transactional support 33 | * by supplying CollectionRegionAccessStrategy 34 | * 35 | * @author mdogan 11/9/12 36 | * @param implementation type of RegionCache 37 | */ 38 | public final class HazelcastCollectionRegion extends AbstractTransactionalDataRegion 39 | implements CollectionRegion { 40 | 41 | public HazelcastCollectionRegion(final HazelcastInstance instance, 42 | final String regionName, final Properties props, 43 | final CacheDataDescription metadata, final Cache cache) { 44 | super(instance, regionName, props, metadata, cache); 45 | } 46 | 47 | @Override 48 | public CollectionRegionAccessStrategy buildAccessStrategy(final AccessType accessType) throws CacheException { 49 | if (AccessType.READ_ONLY.equals(accessType)) { 50 | return new CollectionRegionAccessStrategyAdapter( 51 | new ReadOnlyAccessDelegate(this, props)); 52 | } 53 | if (AccessType.NONSTRICT_READ_WRITE.equals(accessType)) { 54 | return new CollectionRegionAccessStrategyAdapter( 55 | new NonStrictReadWriteAccessDelegate(this, props)); 56 | } 57 | if (AccessType.READ_WRITE.equals(accessType)) { 58 | return new CollectionRegionAccessStrategyAdapter( 59 | new ReadWriteAccessDelegate(this, props)); 60 | } 61 | throw new CacheException("AccessType \"" + accessType + "\" is not currently supported by Hazelcast."); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/region/HazelcastEntityRegion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.region; 17 | 18 | import com.hazelcast.core.HazelcastInstance; 19 | import com.hazelcast.hibernate.RegionCache; 20 | import com.hazelcast.hibernate.access.NonStrictReadWriteAccessDelegate; 21 | import com.hazelcast.hibernate.access.ReadOnlyAccessDelegate; 22 | import com.hazelcast.hibernate.access.ReadWriteAccessDelegate; 23 | import org.hibernate.cache.CacheDataDescription; 24 | import org.hibernate.cache.CacheException; 25 | import org.hibernate.cache.EntityRegion; 26 | import org.hibernate.cache.access.AccessType; 27 | import org.hibernate.cache.access.EntityRegionAccessStrategy; 28 | 29 | import java.util.Properties; 30 | 31 | /** 32 | * An entity region implementation based upon Hazelcast IMap with basic concurrency / transactional support 33 | * by supplying EntityRegionAccessStrategy 34 | * 35 | * @author mdogan 11/9/12 36 | * @param implementation type of RegionCache 37 | */ 38 | public final class HazelcastEntityRegion 39 | extends AbstractTransactionalDataRegion implements EntityRegion { 40 | 41 | public HazelcastEntityRegion(final HazelcastInstance instance, 42 | final String regionName, final Properties props, 43 | final CacheDataDescription metadata, final Cache cache) { 44 | super(instance, regionName, props, metadata, cache); 45 | } 46 | 47 | @Override 48 | public EntityRegionAccessStrategy buildAccessStrategy(final AccessType accessType) throws CacheException { 49 | if (AccessType.READ_ONLY.equals(accessType)) { 50 | return new EntityRegionAccessStrategyAdapter( 51 | new ReadOnlyAccessDelegate(this, props)); 52 | } 53 | if (AccessType.NONSTRICT_READ_WRITE.equals(accessType)) { 54 | return new EntityRegionAccessStrategyAdapter( 55 | new NonStrictReadWriteAccessDelegate(this, props)); 56 | } 57 | if (AccessType.READ_WRITE.equals(accessType)) { 58 | return new EntityRegionAccessStrategyAdapter( 59 | new ReadWriteAccessDelegate(this, props)); 60 | } 61 | throw new CacheException("AccessType \"" + accessType + "\" is not currently supported by Hazelcast."); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/region/HazelcastQueryResultsRegion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.region; 17 | 18 | import com.hazelcast.core.HazelcastInstance; 19 | import com.hazelcast.hibernate.local.LocalRegionCache; 20 | import org.hibernate.cache.QueryResultsRegion; 21 | 22 | import java.util.Properties; 23 | 24 | /** 25 | * Hazelcast based implementation of a storage region for query results 26 | */ 27 | public class HazelcastQueryResultsRegion extends AbstractGeneralRegion implements QueryResultsRegion { 28 | 29 | public HazelcastQueryResultsRegion(final HazelcastInstance instance, final String name, final Properties props) { 30 | // Note: The HazelcastInstance _must_ be passed down here. Otherwise query caches 31 | // cannot be configured and will always use defaults. However, even though we're 32 | // passing the HazelcastInstance, we don't want to use an ITopic for invalidation 33 | // because the timestamps cache can take care of outdated queries 34 | super(instance, name, props, new LocalRegionCache(name, instance, null, false)); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/region/HazelcastRegion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.region; 17 | 18 | import com.hazelcast.core.HazelcastInstance; 19 | import com.hazelcast.hibernate.RegionCache; 20 | import com.hazelcast.logging.ILogger; 21 | import org.hibernate.cache.Region; 22 | 23 | /** 24 | * Hazelcast specific interface version of Hibernate's Region 25 | * 26 | * @author Leo Kim (lkim@limewire.com) 27 | * @param implementation type of RegionCache 28 | */ 29 | public interface HazelcastRegion extends Region { 30 | 31 | HazelcastInstance getInstance(); 32 | 33 | Cache getCache(); 34 | 35 | ILogger getLogger(); 36 | } 37 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/region/HazelcastTimestampsRegion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.region; 17 | 18 | import com.hazelcast.core.HazelcastInstance; 19 | import com.hazelcast.hibernate.RegionCache; 20 | import org.hibernate.cache.TimestampsRegion; 21 | 22 | import java.util.Properties; 23 | 24 | /** 25 | * Hazelcast based timestamp using region implementation 26 | * 27 | * @param implementation type of RegionCache 28 | */ 29 | public class HazelcastTimestampsRegion 30 | extends AbstractGeneralRegion implements TimestampsRegion { 31 | 32 | public HazelcastTimestampsRegion(final HazelcastInstance instance, final String name, 33 | final Properties props, final Cache cache) { 34 | super(instance, name, props, cache); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/region/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | /** 17 | * Provides region interfaces/classes for Hibernate. 18 | */ 19 | package com.hazelcast.hibernate.region; 20 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/serialization/Hibernate3CacheEntrySerializerHook.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.serialization; 17 | 18 | import com.hazelcast.logging.Logger; 19 | import com.hazelcast.nio.UnsafeHelper; 20 | import com.hazelcast.nio.serialization.Serializer; 21 | import com.hazelcast.nio.serialization.SerializerHook; 22 | 23 | /** 24 | * This class is used to register a special serializer to not loose 25 | * power over serialization in Hibernate 3 26 | */ 27 | public class Hibernate3CacheEntrySerializerHook 28 | implements SerializerHook { 29 | 30 | private static final String SKIP_INIT_MSG = "Hibernate3 not available, skipping serializer initialization"; 31 | 32 | private final Class cacheEntryClass; 33 | 34 | public Hibernate3CacheEntrySerializerHook() { 35 | Class cacheEntryClass = null; 36 | if (UnsafeHelper.UNSAFE_AVAILABLE) { 37 | try { 38 | cacheEntryClass = Class.forName("org.hibernate.cache.entry.CacheEntry"); 39 | } catch (Exception e) { 40 | Logger.getLogger(Hibernate3CacheEntrySerializerHook.class).finest(SKIP_INIT_MSG); 41 | } 42 | } 43 | this.cacheEntryClass = cacheEntryClass; 44 | } 45 | 46 | @Override 47 | public Class getSerializationType() { 48 | return cacheEntryClass; 49 | } 50 | 51 | @Override 52 | public Serializer createSerializer() { 53 | if (cacheEntryClass != null) { 54 | return new Hibernate3CacheEntrySerializer(); 55 | } 56 | return null; 57 | } 58 | 59 | @Override 60 | public boolean isOverwritable() { 61 | return true; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/serialization/Hibernate3CacheKeySerializerHook.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.serialization; 17 | 18 | import com.hazelcast.logging.Logger; 19 | import com.hazelcast.nio.UnsafeHelper; 20 | import com.hazelcast.nio.serialization.Serializer; 21 | import com.hazelcast.nio.serialization.SerializerHook; 22 | 23 | /** 24 | * This class is used to register a special serializer to not loose 25 | * power over serialization in Hibernate 3 26 | */ 27 | public class Hibernate3CacheKeySerializerHook 28 | implements SerializerHook { 29 | 30 | private static final String SKIP_INIT_MSG = "Hibernate3 not available, skipping serializer initialization"; 31 | 32 | private final Class cacheKeyClass; 33 | 34 | public Hibernate3CacheKeySerializerHook() { 35 | Class cacheKeyClass = null; 36 | if (UnsafeHelper.UNSAFE_AVAILABLE) { 37 | try { 38 | cacheKeyClass = Class.forName("org.hibernate.cache.CacheKey"); 39 | } catch (Exception e) { 40 | Logger.getLogger(Hibernate3CacheKeySerializerHook.class).finest(SKIP_INIT_MSG); 41 | } 42 | } 43 | this.cacheKeyClass = cacheKeyClass; 44 | } 45 | 46 | @Override 47 | public Class getSerializationType() { 48 | return cacheKeyClass; 49 | } 50 | 51 | @Override 52 | public Serializer createSerializer() { 53 | if (cacheKeyClass != null) { 54 | return new Hibernate3CacheKeySerializer(); 55 | } 56 | return null; 57 | } 58 | 59 | @Override 60 | public boolean isOverwritable() { 61 | return true; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/serialization/MarkerWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.serialization; 17 | 18 | 19 | import org.hibernate.cache.access.SoftLock; 20 | 21 | /** 22 | * A wrapper class for ExpiryMarker for returning copy of marker object with 23 | * {@link org.hibernate.cache.access.SoftLock} marker interface. 24 | */ 25 | 26 | public class MarkerWrapper implements SoftLock { 27 | 28 | private final ExpiryMarker marker; 29 | 30 | public MarkerWrapper(ExpiryMarker marker) { 31 | this.marker = marker; 32 | } 33 | 34 | public ExpiryMarker getMarker() { 35 | return marker; 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/serialization/Value.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.serialization; 17 | 18 | import com.hazelcast.nio.ObjectDataInput; 19 | import com.hazelcast.nio.ObjectDataOutput; 20 | 21 | import java.io.IOException; 22 | import java.util.Comparator; 23 | 24 | /** 25 | * A value within a region cache 26 | */ 27 | public class Value extends Expirable { 28 | 29 | private long timestamp; 30 | private Object value; 31 | 32 | public Value() { 33 | } 34 | 35 | public Value(Object version, long timestamp, Object value) { 36 | super(version); 37 | this.timestamp = timestamp; 38 | this.value = value; 39 | } 40 | 41 | @Override 42 | @SuppressWarnings("unchecked") 43 | public boolean isReplaceableBy(long txTimestamp, Object newVersion, Comparator versionComparator) { 44 | return version == null 45 | ? timestamp <= txTimestamp 46 | : versionComparator.compare(version, newVersion) < 0; 47 | } 48 | 49 | public long getTimestamp() { 50 | return timestamp; 51 | } 52 | 53 | @Override 54 | public Object getValue() { 55 | return value; 56 | } 57 | 58 | @Override 59 | public Object getValue(long txTimestamp) { 60 | return timestamp <= txTimestamp ? value : null; 61 | } 62 | 63 | @Override 64 | public boolean matches(ExpiryMarker lock) { 65 | return false; 66 | } 67 | 68 | @Override 69 | public ExpiryMarker markForExpiration(long timeout, String nextMarkerId) { 70 | return new ExpiryMarker(version, timeout, nextMarkerId); 71 | } 72 | 73 | @Override 74 | public void readData(ObjectDataInput in) throws IOException { 75 | super.readData(in); 76 | timestamp = in.readLong(); 77 | value = in.readObject(); 78 | } 79 | 80 | @Override 81 | public void writeData(ObjectDataOutput out) throws IOException { 82 | super.writeData(out); 83 | out.writeLong(timestamp); 84 | out.writeObject(value); 85 | } 86 | 87 | @Override 88 | public int getFactoryId() { 89 | return HibernateDataSerializerHook.F_ID; 90 | } 91 | 92 | @Override 93 | public int getId() { 94 | return HibernateDataSerializerHook.VALUE; 95 | } 96 | 97 | } 98 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/java/com/hazelcast/hibernate/serialization/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | /** 17 | * This class contains the Hibernate3 serializer hooks so what we don't 18 | * loose handling on serialization while working on hibernate 19 | */ 20 | package com.hazelcast.hibernate.serialization; 21 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/resources/META-INF/services/com.hazelcast.DataSerializerHook: -------------------------------------------------------------------------------- 1 | com.hazelcast.hibernate.serialization.HibernateDataSerializerHook -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/main/resources/META-INF/services/com.hazelcast.SerializerHook: -------------------------------------------------------------------------------- 1 | com.hazelcast.hibernate.serialization.Hibernate3CacheKeySerializerHook 2 | com.hazelcast.hibernate.serialization.Hibernate3CacheEntrySerializerHook -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/test/java/com/hazelcast/hibernate/LocalRegionFactorySlowTest.java: -------------------------------------------------------------------------------- 1 | package com.hazelcast.hibernate; 2 | 3 | import com.hazelcast.hibernate.entity.DummyEntity; 4 | import com.hazelcast.test.HazelcastSerialClassRunner; 5 | import com.hazelcast.test.annotation.SlowTest; 6 | import org.hibernate.Session; 7 | import org.hibernate.Transaction; 8 | import org.hibernate.cfg.Environment; 9 | import org.junit.Test; 10 | import org.junit.experimental.categories.Category; 11 | import org.junit.runner.RunWith; 12 | 13 | import java.util.List; 14 | import java.util.Properties; 15 | 16 | import static org.junit.Assert.assertEquals; 17 | import static org.junit.Assert.assertNotNull; 18 | 19 | @RunWith(HazelcastSerialClassRunner.class) 20 | @Category(SlowTest.class) 21 | public class LocalRegionFactorySlowTest extends HibernateSlowTestSupport { 22 | 23 | @Test 24 | public void test_query_with_non_mock_network() { 25 | final int entityCount = 10; 26 | final int queryCount = 2; 27 | insertDummyEntities(entityCount); 28 | List list = null; 29 | for (int i = 0; i < queryCount; i++) { 30 | list = executeQuery(sf); 31 | assertEquals(entityCount, list.size()); 32 | } 33 | for (int i = 0; i < queryCount; i++) { 34 | list = executeQuery(sf2); 35 | assertEquals(entityCount, list.size()); 36 | } 37 | 38 | assertNotNull(list); 39 | DummyEntity toDelete = list.get(0); 40 | Session session = sf2.openSession(); 41 | Transaction tx = session.beginTransaction(); 42 | try { 43 | session.delete(toDelete); 44 | tx.commit(); 45 | } catch (Exception e) { 46 | tx.rollback(); 47 | e.printStackTrace(); 48 | } finally { 49 | session.close(); 50 | } 51 | assertEquals(entityCount - 1, executeQuery(sf).size()); 52 | assertEquals(entityCount - 1, executeQuery(sf2).size()); 53 | 54 | } 55 | 56 | @Override 57 | protected Properties getCacheProperties() { 58 | Properties props = new Properties(); 59 | props.setProperty(Environment.CACHE_REGION_FACTORY, HazelcastLocalCacheRegionFactory.class.getName()); 60 | return props; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/test/java/com/hazelcast/hibernate/NativeClientTest.java: -------------------------------------------------------------------------------- 1 | package com.hazelcast.hibernate; 2 | 3 | import com.hazelcast.hibernate.entity.DummyEntity; 4 | import com.hazelcast.hibernate.entity.DummyProperty; 5 | import com.hazelcast.test.HazelcastSerialClassRunner; 6 | import com.hazelcast.test.annotation.QuickTest; 7 | import com.hazelcast.test.annotation.SlowTest; 8 | import org.hibernate.SessionFactory; 9 | import org.hibernate.Transaction; 10 | import org.hibernate.cfg.Configuration; 11 | import org.hibernate.cfg.Environment; 12 | import org.hibernate.Session; 13 | import org.junit.After; 14 | import org.junit.Before; 15 | import org.junit.Test; 16 | import org.junit.experimental.categories.Category; 17 | import org.junit.runner.RunWith; 18 | 19 | import java.net.URL; 20 | import java.util.Date; 21 | import java.util.Properties; 22 | 23 | import static org.junit.Assert.assertEquals; 24 | 25 | @RunWith(HazelcastSerialClassRunner.class) 26 | @Category(QuickTest.class) 27 | public class NativeClientTest 28 | extends HibernateSlowTestSupport { 29 | 30 | protected SessionFactory clientSf; 31 | 32 | @Override 33 | protected Properties getCacheProperties() { 34 | Properties props = new Properties(); 35 | props.setProperty(Environment.CACHE_REGION_FACTORY, HazelcastCacheRegionFactory.class.getName()); 36 | return props; 37 | } 38 | 39 | @Before 40 | @Override 41 | public void postConstruct() { 42 | sf = createSessionFactory(getCacheProperties(), null); 43 | clientSf = createClientSessionFactory(getCacheProperties()); 44 | } 45 | 46 | @Test 47 | public void testInsertLoad() { 48 | Session session = clientSf.openSession(); 49 | Transaction tx = session.beginTransaction(); 50 | DummyEntity e = new DummyEntity((long) 1, "dummy:1", 123456d, new Date()); 51 | session.save(e); 52 | tx.commit(); 53 | session.close(); 54 | 55 | session = clientSf.openSession(); 56 | DummyEntity retrieved = (DummyEntity) session.get(DummyEntity.class, (long) 1); 57 | assertEquals("dummy:1", retrieved.getName()); 58 | 59 | session.close(); 60 | session = clientSf.openSession(); 61 | retrieved.setName("dummy:update"); 62 | tx = session.beginTransaction(); 63 | session.update(retrieved); 64 | tx.commit(); 65 | session.close(); 66 | } 67 | 68 | @After 69 | public void tearDown() { 70 | if (clientSf != null) { 71 | clientSf.close(); 72 | } 73 | } 74 | 75 | protected SessionFactory createClientSessionFactory(Properties props) { 76 | Configuration conf = new Configuration(); 77 | URL xml = HibernateTestSupport.class.getClassLoader().getResource("test-hibernate-client.cfg.xml"); 78 | conf.configure(xml); 79 | conf.setCacheConcurrencyStrategy(DummyEntity.class.getName(), getCacheStrategy()); 80 | conf.setCacheConcurrencyStrategy(DummyProperty.class.getName(), getCacheStrategy()); 81 | conf.setCollectionCacheConcurrencyStrategy(DummyEntity.class.getName() + ".properties", getCacheStrategy()); 82 | conf.addProperties(props); 83 | final SessionFactory sf = conf.buildSessionFactory(); 84 | sf.getStatistics().setStatisticsEnabled(true); 85 | return sf; 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/test/java/com/hazelcast/hibernate/RegionFactoryQueryTest.java: -------------------------------------------------------------------------------- 1 | package com.hazelcast.hibernate; 2 | 3 | import com.hazelcast.hibernate.entity.DummyEntity; 4 | import com.hazelcast.test.HazelcastSerialClassRunner; 5 | import com.hazelcast.test.annotation.SlowTest; 6 | import org.hibernate.Session; 7 | import org.hibernate.Transaction; 8 | import org.hibernate.cfg.Environment; 9 | import org.junit.Before; 10 | import org.junit.Test; 11 | import org.junit.experimental.categories.Category; 12 | import org.junit.runner.RunWith; 13 | 14 | import java.util.List; 15 | import java.util.Properties; 16 | 17 | import static org.junit.Assert.assertEquals; 18 | import static org.junit.Assert.assertNotNull; 19 | 20 | @RunWith(HazelcastSerialClassRunner.class) 21 | @Category(SlowTest.class) 22 | public class RegionFactoryQueryTest extends HibernateSlowTestSupport{ 23 | 24 | @Before 25 | @Override 26 | public void postConstruct() { 27 | sf = createSessionFactory(getCacheProperties(), null); 28 | sf2 = createSessionFactory(getCacheProperties(), null); 29 | } 30 | 31 | @Test 32 | public void test_query_with_non_mock_network() { 33 | final int entityCount = 10; 34 | final int queryCount = 2; 35 | insertDummyEntities(entityCount); 36 | List list = null; 37 | for (int i = 0; i < queryCount; i++) { 38 | list = executeQuery(sf); 39 | assertEquals(entityCount, list.size()); 40 | } 41 | for (int i = 0; i < queryCount; i++) { 42 | list = executeQuery(sf2); 43 | assertEquals(entityCount, list.size()); 44 | } 45 | 46 | assertNotNull(list); 47 | DummyEntity toDelete = list.get(0); 48 | Session session = sf.openSession(); 49 | Transaction tx = session.beginTransaction(); 50 | try { 51 | session.delete(toDelete); 52 | tx.commit(); 53 | } catch (Exception e) { 54 | tx.rollback(); 55 | e.printStackTrace(); 56 | } finally { 57 | session.close(); 58 | } 59 | assertEquals(entityCount - 1, executeQuery(sf).size()); 60 | assertEquals(entityCount - 1, executeQuery(sf2).size()); 61 | 62 | } 63 | 64 | protected Properties getCacheProperties() { 65 | Properties props = new Properties(); 66 | props.setProperty(Environment.CACHE_REGION_FACTORY, HazelcastCacheRegionFactory.class.getName()); 67 | return props; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/test/java/com/hazelcast/hibernate/VersionAwareMapMergePolicyTest.java: -------------------------------------------------------------------------------- 1 | package com.hazelcast.hibernate; 2 | 3 | import com.hazelcast.test.HazelcastSerialClassRunner; 4 | import org.hibernate.engine.SessionImplementor; 5 | import org.hibernate.persister.entity.EntityPersister; 6 | import org.hibernate.cache.entry.CacheEntry; 7 | import com.hazelcast.core.EntryView; 8 | import com.hazelcast.map.merge.MapMergePolicy; 9 | import com.hazelcast.test.annotation.ParallelTest; 10 | import com.hazelcast.test.annotation.QuickTest; 11 | import org.junit.Before; 12 | import org.junit.Test; 13 | import org.junit.experimental.categories.Category; 14 | import org.junit.runner.RunWith; 15 | 16 | import static org.junit.Assert.assertEquals; 17 | import static org.mockito.Mockito.mock; 18 | import static org.mockito.Mockito.when; 19 | 20 | @RunWith(HazelcastSerialClassRunner.class) 21 | @Category({QuickTest.class, ParallelTest.class}) 22 | public class VersionAwareMapMergePolicyTest { 23 | 24 | 25 | private static final MockVersion versionOld = new MockVersion(0); 26 | private static final MockVersion versionNew = new MockVersion(1); 27 | 28 | protected MapMergePolicy policy; 29 | 30 | @Before 31 | public void given() { 32 | policy = new VersionAwareMapMergePolicy(); 33 | } 34 | 35 | @Test 36 | public void merge_mergingUptodate() { 37 | CacheEntry existing = cacheEntryWithVersion(versionOld); 38 | CacheEntry merging = cacheEntryWithVersion(versionNew); 39 | 40 | EntryView entryExisting = entryWithGivenValue(existing); 41 | EntryView entryMerging = entryWithGivenValue(merging); 42 | 43 | assertEquals(merging, policy.merge("map", entryMerging, entryExisting)); 44 | } 45 | 46 | @Test 47 | public void merge_mergingStale() { 48 | CacheEntry existing = cacheEntryWithVersion(versionNew); 49 | CacheEntry merging = cacheEntryWithVersion(versionOld); 50 | 51 | EntryView entryExisting = entryWithGivenValue(existing); 52 | EntryView entryMerging = entryWithGivenValue(merging); 53 | 54 | assertEquals(existing, policy.merge("map", entryMerging, entryExisting)); 55 | } 56 | 57 | @Test 58 | public void merge_mergingNull() { 59 | CacheEntry existing = null; 60 | CacheEntry merging = cacheEntryWithVersion(versionNew); 61 | 62 | EntryView entryExisting = entryWithGivenValue(existing); 63 | EntryView entryMerging = entryWithGivenValue(merging); 64 | 65 | assertEquals(merging, policy.merge("map", entryMerging, entryExisting)); 66 | } 67 | 68 | 69 | private CacheEntry cacheEntryWithVersion(MockVersion mockVersion) { 70 | return new CacheEntry(new Object[]{}, mock(EntityPersister.class), false, mockVersion, mock(SessionImplementor.class), mock(Object.class)); 71 | } 72 | 73 | private EntryView entryWithGivenValue(Object value) { 74 | EntryView entryView = mock(EntryView.class); 75 | try { 76 | when(entryView.getValue()).thenReturn(value); 77 | return entryView; 78 | } catch (Exception e) { 79 | throw new RuntimeException(e); 80 | } 81 | 82 | } 83 | 84 | protected static class MockVersion implements Comparable { 85 | 86 | private int version; 87 | 88 | public MockVersion(int version) { 89 | this.version = version; 90 | } 91 | 92 | @Override 93 | public int compareTo(MockVersion o) { 94 | return version - o.version; 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/test/java/com/hazelcast/hibernate/access/ReadWriteAccessDelegateTest.java: -------------------------------------------------------------------------------- 1 | package com.hazelcast.hibernate.access; 2 | 3 | import com.hazelcast.core.HazelcastException; 4 | import com.hazelcast.hibernate.HibernateTestSupport; 5 | import com.hazelcast.hibernate.RegionCache; 6 | import com.hazelcast.hibernate.region.HazelcastRegion; 7 | import com.hazelcast.logging.ILogger; 8 | import com.hazelcast.logging.Logger; 9 | import com.hazelcast.test.HazelcastParallelClassRunner; 10 | import com.hazelcast.test.annotation.QuickTest; 11 | import org.hibernate.cache.access.SoftLock; 12 | import org.junit.Before; 13 | import org.junit.Test; 14 | import org.junit.experimental.categories.Category; 15 | import org.junit.runner.RunWith; 16 | 17 | import static org.junit.Assert.assertFalse; 18 | import static org.mockito.Matchers.any; 19 | import static org.mockito.Mockito.mock; 20 | import static org.mockito.Mockito.when; 21 | 22 | @RunWith(HazelcastParallelClassRunner.class) 23 | @Category(QuickTest.class) 24 | public class ReadWriteAccessDelegateTest extends HibernateTestSupport { 25 | 26 | private RegionCache cache; 27 | private ReadWriteAccessDelegate delegate; 28 | 29 | @Before 30 | public void setUp() { 31 | ILogger log = Logger.getLogger(ReadWriteAccessDelegateTest.class); 32 | 33 | cache = mock(RegionCache.class); 34 | 35 | HazelcastRegion region = mock(HazelcastRegion.class); 36 | when(region.getLogger()).thenReturn(log); 37 | when(region.getCache()).thenReturn(cache); 38 | 39 | delegate = new ReadWriteAccessDelegate(region, null); 40 | } 41 | 42 | @Test 43 | public void testAfterInsert() { 44 | when(cache.insert(any(), any(), any())).thenThrow(new HazelcastException("expected exception")); 45 | assertFalse(delegate.afterInsert(null, null, null)); 46 | } 47 | 48 | @Test 49 | public void testAfterUpdate() { 50 | when(cache.update(any(), any(), any(), any(SoftLock.class))).thenThrow(new HazelcastException("expected exception")); 51 | assertFalse(delegate.afterUpdate(null, null, null, null, null)); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/test/java/com/hazelcast/hibernate/entity/DummyEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.entity; 17 | 18 | import java.util.Date; 19 | import java.util.Set; 20 | 21 | public class DummyEntity { 22 | 23 | private long id; 24 | 25 | private int version; 26 | 27 | private String name; 28 | 29 | private double value; 30 | 31 | private Date date; 32 | 33 | private Set properties; 34 | 35 | public DummyEntity() { 36 | } 37 | 38 | public DummyEntity(long id, String name, double value, Date date) { 39 | this.id = id; 40 | this.name = name; 41 | this.value = value; 42 | this.date = date; 43 | } 44 | 45 | public long getId() { 46 | return id; 47 | } 48 | 49 | public void setId(long id) { 50 | this.id = id; 51 | } 52 | 53 | public int getVersion() { 54 | return version; 55 | } 56 | 57 | public void setVersion(int version) { 58 | this.version = version; 59 | } 60 | 61 | public String getName() { 62 | return name; 63 | } 64 | 65 | public void setName(String name) { 66 | this.name = name; 67 | } 68 | 69 | public double getValue() { 70 | return value; 71 | } 72 | 73 | public void setValue(double value) { 74 | this.value = value; 75 | } 76 | 77 | public Date getDate() { 78 | return date; 79 | } 80 | 81 | public void setDate(Date date) { 82 | this.date = date; 83 | } 84 | 85 | public void setProperties(Set properties) { 86 | this.properties = properties; 87 | } 88 | 89 | public Set getProperties() { 90 | return properties; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/test/java/com/hazelcast/hibernate/entity/DummyProperty.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.entity; 17 | 18 | public class DummyProperty { 19 | 20 | private long id; 21 | 22 | private int version; 23 | 24 | private String key; 25 | 26 | private DummyEntity entity; 27 | 28 | public DummyProperty() { 29 | } 30 | 31 | public DummyProperty(String key) { 32 | this.key = key; 33 | } 34 | 35 | public DummyProperty(String key, DummyEntity entity) { 36 | this.key = key; 37 | this.entity = entity; 38 | } 39 | 40 | public DummyProperty(long id, String key, DummyEntity entity) { 41 | this.id = id; 42 | this.key = key; 43 | this.entity = entity; 44 | } 45 | 46 | public long getId() { 47 | return id; 48 | } 49 | 50 | public void setId(long id) { 51 | this.id = id; 52 | } 53 | 54 | public int getVersion() { 55 | return version; 56 | } 57 | 58 | public void setVersion(int version) { 59 | this.version = version; 60 | } 61 | 62 | public String getKey() { 63 | return key; 64 | } 65 | 66 | public void setKey(String key) { 67 | this.key = key; 68 | } 69 | 70 | public DummyEntity getEntity() { 71 | return entity; 72 | } 73 | 74 | public void setEntity(DummyEntity entity) { 75 | this.entity = entity; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/test/java/com/hazelcast/hibernate/serialization/HibernateSerializationHookAvailableTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.serialization; 17 | 18 | import com.hazelcast.core.Hazelcast; 19 | import com.hazelcast.core.HazelcastInstance; 20 | import com.hazelcast.instance.HazelcastInstanceImpl; 21 | import com.hazelcast.instance.HazelcastInstanceProxy; 22 | import com.hazelcast.internal.serialization.impl.AbstractSerializationService; 23 | import com.hazelcast.spi.serialization.SerializationService; 24 | import com.hazelcast.test.HazelcastSerialClassRunner; 25 | import com.hazelcast.test.annotation.ParallelTest; 26 | import com.hazelcast.test.annotation.QuickTest; 27 | import org.hibernate.cache.CacheKey; 28 | import org.hibernate.cache.entry.CacheEntry; 29 | import org.junit.After; 30 | import org.junit.Test; 31 | import org.junit.experimental.categories.Category; 32 | import org.junit.runner.RunWith; 33 | 34 | import java.lang.reflect.Field; 35 | import java.util.concurrent.ConcurrentMap; 36 | 37 | import static org.junit.Assert.assertTrue; 38 | 39 | @RunWith(HazelcastSerialClassRunner.class) 40 | @Category({QuickTest.class, ParallelTest.class}) 41 | public class HibernateSerializationHookAvailableTest { 42 | 43 | private static final Field ORIGINAL; 44 | private static final Field TYPE_MAP; 45 | 46 | static { 47 | try { 48 | ORIGINAL = HazelcastInstanceProxy.class.getDeclaredField("original"); 49 | ORIGINAL.setAccessible(true); 50 | 51 | TYPE_MAP = AbstractSerializationService.class.getDeclaredField("typeMap"); 52 | TYPE_MAP.setAccessible(true); 53 | } catch (Exception e) { 54 | throw new RuntimeException(e); 55 | } 56 | } 57 | 58 | @After 59 | public void teardown() { 60 | Hazelcast.shutdownAll(); 61 | } 62 | 63 | @Test 64 | public void testAutoregistrationOnHibernate3Available() 65 | throws Exception { 66 | 67 | HazelcastInstance hz = Hazelcast.newHazelcastInstance(); 68 | HazelcastInstanceImpl impl = (HazelcastInstanceImpl) ORIGINAL.get(hz); 69 | SerializationService ss = impl.getSerializationService(); 70 | ConcurrentMap typeMap = (ConcurrentMap) TYPE_MAP.get(ss); 71 | 72 | boolean cacheKeySerializerFound = false; 73 | boolean cacheEntrySerializerFound = false; 74 | for (Class clazz : typeMap.keySet()) { 75 | if (clazz == CacheKey.class) { 76 | cacheKeySerializerFound = true; 77 | } else if (clazz == CacheEntry.class) { 78 | cacheEntrySerializerFound = true; 79 | } 80 | } 81 | 82 | assertTrue("CacheKey serializer not found", cacheKeySerializerFound); 83 | assertTrue("CacheEntry serializer not found", cacheEntrySerializerFound); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/test/java/com/hazelcast/hibernate/serialization/ValueTest.java: -------------------------------------------------------------------------------- 1 | package com.hazelcast.hibernate.serialization; 2 | 3 | import com.hazelcast.test.HazelcastSerialClassRunner; 4 | import com.hazelcast.test.annotation.ParallelTest; 5 | import com.hazelcast.test.annotation.QuickTest; 6 | import org.hibernate.util.ComparableComparator; 7 | import org.junit.Test; 8 | import org.junit.experimental.categories.Category; 9 | import org.junit.runner.RunWith; 10 | 11 | import static org.junit.Assert.assertEquals; 12 | import static org.junit.Assert.assertFalse; 13 | import static org.junit.Assert.assertNull; 14 | import static org.junit.Assert.assertTrue; 15 | 16 | @RunWith(HazelcastSerialClassRunner.class) 17 | @Category({QuickTest.class, ParallelTest.class}) 18 | public class ValueTest { 19 | 20 | @Test 21 | public void testGetValue() throws Exception { 22 | String expectedValue = "Some value"; 23 | Value value = new Value(null, 100L, expectedValue); 24 | assertEquals(expectedValue, value.getValue()); 25 | } 26 | 27 | @Test 28 | public void testGetValueWithTimestampBefore() throws Exception { 29 | String expectedValue = "Some value"; 30 | Value value = new Value(null, 100L, expectedValue); 31 | assertNull(value.getValue(99L)); 32 | } 33 | 34 | @Test 35 | public void testGetValueWithTimestampEqual() throws Exception { 36 | String expectedValue = "Some value"; 37 | Value value = new Value(null, 100L, expectedValue); 38 | assertEquals(expectedValue, value.getValue(100L)); 39 | } 40 | 41 | @Test 42 | public void testGetValueWithTimestampAfter() throws Exception { 43 | String expectedValue = "Some value"; 44 | Value value = new Value(null, 100L, expectedValue); 45 | assertEquals(expectedValue, value.getValue(101L)); 46 | } 47 | 48 | @Test 49 | public void testIsReplaceableByTimestampBefore() throws Exception { 50 | String expectedValue = "Some value"; 51 | Value value = new Value(null, 100L, expectedValue); 52 | assertFalse(value.isReplaceableBy(99L, null, null)); 53 | } 54 | 55 | @Test 56 | public void testIsReplaceableByTimestampEqual() throws Exception { 57 | String expectedValue = "Some value"; 58 | Value value = new Value(null, 100L, expectedValue); 59 | assertTrue(value.isReplaceableBy(100L, null, null)); 60 | } 61 | 62 | @Test 63 | public void testIsReplaceableByTimestampAfter() throws Exception { 64 | String expectedValue = "Some value"; 65 | Value value = new Value(null, 100L, expectedValue); 66 | assertTrue(value.isReplaceableBy(101L, null, null)); 67 | } 68 | 69 | @Test 70 | public void testIsReplaceableByVersionBefore() throws Exception { 71 | String expectedValue = "Some value"; 72 | Value value = new Value(10, 100L, expectedValue); 73 | assertFalse(value.isReplaceableBy(99L, 9, ComparableComparator.INSTANCE)); 74 | } 75 | 76 | @Test 77 | public void testIsReplaceableByVersionEqual() throws Exception { 78 | String expectedValue = "Some value"; 79 | Value value = new Value(10, 100L, expectedValue); 80 | assertFalse(value.isReplaceableBy(101L, 10, ComparableComparator.INSTANCE)); 81 | } 82 | 83 | @Test 84 | public void testIsReplaceableByVersionAfter() throws Exception { 85 | String expectedValue = "Some value"; 86 | Value value = new Value(10, 100L, expectedValue); 87 | assertTrue(value.isReplaceableBy(99L, 11, ComparableComparator.INSTANCE)); 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/test/resources/com/hazelcast/hibernate/entity/DummyEntity.hbm.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 17 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/test/resources/com/hazelcast/hibernate/entity/DummyProperty.hbm.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 17 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/test/resources/hazelcast-client-custom.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 17 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/test/resources/hazelcast-custom.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 17 | 21 | 22 | dev-custom 23 | dev-pass 24 | 25 | 26 | 5701 27 | 28 | 29 | 224.2.2.3 30 | 54327 31 | 32 | 33 | 127.0.0.1 34 | 35 | 36 | 37 | 10.3.17.* 38 | 39 | 40 | 41 | 46 | 1 47 | 54 | NONE 55 | 61 | 0 62 | 68 | 25 69 | 70 | 71 | 72 | 73 | 0 74 | 50 75 | 76 | 77 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/test/resources/logging.properties: -------------------------------------------------------------------------------- 1 | 2 | # 3 | # Copyright 2020 Hazelcast Inc. 4 | # 5 | # Licensed under the Hazelcast Community License (the "License"); you may not use 6 | # this file except in compliance with the License. You may obtain a copy of the 7 | # License at 8 | # 9 | # http://hazelcast.com/hazelcast-community-license 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13 | # WARRANTIES OF ANY KIND, either express or implied. See the License for the 14 | # specific language governing permissions and limitations under the License. 15 | # 16 | 17 | # Specify the handlers to create in the root logger 18 | # (all loggers are children of the root logger) 19 | # The following creates two handlers 20 | #handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler 21 | handlers = java.util.logging.ConsoleHandler 22 | #handlers = java.util.logging.FileHandler 23 | 24 | # Set the default logging level for the root logger 25 | .level = INFO 26 | 27 | # Set the default logging level for new ConsoleHandler instances 28 | java.util.logging.ConsoleHandler.level = INFO 29 | 30 | # Set the default logging level for new FileHandler instances 31 | #java.util.logging.FileHandler.level = INFO 32 | 33 | # Set the default formatter for new ConsoleHandler instances 34 | #java.util.logging.ConsoleHandler.formatter = com.hazelcast.impl.LogFormatter 35 | #java.util.logging.FileHandler.formatter = com.hazelcast.impl.LogFormatter 36 | #java.util.logging.FileHandler.pattern = ./hazelcast%u.log 37 | 38 | # Set the default logging level for the logger named com.hazelcast 39 | com.hazelcast.level = FINEST 40 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/test/resources/test-hibernate-client.cfg.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 17 | 20 | 21 | 22 | 23 | org.hibernate.dialect.HSQLDialect 24 | org.hsqldb.jdbcDriver 25 | jdbc:hsqldb:mem:testdb 26 | sa 27 | 28 | 29 | 2 30 | false 31 | create-drop 32 | 33 | true 34 | true 35 | true 36 | 37 | true 38 | localhost 39 | dev-custom 40 | dev-pass 41 | true 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /hazelcast-hibernate3/src/test/resources/test-hibernate.cfg.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 17 | 20 | 21 | 22 | 23 | org.hibernate.dialect.HSQLDialect 24 | org.hsqldb.jdbcDriver 25 | jdbc:hsqldb:mem:testdb 26 | sa 27 | 28 | 29 | 2 30 | false 31 | create-drop 32 | 33 | true 34 | true 35 | true 36 | 37 | hazelcast-custom.xml 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/pom.xml: -------------------------------------------------------------------------------- 1 | 15 | 4.0.0 16 | 17 | hazelcast-hibernate4 18 | hazelcast-hibernate4 19 | jar 20 | 21 | 22 | com.hazelcast 23 | hazelcast-hibernate 24 | 3.8.5-SNAPSHOT 25 | ../pom.xml 26 | 27 | 28 | 29 | 30 | ${project.parent.basedir} 31 | 32 | 4.3.8.Final 33 | 34 | 35 | 36 | 37 | 38 | org.apache.felix 39 | maven-bundle-plugin 40 | 2.3.7 41 | 42 | 43 | bundle-manifest 44 | process-classes 45 | 46 | manifest 47 | 48 | 49 | 50 | com.hazelcast 51 | !com.hazelcast.*, * 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | org.hibernate 63 | hibernate-core 64 | ${hibernate.core.version} 65 | provided 66 | 67 | 68 | slf4j-api 69 | org.slf4j 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/HazelcastTimestamper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate; 17 | 18 | import com.hazelcast.config.Config; 19 | import com.hazelcast.config.MapConfig; 20 | import com.hazelcast.core.HazelcastInstance; 21 | import com.hazelcast.logging.Logger; 22 | 23 | /** 24 | * Helper class to create timestamps and calculate timeouts based on either Hazelcast 25 | * configuration of by requesting values on the cluster. 26 | */ 27 | public final class HazelcastTimestamper { 28 | 29 | private static final int SEC_TO_MS = 1000; 30 | 31 | private HazelcastTimestamper() { 32 | } 33 | 34 | public static long nextTimestamp(HazelcastInstance instance) { 35 | if (instance == null) { 36 | throw new RuntimeException("No Hazelcast instance!"); 37 | } else if (instance.getCluster() == null) { 38 | throw new RuntimeException("Hazelcast instance has no cluster!"); 39 | } 40 | 41 | // System time in ms. 42 | return instance.getCluster().getClusterTime(); 43 | } 44 | 45 | public static int getTimeout(HazelcastInstance instance, String regionName) { 46 | try { 47 | final MapConfig cfg = instance.getConfig().findMapConfig(regionName); 48 | if (cfg.getTimeToLiveSeconds() > 0) { 49 | // TTL in ms 50 | return cfg.getTimeToLiveSeconds() * SEC_TO_MS; 51 | } 52 | } catch (UnsupportedOperationException e) { 53 | // HazelcastInstance is instance of HazelcastClient. 54 | Logger.getLogger(HazelcastTimestamper.class).finest(e); 55 | } 56 | return CacheEnvironment.getDefaultCacheTimeoutInMillis(); 57 | } 58 | 59 | public static long getMaxOperationTimeout(HazelcastInstance instance) { 60 | String maxOpTimeoutProp = null; 61 | try { 62 | Config config = instance.getConfig(); 63 | maxOpTimeoutProp = config.getProperty(CacheEnvironment.HAZELCAST_OPERATION_TIMEOUT); 64 | } catch (UnsupportedOperationException e) { 65 | // HazelcastInstance is instance of HazelcastClient. 66 | Logger.getLogger(HazelcastTimestamper.class).finest(e); 67 | } 68 | if (maxOpTimeoutProp != null) { 69 | return Long.parseLong(maxOpTimeoutProp); 70 | } 71 | return Long.MAX_VALUE; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/RegionCache.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate; 17 | 18 | import org.hibernate.cache.spi.access.SoftLock; 19 | 20 | import java.util.Map; 21 | 22 | /** 23 | * This interface defines an internal cached region implementation as well as a mechanism 24 | * to unmap the cache to an underlying Map data-structure 25 | */ 26 | public interface RegionCache { 27 | 28 | Object get(final Object key, final long txTimestamp); 29 | 30 | boolean insert(final Object key, final Object value, final Object currentVersion); 31 | 32 | boolean put(final Object key, final Object value, final long txTimestamp, final Object version); 33 | 34 | boolean update(final Object key, final Object newValue, final Object newVersion, final SoftLock lock); 35 | 36 | boolean remove(final Object key); 37 | 38 | SoftLock tryLock(final Object key, final Object version); 39 | 40 | void unlock(final Object key, final SoftLock lock); 41 | 42 | boolean contains(final Object key); 43 | 44 | void clear(); 45 | 46 | long size(); 47 | 48 | long getSizeInMemory(); 49 | 50 | Map asMap(); 51 | 52 | } 53 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/VersionAwareMapMergePolicy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate; 17 | 18 | import com.hazelcast.core.EntryView; 19 | import com.hazelcast.map.merge.MapMergePolicy; 20 | import com.hazelcast.nio.ObjectDataInput; 21 | import com.hazelcast.nio.ObjectDataOutput; 22 | import org.hibernate.cache.spi.entry.CacheEntry; 23 | 24 | import java.io.IOException; 25 | 26 | /** 27 | * A merge policy implementation to handle split brain remerges based on the timestamps stored in 28 | * the values. 29 | */ 30 | public class VersionAwareMapMergePolicy implements MapMergePolicy { 31 | 32 | public Object merge(String mapName, EntryView mergingEntry, EntryView existingEntry) { 33 | final Object existingValue = existingEntry != null ? existingEntry.getValue() : null; 34 | final Object mergingValue = mergingEntry.getValue(); 35 | if (existingValue != null && existingValue instanceof CacheEntry 36 | && mergingValue != null && mergingValue instanceof CacheEntry) { 37 | 38 | final CacheEntry existingCacheEntry = (CacheEntry) existingValue; 39 | final CacheEntry mergingCacheEntry = (CacheEntry) mergingValue; 40 | final Object mergingVersionObject = mergingCacheEntry.getVersion(); 41 | final Object existingVersionObject = existingCacheEntry.getVersion(); 42 | if (mergingVersionObject != null && existingVersionObject != null 43 | && mergingVersionObject instanceof Comparable && existingVersionObject instanceof Comparable) { 44 | 45 | final Comparable mergingVersion = (Comparable) mergingVersionObject; 46 | final Comparable existingVersion = (Comparable) existingVersionObject; 47 | 48 | if (mergingVersion.compareTo(existingVersion) > 0) { 49 | return mergingValue; 50 | } else { 51 | return existingValue; 52 | } 53 | } 54 | } 55 | return mergingValue; 56 | } 57 | 58 | @Override 59 | public void writeData(ObjectDataOutput out) throws IOException { 60 | } 61 | 62 | @Override 63 | public void readData(ObjectDataInput in) throws IOException { 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/access/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | /** 17 | * Provides access interfaces/classes for Hibernate. 18 | * such as AccessDelegate , ReadWriteAccessDelegate. 19 | */ 20 | package com.hazelcast.hibernate.access; 21 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/distributed/AbstractRegionCacheEntryProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.distributed; 17 | 18 | import com.hazelcast.hibernate.serialization.Expirable; 19 | import com.hazelcast.hibernate.serialization.HibernateDataSerializerHook; 20 | import com.hazelcast.map.EntryBackupProcessor; 21 | import com.hazelcast.map.EntryProcessor; 22 | import com.hazelcast.nio.serialization.IdentifiedDataSerializable; 23 | 24 | import java.util.Map; 25 | 26 | /** 27 | * An abstract implementation of {@link EntryProcessor} which acts on a hibernate region cache 28 | * {@link com.hazelcast.core.IMap} 29 | */ 30 | public abstract class AbstractRegionCacheEntryProcessor implements EntryProcessor, 31 | EntryBackupProcessor, IdentifiedDataSerializable { 32 | 33 | @Override 34 | public int getFactoryId() { 35 | return HibernateDataSerializerHook.F_ID; 36 | } 37 | 38 | @Override 39 | public void processBackup(Map.Entry entry) { 40 | process(entry); 41 | } 42 | 43 | @Override 44 | public EntryBackupProcessor getBackupProcessor() { 45 | return this; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/distributed/LockEntryProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.distributed; 17 | 18 | import com.hazelcast.hibernate.serialization.Expirable; 19 | import com.hazelcast.hibernate.serialization.ExpiryMarker; 20 | import com.hazelcast.hibernate.serialization.HibernateDataSerializerHook; 21 | import com.hazelcast.nio.ObjectDataInput; 22 | import com.hazelcast.nio.ObjectDataOutput; 23 | 24 | import java.io.IOException; 25 | import java.util.Map; 26 | 27 | /** 28 | * A concrete implementation of {@link com.hazelcast.map.EntryProcessor} which soft-locks 29 | * a region cached entry 30 | */ 31 | public class LockEntryProcessor extends AbstractRegionCacheEntryProcessor { 32 | 33 | private String nextMarkerId; 34 | private long timeout; 35 | private Object version; 36 | 37 | public LockEntryProcessor() { 38 | } 39 | 40 | public LockEntryProcessor(String nextMarkerId, long timeout, Object version) { 41 | this.nextMarkerId = nextMarkerId; 42 | this.timeout = timeout; 43 | this.version = version; 44 | } 45 | 46 | @Override 47 | public Expirable process(Map.Entry entry) { 48 | Expirable expirable = entry.getValue(); 49 | 50 | if (expirable == null) { 51 | expirable = new ExpiryMarker(version, timeout, nextMarkerId); 52 | } else { 53 | expirable = expirable.markForExpiration(timeout, nextMarkerId); 54 | } 55 | 56 | entry.setValue(expirable); 57 | 58 | return expirable; 59 | } 60 | 61 | @Override 62 | public void writeData(ObjectDataOutput out) throws IOException { 63 | out.writeUTF(nextMarkerId); 64 | out.writeLong(timeout); 65 | out.writeObject(version); 66 | } 67 | 68 | @Override 69 | public void readData(ObjectDataInput in) throws IOException { 70 | nextMarkerId = in.readUTF(); 71 | timeout = in.readLong(); 72 | version = in.readObject(); 73 | } 74 | 75 | @Override 76 | public int getId() { 77 | return HibernateDataSerializerHook.LOCK; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/distributed/UnlockEntryProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.distributed; 17 | 18 | import com.hazelcast.hibernate.serialization.Expirable; 19 | import com.hazelcast.hibernate.serialization.ExpiryMarker; 20 | import com.hazelcast.hibernate.serialization.HibernateDataSerializerHook; 21 | import com.hazelcast.nio.ObjectDataInput; 22 | import com.hazelcast.nio.ObjectDataOutput; 23 | 24 | import java.io.IOException; 25 | import java.util.Map; 26 | 27 | /** 28 | * A concrete implementation of {@link com.hazelcast.map.EntryProcessor} which unlocks 29 | * a soft-locked region cached entry 30 | */ 31 | public class UnlockEntryProcessor extends AbstractRegionCacheEntryProcessor { 32 | 33 | private ExpiryMarker lock; 34 | private String nextMarkerId; 35 | private long timestamp; 36 | 37 | public UnlockEntryProcessor() { 38 | } 39 | 40 | public UnlockEntryProcessor(ExpiryMarker lock, String nextMarkerId, long timestamp) { 41 | this.lock = lock; 42 | this.nextMarkerId = nextMarkerId; 43 | this.timestamp = timestamp; 44 | } 45 | 46 | @Override 47 | public Void process(Map.Entry entry) { 48 | Expirable expirable = entry.getValue(); 49 | 50 | if (expirable != null) { 51 | if (expirable.matches(lock)) { 52 | expirable = ((ExpiryMarker) expirable).expire(timestamp); 53 | } else if (expirable.getValue() != null) { 54 | // It's a value. Expire the value immediately. This prevents 55 | // in-flight transactions from adding stale values to the cache 56 | expirable = new ExpiryMarker(null, timestamp, nextMarkerId).expire(timestamp); 57 | } else { 58 | // It's a different marker. Leave it alone. 59 | return null; 60 | } 61 | entry.setValue(expirable); 62 | } 63 | 64 | return null; 65 | } 66 | 67 | @Override 68 | public void writeData(ObjectDataOutput out) throws IOException { 69 | out.writeObject(lock); 70 | out.writeUTF(nextMarkerId); 71 | out.writeLong(timestamp); 72 | } 73 | 74 | @Override 75 | public void readData(ObjectDataInput in) throws IOException { 76 | lock = in.readObject(); 77 | nextMarkerId = in.readUTF(); 78 | timestamp = in.readLong(); 79 | } 80 | 81 | @Override 82 | public int getId() { 83 | return HibernateDataSerializerHook.UNLOCK; 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/distributed/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | /** 17 | * Provides distributed class for Hibernate. 18 | * such as IMapRegionCache. 19 | */ 20 | package com.hazelcast.hibernate.distributed; 21 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/instance/HazelcastInstanceFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.instance; 17 | 18 | import com.hazelcast.hibernate.CacheEnvironment; 19 | import org.hibernate.cache.CacheException; 20 | 21 | import java.util.Properties; 22 | 23 | /** 24 | * A factory class to build up implementations of {@link com.hazelcast.hibernate.instance.IHazelcastInstanceLoader} 25 | * that returns a {@link com.hazelcast.core.HazelcastInstance} depending on configuration either backed by Hazelcast 26 | * client or node implementation. 27 | */ 28 | public final class HazelcastInstanceFactory { 29 | 30 | private static final String HZ_CLIENT_LOADER_CLASSNAME = "com.hazelcast.hibernate.instance.HazelcastClientLoader"; 31 | private static final String HZ_INSTANCE_LOADER_CLASSNAME = "com.hazelcast.hibernate.instance.HazelcastInstanceLoader"; 32 | 33 | private HazelcastInstanceFactory() { 34 | } 35 | 36 | public static IHazelcastInstanceLoader createInstanceLoader(Properties props) throws CacheException { 37 | try { 38 | IHazelcastInstanceLoader instanceLoader = (IHazelcastInstanceLoader) props. 39 | get("com.hazelcast.hibernate.instance.loader"); 40 | if (instanceLoader != null) { 41 | return instanceLoader; 42 | } 43 | Class loaderClass = getInstanceLoaderClass(props); 44 | instanceLoader = (IHazelcastInstanceLoader) loaderClass.newInstance(); 45 | instanceLoader.configure(props); 46 | return instanceLoader; 47 | } catch (Exception e) { 48 | throw new CacheException(e); 49 | } 50 | } 51 | 52 | private static Class getInstanceLoaderClass(Properties props) throws ClassNotFoundException { 53 | ClassLoader cl = HazelcastInstanceFactory.class.getClassLoader(); 54 | if (props != null && CacheEnvironment.isNativeClient(props)) { 55 | return cl.loadClass(HZ_CLIENT_LOADER_CLASSNAME); 56 | } else { 57 | return cl.loadClass(HZ_INSTANCE_LOADER_CLASSNAME); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/instance/IHazelcastInstanceLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.instance; 17 | 18 | import com.hazelcast.core.HazelcastInstance; 19 | import org.hibernate.cache.CacheException; 20 | 21 | import java.util.Properties; 22 | 23 | /** 24 | * Factory interface to build Hazelcast instances and configure them depending 25 | * on configuration. 26 | */ 27 | public interface IHazelcastInstanceLoader { 28 | 29 | /** 30 | * Applies a set of properties to the factory 31 | * 32 | * @param props properties to apply 33 | */ 34 | void configure(Properties props); 35 | 36 | /** 37 | * Create a new {@link com.hazelcast.core.HazelcastInstance} or loads an already 38 | * existing instances by it's name. 39 | * 40 | * @return new or existing HazelcastInstance (either client or node mode) 41 | * @throws CacheException all exceptions wrapped to CacheException 42 | */ 43 | HazelcastInstance loadInstance() throws CacheException; 44 | 45 | /** 46 | * Tries to shutdown a HazelcastInstance 47 | * 48 | * @throws CacheException all exceptions wrapped to CacheException 49 | */ 50 | void unloadInstance() throws CacheException; 51 | } 52 | 53 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/instance/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | /** 17 | * Provides instance interfaces/classes for Hibernate. 18 | */ 19 | package com.hazelcast.hibernate.instance; 20 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/local/CleanupService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.local; 17 | 18 | 19 | import com.hazelcast.instance.OutOfMemoryErrorDispatcher; 20 | 21 | import java.util.concurrent.Executors; 22 | import java.util.concurrent.ScheduledExecutorService; 23 | import java.util.concurrent.ThreadFactory; 24 | import java.util.concurrent.TimeUnit; 25 | 26 | /** 27 | * An internal service to clean cache regions 28 | */ 29 | public final class CleanupService { 30 | 31 | private static final long FIXED_DELAY = 60; 32 | private static final long FIXED_DELAY1 = 60; 33 | 34 | private final String name; 35 | private final ScheduledExecutorService executor; 36 | 37 | public CleanupService(final String name) { 38 | this.name = name; 39 | executor = Executors.newSingleThreadScheduledExecutor(new CleanupThreadFactory()); 40 | } 41 | 42 | public void registerCache(final LocalRegionCache cache) { 43 | executor.scheduleWithFixedDelay(new Runnable() { 44 | public void run() { 45 | cache.cleanup(); 46 | } 47 | }, FIXED_DELAY, FIXED_DELAY1, TimeUnit.SECONDS); 48 | } 49 | 50 | public void stop() { 51 | executor.shutdownNow(); 52 | } 53 | 54 | /** 55 | * Internal ThreadFactory to create cleanup threads 56 | */ 57 | private class CleanupThreadFactory implements ThreadFactory { 58 | 59 | public Thread newThread(final Runnable r) { 60 | final Thread thread = new CleanupThread(r, name + ".hibernate.cleanup"); 61 | thread.setDaemon(true); 62 | return thread; 63 | } 64 | } 65 | 66 | /** 67 | * Runnable thread adapter to capture exceptions and notify Hazelcast about them 68 | */ 69 | private static final class CleanupThread extends Thread { 70 | 71 | private CleanupThread(final Runnable target, final String name) { 72 | super(target, name); 73 | } 74 | 75 | public void run() { 76 | try { 77 | super.run(); 78 | } catch (OutOfMemoryError e) { 79 | OutOfMemoryErrorDispatcher.onOutOfMemory(e); 80 | } 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/local/Invalidation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.local; 17 | 18 | 19 | import com.hazelcast.hibernate.serialization.HibernateDataSerializerHook; 20 | import com.hazelcast.nio.ObjectDataInput; 21 | import com.hazelcast.nio.ObjectDataOutput; 22 | import com.hazelcast.nio.serialization.IdentifiedDataSerializable; 23 | 24 | import java.io.IOException; 25 | 26 | /** 27 | * An invalidation messages 28 | */ 29 | public class Invalidation implements IdentifiedDataSerializable { 30 | 31 | private Object key; 32 | private Object version; 33 | 34 | public Invalidation() { 35 | } 36 | 37 | public Invalidation(final Object key, final Object version) { 38 | this.key = key; 39 | this.version = version; 40 | } 41 | 42 | public Object getKey() { 43 | return key; 44 | } 45 | 46 | public Object getVersion() { 47 | return version; 48 | } 49 | 50 | public void writeData(final ObjectDataOutput out) throws IOException { 51 | out.writeObject(key); 52 | out.writeObject(version); 53 | } 54 | 55 | public void readData(final ObjectDataInput in) throws IOException { 56 | key = in.readObject(); 57 | version = in.readObject(); 58 | } 59 | 60 | @Override 61 | public int getFactoryId() { 62 | return HibernateDataSerializerHook.F_ID; 63 | } 64 | 65 | @Override 66 | public int getId() { 67 | return HibernateDataSerializerHook.INVALIDATION; 68 | } 69 | 70 | @Override 71 | public String toString() { 72 | return "Invalidation{key=" + key + ", version=" + version + '}'; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/local/Timestamp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.local; 17 | 18 | import com.hazelcast.hibernate.serialization.HibernateDataSerializerHook; 19 | import com.hazelcast.nio.ObjectDataInput; 20 | import com.hazelcast.nio.ObjectDataOutput; 21 | import com.hazelcast.nio.serialization.IdentifiedDataSerializable; 22 | 23 | import java.io.IOException; 24 | 25 | /** 26 | * Hazelcast compatible implementation of a timestamp for internal eviction 27 | */ 28 | public class Timestamp implements IdentifiedDataSerializable { 29 | 30 | private Object key; 31 | private long timestamp; 32 | 33 | public Timestamp() { 34 | } 35 | 36 | public Timestamp(final Object key, final long timestamp) { 37 | this.key = key; 38 | this.timestamp = timestamp; 39 | } 40 | 41 | public Object getKey() { 42 | return key; 43 | } 44 | 45 | public long getTimestamp() { 46 | return timestamp; 47 | } 48 | 49 | @Override 50 | public void writeData(final ObjectDataOutput out) throws IOException { 51 | out.writeObject(key); 52 | out.writeLong(timestamp); 53 | } 54 | 55 | @Override 56 | public void readData(final ObjectDataInput in) throws IOException { 57 | key = in.readObject(); 58 | timestamp = in.readLong(); 59 | } 60 | 61 | @Override 62 | public int getFactoryId() { 63 | return HibernateDataSerializerHook.F_ID; 64 | } 65 | 66 | @Override 67 | public int getId() { 68 | return HibernateDataSerializerHook.TIMESTAMP; 69 | } 70 | 71 | @Override 72 | public String toString() { 73 | return "Timestamp" + "{key=" + key + ", timestamp=" + timestamp + '}'; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/local/TimestampsRegionCache.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.local; 17 | 18 | import com.hazelcast.core.HazelcastInstance; 19 | import com.hazelcast.hibernate.RegionCache; 20 | import com.hazelcast.hibernate.serialization.Expirable; 21 | import com.hazelcast.hibernate.serialization.Value; 22 | 23 | /** 24 | * A timestamp based local RegionCache 25 | */ 26 | public class TimestampsRegionCache extends LocalRegionCache implements RegionCache { 27 | 28 | public TimestampsRegionCache(final String name, final HazelcastInstance hazelcastInstance) { 29 | super(name, hazelcastInstance, null); 30 | } 31 | 32 | @Override 33 | public boolean put(Object key, Object value, long txTimestamp, Object version) { 34 | // use the value in txTimestamp as the timestamp instead of the value, since 35 | // hibernate pre-invalidates with a large value, and then invalidates with 36 | //the actual time, which can cause queries to not be cached. 37 | boolean succeed = super.put(key, value, txTimestamp, version); 38 | if (succeed) { 39 | maybeNotifyTopic(key, value, version); 40 | } 41 | return succeed; 42 | } 43 | 44 | @Override 45 | protected void maybeInvalidate(final Object messageObject) { 46 | final Timestamp ts = (Timestamp) messageObject; 47 | final Object key = ts.getKey(); 48 | 49 | if (key == null) { 50 | // Invalidate the entire region cache. 51 | cache.clear(); 52 | return; 53 | } 54 | 55 | for (;;) { 56 | final Expirable value = cache.get(key); 57 | final Long current = value != null ? (Long) value.getValue() : null; 58 | if (current != null) { 59 | if (ts.getTimestamp() > current) { 60 | if (cache.replace(key, value, new Value(value.getVersion(), nextTimestamp(), ts.getTimestamp()))) { 61 | return; 62 | } 63 | } else { 64 | return; 65 | } 66 | } else { 67 | if (cache.putIfAbsent(key, new Value(null, nextTimestamp(), ts.getTimestamp())) == null) { 68 | return; 69 | } 70 | } 71 | } 72 | } 73 | 74 | @Override 75 | protected Object createMessage(final Object key, final Object value, final Object currentVersion) { 76 | return new Timestamp(key, (Long) value); 77 | } 78 | 79 | @Override 80 | public void clear() { 81 | cache.clear(); 82 | maybeNotifyTopic(null, -1L, null); 83 | } 84 | 85 | final void cleanup() { 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/local/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | /** 17 | * Provides local classes for Hibernate. 18 | * such as TimeStamp,CleanupService. 19 | */ 20 | package com.hazelcast.hibernate.local; 21 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | /** 17 | * Contains interfaces/classes related to Hibernate. 18 | */ 19 | package com.hazelcast.hibernate; 20 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/region/AbstractGeneralRegion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.region; 17 | 18 | import com.hazelcast.core.HazelcastInstance; 19 | import com.hazelcast.core.OperationTimeoutException; 20 | import com.hazelcast.hibernate.RegionCache; 21 | import com.hazelcast.logging.Logger; 22 | import org.hibernate.cache.CacheException; 23 | import org.hibernate.cache.spi.GeneralDataRegion; 24 | 25 | import java.util.Properties; 26 | 27 | /** 28 | * Basic implementation of a IMap based cache without any special security checks 29 | * 30 | * @author Leo Kim (lkim@limewire.com) 31 | * @param implementation type of RegionCache 32 | */ 33 | abstract class AbstractGeneralRegion extends AbstractHazelcastRegion 34 | implements GeneralDataRegion { 35 | 36 | private final Cache cache; 37 | 38 | protected AbstractGeneralRegion(final HazelcastInstance instance, final String name 39 | , final Properties props, final Cache cache) { 40 | super(instance, name, props); 41 | this.cache = cache; 42 | } 43 | 44 | public void evict(final Object key) throws CacheException { 45 | try { 46 | getCache().remove(key); 47 | } catch (OperationTimeoutException e) { 48 | Logger.getLogger(AbstractGeneralRegion.class).finest(e); 49 | } 50 | } 51 | 52 | public void evictAll() throws CacheException { 53 | try { 54 | getCache().clear(); 55 | } catch (OperationTimeoutException e) { 56 | Logger.getLogger(AbstractGeneralRegion.class).finest(e); 57 | } 58 | } 59 | 60 | public Object get(final Object key) throws CacheException { 61 | try { 62 | return getCache().get(key, nextTimestamp()); 63 | } catch (OperationTimeoutException e) { 64 | return null; 65 | } 66 | } 67 | 68 | public void put(final Object key, final Object value) throws CacheException { 69 | try { 70 | getCache().put(key, value, nextTimestamp(), null); 71 | } catch (OperationTimeoutException e) { 72 | Logger.getLogger(AbstractGeneralRegion.class).finest(e); 73 | } 74 | } 75 | 76 | public Cache getCache() { 77 | return cache; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/region/AbstractTransactionalDataRegion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.region; 17 | 18 | import com.hazelcast.core.HazelcastInstance; 19 | import com.hazelcast.hibernate.RegionCache; 20 | import org.hibernate.cache.spi.CacheDataDescription; 21 | import org.hibernate.cache.spi.TransactionalDataRegion; 22 | 23 | import java.util.Properties; 24 | 25 | /** 26 | * Abstract base class of all regions 27 | * 28 | * @author Leo Kim (lkim@limewire.com) 29 | * @param implementation type of RegionCache 30 | */ 31 | public abstract class AbstractTransactionalDataRegion extends AbstractHazelcastRegion 32 | implements 33 | TransactionalDataRegion { 34 | 35 | private final CacheDataDescription metadata; 36 | private final Cache cache; 37 | 38 | protected AbstractTransactionalDataRegion(final HazelcastInstance instance, final String regionName, 39 | final Properties props, final CacheDataDescription metadata, final Cache cache) { 40 | super(instance, regionName, props); 41 | this.metadata = metadata; 42 | this.cache = cache; 43 | } 44 | 45 | public CacheDataDescription getCacheDataDescription() { 46 | return metadata; 47 | } 48 | 49 | public boolean isTransactionAware() { 50 | return false; 51 | } 52 | 53 | public Cache getCache() { 54 | return cache; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/region/CollectionRegionAccessStrategyAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.region; 17 | 18 | import com.hazelcast.hibernate.access.AccessDelegate; 19 | import org.hibernate.cache.CacheException; 20 | import org.hibernate.cache.spi.CollectionRegion; 21 | import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; 22 | import org.hibernate.cache.spi.access.SoftLock; 23 | 24 | /** 25 | * Simple adapter implementation for transactional / concurrent access control on collections 26 | * 27 | * @author Leo Kim (lkim@limewire.com) 28 | */ 29 | public final class CollectionRegionAccessStrategyAdapter implements CollectionRegionAccessStrategy { 30 | 31 | private final AccessDelegate delegate; 32 | 33 | public CollectionRegionAccessStrategyAdapter(final AccessDelegate delegate) { 34 | this.delegate = delegate; 35 | } 36 | 37 | public void evict(final Object key) throws CacheException { 38 | delegate.evict(key); 39 | } 40 | 41 | public void evictAll() throws CacheException { 42 | delegate.evictAll(); 43 | } 44 | 45 | public Object get(final Object key, final long txTimestamp) throws CacheException { 46 | return delegate.get(key, txTimestamp); 47 | } 48 | 49 | public CollectionRegion getRegion() { 50 | return delegate.getHazelcastRegion(); 51 | } 52 | 53 | public SoftLock lockItem(final Object key, final Object version) throws CacheException { 54 | return delegate.lockItem(key, version); 55 | } 56 | 57 | public SoftLock lockRegion() throws CacheException { 58 | return delegate.lockRegion(); 59 | } 60 | 61 | public boolean putFromLoad(final Object key, final Object value, final long txTimestamp, final Object version) 62 | throws CacheException { 63 | return delegate.putFromLoad(key, value, txTimestamp, version); 64 | } 65 | 66 | public boolean putFromLoad(final Object key, final Object value, final long txTimestamp, final Object version, 67 | final boolean minimalPutOverride) throws CacheException { 68 | return delegate.putFromLoad(key, value, txTimestamp, version, minimalPutOverride); 69 | } 70 | 71 | public void remove(final Object key) throws CacheException { 72 | delegate.remove(key); 73 | } 74 | 75 | public void removeAll() throws CacheException { 76 | delegate.removeAll(); 77 | } 78 | 79 | public void unlockItem(final Object key, final SoftLock lock) throws CacheException { 80 | delegate.unlockItem(key, lock); 81 | } 82 | 83 | public void unlockRegion(final SoftLock lock) throws CacheException { 84 | delegate.unlockRegion(lock); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/region/HazelcastCollectionRegion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.region; 17 | 18 | import com.hazelcast.core.HazelcastInstance; 19 | import com.hazelcast.hibernate.RegionCache; 20 | import com.hazelcast.hibernate.access.NonStrictReadWriteAccessDelegate; 21 | import com.hazelcast.hibernate.access.ReadOnlyAccessDelegate; 22 | import com.hazelcast.hibernate.access.ReadWriteAccessDelegate; 23 | import org.hibernate.cache.CacheException; 24 | import org.hibernate.cache.spi.CacheDataDescription; 25 | import org.hibernate.cache.spi.CollectionRegion; 26 | import org.hibernate.cache.spi.access.AccessType; 27 | import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; 28 | 29 | import java.util.Properties; 30 | 31 | /** 32 | * An collection region implementation based upon Hazelcast IMap with basic concurrency / transactional support 33 | * by supplying CollectionRegionAccessStrategy 34 | * 35 | * @author mdogan 11/9/12 36 | * @param implementation type of RegionCache 37 | */ 38 | public final class HazelcastCollectionRegion extends AbstractTransactionalDataRegion 39 | implements CollectionRegion { 40 | 41 | public HazelcastCollectionRegion(final HazelcastInstance instance, 42 | final String regionName, final Properties props, 43 | final CacheDataDescription metadata, final Cache cache) { 44 | super(instance, regionName, props, metadata, cache); 45 | } 46 | 47 | public CollectionRegionAccessStrategy buildAccessStrategy(final AccessType accessType) throws CacheException { 48 | if (AccessType.READ_ONLY.equals(accessType)) { 49 | return new CollectionRegionAccessStrategyAdapter( 50 | new ReadOnlyAccessDelegate(this, props)); 51 | } 52 | if (AccessType.NONSTRICT_READ_WRITE.equals(accessType)) { 53 | return new CollectionRegionAccessStrategyAdapter( 54 | new NonStrictReadWriteAccessDelegate(this, props)); 55 | } 56 | if (AccessType.READ_WRITE.equals(accessType)) { 57 | return new CollectionRegionAccessStrategyAdapter( 58 | new ReadWriteAccessDelegate(this, props)); 59 | } 60 | throw new CacheException("AccessType \"" + accessType + "\" is not currently supported by Hazelcast."); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/region/HazelcastEntityRegion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.region; 17 | 18 | import com.hazelcast.core.HazelcastInstance; 19 | import com.hazelcast.hibernate.RegionCache; 20 | import com.hazelcast.hibernate.access.NonStrictReadWriteAccessDelegate; 21 | import com.hazelcast.hibernate.access.ReadOnlyAccessDelegate; 22 | import com.hazelcast.hibernate.access.ReadWriteAccessDelegate; 23 | import org.hibernate.cache.CacheException; 24 | import org.hibernate.cache.spi.CacheDataDescription; 25 | import org.hibernate.cache.spi.EntityRegion; 26 | import org.hibernate.cache.spi.access.AccessType; 27 | import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; 28 | 29 | import java.util.Properties; 30 | 31 | /** 32 | * An entity region implementation based upon Hazelcast IMap with basic concurrency / transactional support 33 | * by supplying EntityRegionAccessStrategy 34 | * 35 | * @author mdogan 11/9/12 36 | * @param implementation type of RegionCache 37 | */ 38 | public final class HazelcastEntityRegion 39 | extends AbstractTransactionalDataRegion implements EntityRegion { 40 | 41 | public HazelcastEntityRegion(final HazelcastInstance instance, 42 | final String regionName, final Properties props, 43 | final CacheDataDescription metadata, final Cache cache) { 44 | super(instance, regionName, props, metadata, cache); 45 | } 46 | 47 | public EntityRegionAccessStrategy buildAccessStrategy(final AccessType accessType) throws CacheException { 48 | if (AccessType.READ_ONLY.equals(accessType)) { 49 | return new EntityRegionAccessStrategyAdapter( 50 | new ReadOnlyAccessDelegate(this, props)); 51 | } 52 | if (AccessType.NONSTRICT_READ_WRITE.equals(accessType)) { 53 | return new EntityRegionAccessStrategyAdapter( 54 | new NonStrictReadWriteAccessDelegate(this, props)); 55 | } 56 | if (AccessType.READ_WRITE.equals(accessType)) { 57 | return new EntityRegionAccessStrategyAdapter( 58 | new ReadWriteAccessDelegate(this, props)); 59 | } 60 | throw new CacheException("AccessType \"" + accessType + "\" is not currently supported by Hazelcast."); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/region/HazelcastNaturalIdRegion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.region; 17 | 18 | import com.hazelcast.core.HazelcastInstance; 19 | import com.hazelcast.hibernate.RegionCache; 20 | import com.hazelcast.hibernate.access.NonStrictReadWriteAccessDelegate; 21 | import com.hazelcast.hibernate.access.ReadOnlyAccessDelegate; 22 | import com.hazelcast.hibernate.access.ReadWriteAccessDelegate; 23 | import org.hibernate.cache.CacheException; 24 | import org.hibernate.cache.spi.CacheDataDescription; 25 | import org.hibernate.cache.spi.NaturalIdRegion; 26 | import org.hibernate.cache.spi.access.AccessType; 27 | import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy; 28 | 29 | import java.util.Properties; 30 | 31 | /** 32 | * Hazelcast based implementation used to store NaturalIds 33 | * 34 | * @param type of Cache 35 | */ 36 | public class HazelcastNaturalIdRegion 37 | extends AbstractTransactionalDataRegion 38 | implements NaturalIdRegion { 39 | 40 | public HazelcastNaturalIdRegion(final HazelcastInstance instance, final String regionName, 41 | final Properties props, final CacheDataDescription metadata, 42 | final Cache cache) { 43 | super(instance, regionName, props, metadata, cache); 44 | } 45 | 46 | public NaturalIdRegionAccessStrategy buildAccessStrategy(final AccessType accessType) throws CacheException { 47 | if (AccessType.READ_ONLY.equals(accessType)) { 48 | return new NaturalIdRegionAccessStrategyAdapter( 49 | new ReadOnlyAccessDelegate(this, props)); 50 | } 51 | if (AccessType.NONSTRICT_READ_WRITE.equals(accessType)) { 52 | return new NaturalIdRegionAccessStrategyAdapter( 53 | new NonStrictReadWriteAccessDelegate(this, props)); 54 | } 55 | if (AccessType.READ_WRITE.equals(accessType)) { 56 | return new NaturalIdRegionAccessStrategyAdapter( 57 | new ReadWriteAccessDelegate(this, props)); 58 | } 59 | throw new CacheException("AccessType \"" + accessType + "\" is not currently supported by Hazelcast."); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/region/HazelcastQueryResultsRegion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.region; 17 | 18 | import com.hazelcast.core.HazelcastInstance; 19 | import com.hazelcast.hibernate.local.LocalRegionCache; 20 | import org.hibernate.cache.spi.QueryResultsRegion; 21 | 22 | import java.util.Properties; 23 | 24 | /** 25 | * Hazelcast based implementation of a storage region for query results 26 | */ 27 | public class HazelcastQueryResultsRegion extends AbstractGeneralRegion implements QueryResultsRegion { 28 | 29 | public HazelcastQueryResultsRegion(final HazelcastInstance instance, final String name, final Properties props) { 30 | // Note: The HazelcastInstance _must_ be passed down here. Otherwise query caches 31 | // cannot be configured and will always use defaults. However, even though we're 32 | // passing the HazelcastInstance, we don't want to use an ITopic for invalidation 33 | // because the timestamps cache can take care of outdated queries 34 | super(instance, name, props, new LocalRegionCache(name, instance, null, false)); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/region/HazelcastRegion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.region; 17 | 18 | import com.hazelcast.core.HazelcastInstance; 19 | import com.hazelcast.hibernate.RegionCache; 20 | import com.hazelcast.logging.ILogger; 21 | import org.hibernate.cache.spi.Region; 22 | 23 | /** 24 | * Hazelcast specific interface version of Hibernate's Region 25 | * 26 | * @author Leo Kim (lkim@limewire.com) 27 | * @param implementation type of RegionCache 28 | */ 29 | public interface HazelcastRegion extends Region { 30 | 31 | HazelcastInstance getInstance(); 32 | 33 | Cache getCache(); 34 | 35 | ILogger getLogger(); 36 | } 37 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/region/HazelcastTimestampsRegion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.region; 17 | 18 | import com.hazelcast.core.HazelcastInstance; 19 | import com.hazelcast.hibernate.RegionCache; 20 | import org.hibernate.cache.spi.TimestampsRegion; 21 | 22 | import java.util.Properties; 23 | 24 | /** 25 | * Hazelcast based timestamp using region implementation 26 | * 27 | * @param implementation type of RegionCache 28 | */ 29 | public class HazelcastTimestampsRegion 30 | extends AbstractGeneralRegion implements TimestampsRegion { 31 | 32 | public HazelcastTimestampsRegion(final HazelcastInstance instance, final String name, 33 | final Properties props, final Cache cache) { 34 | super(instance, name, props, cache); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/region/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | /** 17 | * Provides region interfaces/classes for Hibernate. 18 | */ 19 | package com.hazelcast.hibernate.region; 20 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/serialization/Hibernate4CacheEntrySerializerHook.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.serialization; 17 | 18 | import com.hazelcast.logging.Logger; 19 | import com.hazelcast.nio.UnsafeHelper; 20 | import com.hazelcast.nio.serialization.Serializer; 21 | import com.hazelcast.nio.serialization.SerializerHook; 22 | 23 | /** 24 | * This class is used to register a special serializer to not loose 25 | * power over serialization in Hibernate 3 26 | */ 27 | public class Hibernate4CacheEntrySerializerHook 28 | implements SerializerHook { 29 | 30 | private static final String SKIP_INIT_MSG = "Hibernate4 not available, skipping serializer initialization"; 31 | 32 | private final Class cacheEntryClass; 33 | 34 | public Hibernate4CacheEntrySerializerHook() { 35 | Class cacheEntryClass = null; 36 | if (UnsafeHelper.UNSAFE_AVAILABLE) { 37 | try { 38 | cacheEntryClass = Class.forName("org.hibernate.cache.spi.entry.CacheEntry"); 39 | } catch (Exception e) { 40 | Logger.getLogger(Hibernate4CacheEntrySerializerHook.class).finest(SKIP_INIT_MSG); 41 | } 42 | } 43 | this.cacheEntryClass = cacheEntryClass; 44 | } 45 | 46 | @Override 47 | public Class getSerializationType() { 48 | return cacheEntryClass; 49 | } 50 | 51 | @Override 52 | public Serializer createSerializer() { 53 | if (cacheEntryClass == null) { 54 | return null; 55 | } 56 | if (cacheEntryClass.isInterface()) { 57 | //Hibernate 4.2 and later have a CacheEntry interface, not a class 58 | return new Hibernate42CacheEntrySerializer(); 59 | } 60 | //Hibernate 4.1 and earlier have a CacheEntry class 61 | return new Hibernate41CacheEntrySerializer(); 62 | } 63 | 64 | @Override 65 | public boolean isOverwritable() { 66 | return true; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/serialization/Hibernate4CacheKeySerializerHook.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.serialization; 17 | 18 | import com.hazelcast.logging.Logger; 19 | import com.hazelcast.nio.UnsafeHelper; 20 | import com.hazelcast.nio.serialization.Serializer; 21 | import com.hazelcast.nio.serialization.SerializerHook; 22 | 23 | /** 24 | * This class is used to register a special serializer to not loose 25 | * power over serialization in Hibernate 3 26 | */ 27 | public class Hibernate4CacheKeySerializerHook 28 | implements SerializerHook { 29 | 30 | private static final String SKIP_INIT_MSG = "Hibernate4 not available, skipping serializer initialization"; 31 | 32 | private final Class cacheKeyClass; 33 | 34 | public Hibernate4CacheKeySerializerHook() { 35 | Class cacheKeyClass = null; 36 | if (UnsafeHelper.UNSAFE_AVAILABLE) { 37 | try { 38 | cacheKeyClass = Class.forName("org.hibernate.cache.spi.CacheKey"); 39 | } catch (Exception e) { 40 | Logger.getLogger(Hibernate4CacheKeySerializerHook.class).finest(SKIP_INIT_MSG); 41 | } 42 | } 43 | this.cacheKeyClass = cacheKeyClass; 44 | } 45 | 46 | @Override 47 | public Class getSerializationType() { 48 | return cacheKeyClass; 49 | } 50 | 51 | @Override 52 | public Serializer createSerializer() { 53 | if (cacheKeyClass != null) { 54 | return new Hibernate4CacheKeySerializer(); 55 | } 56 | return null; 57 | } 58 | 59 | @Override 60 | public boolean isOverwritable() { 61 | return true; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/serialization/MarkerWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.serialization; 17 | 18 | import org.hibernate.cache.spi.access.SoftLock; 19 | 20 | /** 21 | * A wrapper class for ExpiryMarker for returning copy of marker object with 22 | * {@link org.hibernate.cache.spi.access.SoftLock} marker interface. 23 | */ 24 | 25 | public class MarkerWrapper implements SoftLock { 26 | 27 | private final ExpiryMarker marker; 28 | 29 | public MarkerWrapper(ExpiryMarker marker) { 30 | this.marker = marker; 31 | } 32 | 33 | public ExpiryMarker getMarker() { 34 | return marker; 35 | } 36 | } 37 | 38 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/serialization/Value.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.serialization; 17 | 18 | import com.hazelcast.nio.ObjectDataInput; 19 | import com.hazelcast.nio.ObjectDataOutput; 20 | import java.io.IOException; 21 | import java.util.Comparator; 22 | 23 | /** 24 | * A value within a region cache 25 | */ 26 | public class Value extends Expirable { 27 | 28 | private long timestamp; 29 | private Object value; 30 | 31 | public Value() { 32 | } 33 | 34 | public Value(Object version, long timestamp, Object value) { 35 | super(version); 36 | this.timestamp = timestamp; 37 | this.value = value; 38 | } 39 | 40 | @Override 41 | @SuppressWarnings("unchecked") 42 | public boolean isReplaceableBy(long txTimestamp, Object newVersion, Comparator versionComparator) { 43 | return version == null 44 | ? timestamp <= txTimestamp 45 | : versionComparator.compare(version, newVersion) < 0; 46 | } 47 | 48 | public long getTimestamp() { 49 | return timestamp; 50 | } 51 | 52 | public Object getValue() { 53 | return value; 54 | } 55 | 56 | @Override 57 | public Object getValue(long txTimestamp) { 58 | return timestamp <= txTimestamp ? value : null; 59 | } 60 | 61 | @Override 62 | public boolean matches(ExpiryMarker lock) { 63 | return false; 64 | } 65 | 66 | @Override 67 | public ExpiryMarker markForExpiration(long timeout, String nextMarkerId) { 68 | return new ExpiryMarker(version, timeout, nextMarkerId); 69 | } 70 | 71 | @Override 72 | public void readData(ObjectDataInput in) throws IOException { 73 | super.readData(in); 74 | timestamp = in.readLong(); 75 | value = in.readObject(); 76 | } 77 | 78 | @Override 79 | public void writeData(ObjectDataOutput out) throws IOException { 80 | super.writeData(out); 81 | out.writeLong(timestamp); 82 | out.writeObject(value); 83 | } 84 | 85 | @Override 86 | public int getFactoryId() { 87 | return HibernateDataSerializerHook.F_ID; 88 | } 89 | 90 | @Override 91 | public int getId() { 92 | return HibernateDataSerializerHook.VALUE; 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/java/com/hazelcast/hibernate/serialization/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | /** 17 | * This class contains the Hibernate4 serializer hooks so what we don't 18 | * loose handling on serialization while working on hibernate 19 | */ 20 | package com.hazelcast.hibernate.serialization; 21 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/resources/META-INF/services/com.hazelcast.DataSerializerHook: -------------------------------------------------------------------------------- 1 | com.hazelcast.hibernate.serialization.HibernateDataSerializerHook -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/main/resources/META-INF/services/com.hazelcast.SerializerHook: -------------------------------------------------------------------------------- 1 | com.hazelcast.hibernate.serialization.Hibernate4CacheKeySerializerHook 2 | com.hazelcast.hibernate.serialization.Hibernate4CacheEntrySerializerHook -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/test/java/com/hazelcast/hibernate/LocalRegionFactorySlowTest.java: -------------------------------------------------------------------------------- 1 | package com.hazelcast.hibernate; 2 | 3 | import com.hazelcast.hibernate.entity.DummyEntity; 4 | import com.hazelcast.test.HazelcastSerialClassRunner; 5 | import com.hazelcast.test.annotation.SlowTest; 6 | import org.hibernate.Session; 7 | import org.hibernate.Transaction; 8 | import org.hibernate.cfg.Environment; 9 | import org.junit.Test; 10 | import org.junit.experimental.categories.Category; 11 | import org.junit.runner.RunWith; 12 | 13 | import java.util.List; 14 | import java.util.Properties; 15 | 16 | import static org.junit.Assert.assertEquals; 17 | import static org.junit.Assert.assertNotNull; 18 | 19 | @RunWith(HazelcastSerialClassRunner.class) 20 | @Category(SlowTest.class) 21 | public class LocalRegionFactorySlowTest extends HibernateSlowTestSupport { 22 | 23 | @Test 24 | public void test_query_with_non_mock_network() { 25 | final int entityCount = 10; 26 | final int queryCount = 2; 27 | insertDummyEntities(entityCount); 28 | List list = null; 29 | for (int i = 0; i < queryCount; i++) { 30 | list = executeQuery(sf); 31 | assertEquals(entityCount, list.size()); 32 | } 33 | for (int i = 0; i < queryCount; i++) { 34 | list = executeQuery(sf2); 35 | assertEquals(entityCount, list.size()); 36 | } 37 | 38 | assertNotNull(list); 39 | DummyEntity toDelete = list.get(0); 40 | Session session = sf2.openSession(); 41 | Transaction tx = session.beginTransaction(); 42 | try { 43 | session.delete(toDelete); 44 | tx.commit(); 45 | } catch (Exception e) { 46 | tx.rollback(); 47 | e.printStackTrace(); 48 | } finally { 49 | session.close(); 50 | } 51 | assertEquals(entityCount - 1, executeQuery(sf).size()); 52 | assertEquals(entityCount - 1, executeQuery(sf2).size()); 53 | 54 | } 55 | 56 | @Override 57 | protected Properties getCacheProperties() { 58 | Properties props = new Properties(); 59 | props.setProperty(Environment.CACHE_REGION_FACTORY, HazelcastLocalCacheRegionFactory.class.getName()); 60 | return props; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/test/java/com/hazelcast/hibernate/NativeClientTest.java: -------------------------------------------------------------------------------- 1 | package com.hazelcast.hibernate; 2 | 3 | import com.hazelcast.hibernate.entity.DummyEntity; 4 | import com.hazelcast.hibernate.entity.DummyProperty; 5 | import com.hazelcast.test.HazelcastSerialClassRunner; 6 | import com.hazelcast.test.annotation.SlowTest; 7 | import org.hibernate.Session; 8 | import org.hibernate.SessionFactory; 9 | import org.hibernate.Transaction; 10 | import org.hibernate.cfg.Configuration; 11 | import org.hibernate.cfg.Environment; 12 | import org.junit.After; 13 | import org.junit.Before; 14 | import org.junit.Test; 15 | import org.junit.experimental.categories.Category; 16 | import org.junit.runner.RunWith; 17 | 18 | import java.net.URL; 19 | import java.util.Date; 20 | import java.util.Properties; 21 | 22 | import static org.junit.Assert.assertEquals; 23 | 24 | @RunWith(HazelcastSerialClassRunner.class) 25 | @Category(SlowTest.class) 26 | public class NativeClientTest 27 | extends HibernateSlowTestSupport { 28 | 29 | protected SessionFactory clientSf; 30 | 31 | @Override 32 | protected Properties getCacheProperties() { 33 | Properties props = new Properties(); 34 | props.setProperty(Environment.CACHE_REGION_FACTORY, HazelcastCacheRegionFactory.class.getName()); 35 | return props; 36 | } 37 | 38 | @Before 39 | @Override 40 | public void postConstruct() { 41 | sf = createSessionFactory(getCacheProperties(), null); 42 | clientSf = createClientSessionFactory(getCacheProperties()); 43 | } 44 | 45 | @Test 46 | public void testInsertLoad() { 47 | Session session = clientSf.openSession(); 48 | Transaction tx = session.beginTransaction(); 49 | DummyEntity e = new DummyEntity((long) 1, "dummy:1", 123456d, new Date()); 50 | session.save(e); 51 | tx.commit(); 52 | session.close(); 53 | 54 | session = clientSf.openSession(); 55 | DummyEntity retrieved = (DummyEntity) session.get(DummyEntity.class, (long)1); 56 | assertEquals("dummy:1", retrieved.getName()); 57 | } 58 | 59 | @After 60 | public void tearDown() { 61 | if(clientSf !=null) { 62 | clientSf.close(); 63 | } 64 | } 65 | 66 | protected SessionFactory createClientSessionFactory(Properties props) { 67 | Configuration conf = new Configuration(); 68 | URL xml = HibernateTestSupport.class.getClassLoader().getResource("test-hibernate-client.cfg.xml"); 69 | conf.configure(xml); 70 | conf.setCacheConcurrencyStrategy(DummyEntity.class.getName(), getCacheStrategy()); 71 | conf.setCacheConcurrencyStrategy(DummyProperty.class.getName(), getCacheStrategy()); 72 | conf.setCollectionCacheConcurrencyStrategy(DummyEntity.class.getName() + ".properties", getCacheStrategy()); 73 | conf.addProperties(props); 74 | final SessionFactory sf = conf.buildSessionFactory(); 75 | sf.getStatistics().setStatisticsEnabled(true); 76 | return sf; 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/test/java/com/hazelcast/hibernate/RegionFactoryQueryTest.java: -------------------------------------------------------------------------------- 1 | package com.hazelcast.hibernate; 2 | 3 | import com.hazelcast.hibernate.entity.DummyEntity; 4 | import com.hazelcast.test.HazelcastSerialClassRunner; 5 | import com.hazelcast.test.annotation.SlowTest; 6 | import org.hibernate.Session; 7 | import org.hibernate.Transaction; 8 | import org.hibernate.cfg.Environment; 9 | import org.junit.Before; 10 | import org.junit.Test; 11 | import org.junit.experimental.categories.Category; 12 | import org.junit.runner.RunWith; 13 | 14 | import java.util.List; 15 | import java.util.Properties; 16 | 17 | import static org.junit.Assert.assertEquals; 18 | import static org.junit.Assert.assertNotNull; 19 | 20 | @RunWith(HazelcastSerialClassRunner.class) 21 | @Category(SlowTest.class) 22 | public class RegionFactoryQueryTest extends HibernateSlowTestSupport{ 23 | 24 | @Before 25 | @Override 26 | public void postConstruct() { 27 | sf = createSessionFactory(getCacheProperties(), null); 28 | sf2 = createSessionFactory(getCacheProperties(), null); 29 | } 30 | 31 | @Test 32 | public void test_query_with_non_mock_network() { 33 | final int entityCount = 10; 34 | final int queryCount = 2; 35 | insertDummyEntities(entityCount); 36 | List list = null; 37 | for (int i = 0; i < queryCount; i++) { 38 | list = executeQuery(sf); 39 | assertEquals(entityCount, list.size()); 40 | } 41 | for (int i = 0; i < queryCount; i++) { 42 | list = executeQuery(sf2); 43 | assertEquals(entityCount, list.size()); 44 | } 45 | 46 | assertNotNull(list); 47 | DummyEntity toDelete = list.get(0); 48 | Session session = sf.openSession(); 49 | Transaction tx = session.beginTransaction(); 50 | try { 51 | session.delete(toDelete); 52 | tx.commit(); 53 | } catch (Exception e) { 54 | tx.rollback(); 55 | e.printStackTrace(); 56 | } finally { 57 | session.close(); 58 | } 59 | assertEquals(entityCount - 1, executeQuery(sf).size()); 60 | assertEquals(entityCount - 1, executeQuery(sf2).size()); 61 | 62 | } 63 | 64 | protected Properties getCacheProperties() { 65 | Properties props = new Properties(); 66 | props.setProperty(Environment.CACHE_REGION_FACTORY, HazelcastCacheRegionFactory.class.getName()); 67 | return props; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/test/java/com/hazelcast/hibernate/VersionAwareMapMergePolicyTest.java: -------------------------------------------------------------------------------- 1 | package com.hazelcast.hibernate; 2 | 3 | import com.hazelcast.core.EntryView; 4 | import com.hazelcast.map.merge.MapMergePolicy; 5 | import com.hazelcast.test.HazelcastSerialClassRunner; 6 | import com.hazelcast.test.annotation.ParallelTest; 7 | import com.hazelcast.test.annotation.QuickTest; 8 | import org.hibernate.cache.spi.entry.CacheEntry; 9 | import org.hibernate.persister.entity.EntityPersister; 10 | import org.junit.Before; 11 | import org.junit.Test; 12 | import org.junit.experimental.categories.Category; 13 | import org.junit.runner.RunWith; 14 | 15 | import static org.junit.Assert.assertEquals; 16 | import static org.mockito.Mockito.mock; 17 | import static org.mockito.Mockito.when; 18 | 19 | @RunWith(HazelcastSerialClassRunner.class) 20 | @Category({QuickTest.class, ParallelTest.class}) 21 | public class VersionAwareMapMergePolicyTest { 22 | 23 | 24 | private static final MockVersion versionOld = new MockVersion(0); 25 | private static final MockVersion versionNew = new MockVersion(1); 26 | 27 | protected MapMergePolicy policy; 28 | 29 | @Before 30 | public void given() { 31 | policy = new VersionAwareMapMergePolicy(); 32 | } 33 | 34 | @Test 35 | public void merge_mergingUptodate() { 36 | CacheEntry existing = cacheEntryWithVersion(versionOld); 37 | CacheEntry merging = cacheEntryWithVersion(versionNew); 38 | 39 | EntryView entryExisting = entryWithGivenValue(existing); 40 | EntryView entryMerging = entryWithGivenValue(merging); 41 | 42 | assertEquals(merging, policy.merge("map", entryMerging, entryExisting)); 43 | } 44 | 45 | @Test 46 | public void merge_mergingStale() { 47 | CacheEntry existing = cacheEntryWithVersion(versionNew); 48 | CacheEntry merging = cacheEntryWithVersion(versionOld); 49 | 50 | EntryView entryExisting = entryWithGivenValue(existing); 51 | EntryView entryMerging = entryWithGivenValue(merging); 52 | 53 | assertEquals(existing, policy.merge("map", entryMerging, entryExisting)); 54 | } 55 | 56 | @Test 57 | public void merge_mergingNull() { 58 | CacheEntry existing = null; 59 | CacheEntry merging = cacheEntryWithVersion(versionNew); 60 | 61 | EntryView entryExisting = entryWithGivenValue(existing); 62 | EntryView entryMerging = entryWithGivenValue(merging); 63 | 64 | assertEquals(merging, policy.merge("map", entryMerging, entryExisting)); 65 | } 66 | 67 | 68 | private CacheEntry cacheEntryWithVersion(MockVersion mockVersion) { 69 | CacheEntry cacheEntry = mock(CacheEntry.class); 70 | when(cacheEntry.getVersion()).thenReturn(mockVersion); 71 | return cacheEntry; 72 | } 73 | 74 | private EntryView entryWithGivenValue(Object value) { 75 | EntryView entryView = mock(EntryView.class); 76 | try { 77 | when(entryView.getValue()).thenReturn(value); 78 | return entryView; 79 | } catch (Exception e) { 80 | throw new RuntimeException(e); 81 | } 82 | 83 | } 84 | 85 | protected static class MockVersion implements Comparable { 86 | 87 | private int version; 88 | 89 | public MockVersion(int version) { 90 | this.version = version; 91 | } 92 | 93 | @Override 94 | public int compareTo(MockVersion o) { 95 | return version - o.version; 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/test/java/com/hazelcast/hibernate/access/ReadWriteAccessDelegateTest.java: -------------------------------------------------------------------------------- 1 | package com.hazelcast.hibernate.access; 2 | 3 | import com.hazelcast.core.HazelcastException; 4 | import com.hazelcast.hibernate.HibernateTestSupport; 5 | import com.hazelcast.hibernate.RegionCache; 6 | import com.hazelcast.hibernate.region.HazelcastRegion; 7 | import com.hazelcast.logging.ILogger; 8 | import com.hazelcast.logging.Logger; 9 | import com.hazelcast.test.HazelcastParallelClassRunner; 10 | import com.hazelcast.test.annotation.QuickTest; 11 | import org.hibernate.cache.spi.access.SoftLock; 12 | import org.junit.Before; 13 | import org.junit.Test; 14 | import org.junit.experimental.categories.Category; 15 | import org.junit.runner.RunWith; 16 | 17 | import static org.junit.Assert.assertFalse; 18 | import static org.mockito.Matchers.any; 19 | import static org.mockito.Mockito.mock; 20 | import static org.mockito.Mockito.when; 21 | 22 | @RunWith(HazelcastParallelClassRunner.class) 23 | @Category(QuickTest.class) 24 | public class ReadWriteAccessDelegateTest extends HibernateTestSupport { 25 | 26 | private RegionCache cache; 27 | private ReadWriteAccessDelegate delegate; 28 | 29 | @Before 30 | public void setUp() { 31 | ILogger log = Logger.getLogger(ReadWriteAccessDelegateTest.class); 32 | 33 | cache = mock(RegionCache.class); 34 | 35 | HazelcastRegion region = mock(HazelcastRegion.class); 36 | when(region.getLogger()).thenReturn(log); 37 | when(region.getCache()).thenReturn(cache); 38 | 39 | delegate = new ReadWriteAccessDelegate(region, null); 40 | } 41 | 42 | @Test 43 | public void testAfterInsert() { 44 | when(cache.insert(any(), any(), any())).thenThrow(new HazelcastException("expected exception")); 45 | assertFalse(delegate.afterInsert(null, null, null)); 46 | } 47 | 48 | @Test 49 | public void testAfterUpdate() { 50 | when(cache.update(any(), any(), any(), any(SoftLock.class))).thenThrow(new HazelcastException("expected exception")); 51 | assertFalse(delegate.afterUpdate(null, null, null, null, null)); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/test/java/com/hazelcast/hibernate/entity/AnnotatedEntity.java: -------------------------------------------------------------------------------- 1 | package com.hazelcast.hibernate.entity; 2 | 3 | import org.hibernate.annotations.GenericGenerator; 4 | import org.hibernate.annotations.NaturalId; 5 | import org.hibernate.annotations.NaturalIdCache; 6 | 7 | import javax.persistence.Entity; 8 | import javax.persistence.GeneratedValue; 9 | import javax.persistence.Id; 10 | import javax.persistence.Table; 11 | 12 | @NaturalIdCache 13 | @Entity 14 | @Table( name = "ANNOTATED_ENTITIES" ) 15 | public class AnnotatedEntity { 16 | private Long id; 17 | 18 | private String title; 19 | 20 | public AnnotatedEntity() { 21 | } 22 | 23 | public AnnotatedEntity(String title) { 24 | this.title = title; 25 | } 26 | 27 | @NaturalId(mutable = true) 28 | public String getTitle() { 29 | return title; 30 | } 31 | 32 | public void setTitle(String title) { 33 | this.title = title; 34 | } 35 | 36 | @Id 37 | @GeneratedValue(generator="increment") 38 | @GenericGenerator(name="increment", strategy = "increment") 39 | public Long getId() { 40 | return id; 41 | } 42 | 43 | private void setId(Long id) { 44 | this.id = id; 45 | } 46 | } -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/test/java/com/hazelcast/hibernate/entity/DummyEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.entity; 17 | 18 | import java.util.Date; 19 | import java.util.Set; 20 | 21 | public class DummyEntity { 22 | 23 | private long id; 24 | 25 | private int version; 26 | 27 | private String name; 28 | 29 | private double value; 30 | 31 | private Date date; 32 | 33 | private Set properties; 34 | 35 | public DummyEntity() { 36 | } 37 | 38 | public DummyEntity(long id, String name, double value, Date date) { 39 | this.id = id; 40 | this.name = name; 41 | this.value = value; 42 | this.date = date; 43 | } 44 | 45 | public long getId() { 46 | return id; 47 | } 48 | 49 | public void setId(long id) { 50 | this.id = id; 51 | } 52 | 53 | public int getVersion() { 54 | return version; 55 | } 56 | 57 | public void setVersion(int version) { 58 | this.version = version; 59 | } 60 | 61 | public String getName() { 62 | return name; 63 | } 64 | 65 | public void setName(String name) { 66 | this.name = name; 67 | } 68 | 69 | public double getValue() { 70 | return value; 71 | } 72 | 73 | public void setValue(double value) { 74 | this.value = value; 75 | } 76 | 77 | public Date getDate() { 78 | return date; 79 | } 80 | 81 | public void setDate(Date date) { 82 | this.date = date; 83 | } 84 | 85 | public void setProperties(Set properties) { 86 | this.properties = properties; 87 | } 88 | 89 | public Set getProperties() { 90 | return properties; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/test/java/com/hazelcast/hibernate/entity/DummyProperty.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.entity; 17 | 18 | public class DummyProperty { 19 | 20 | private long id; 21 | 22 | private int version; 23 | 24 | private String key; 25 | 26 | private DummyEntity entity; 27 | 28 | public DummyProperty() { 29 | } 30 | 31 | public DummyProperty(String key) { 32 | this.key = key; 33 | } 34 | 35 | public DummyProperty(String key, DummyEntity entity) { 36 | this.key = key; 37 | this.entity = entity; 38 | } 39 | 40 | public DummyProperty(long id, String key, DummyEntity entity) { 41 | this.id = id; 42 | this.key = key; 43 | this.entity = entity; 44 | } 45 | 46 | public long getId() { 47 | return id; 48 | } 49 | 50 | public void setId(long id) { 51 | this.id = id; 52 | } 53 | 54 | public int getVersion() { 55 | return version; 56 | } 57 | 58 | public void setVersion(int version) { 59 | this.version = version; 60 | } 61 | 62 | public String getKey() { 63 | return key; 64 | } 65 | 66 | public void setKey(String key) { 67 | this.key = key; 68 | } 69 | 70 | public DummyEntity getEntity() { 71 | return entity; 72 | } 73 | 74 | public void setEntity(DummyEntity entity) { 75 | this.entity = entity; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/test/java/com/hazelcast/hibernate/serialization/HibernateSerializationHookAvailableTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020 Hazelcast Inc. 3 | * 4 | * Licensed under the Hazelcast Community License (the "License"); you may not use 5 | * this file except in compliance with the License. You may obtain a copy of the 6 | * License at 7 | * 8 | * http://hazelcast.com/hazelcast-community-license 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 | * WARRANTIES OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | package com.hazelcast.hibernate.serialization; 17 | 18 | import com.hazelcast.core.Hazelcast; 19 | import com.hazelcast.core.HazelcastInstance; 20 | import com.hazelcast.instance.HazelcastInstanceImpl; 21 | import com.hazelcast.instance.HazelcastInstanceProxy; 22 | import com.hazelcast.internal.serialization.impl.AbstractSerializationService; 23 | import com.hazelcast.spi.serialization.SerializationService; 24 | import com.hazelcast.test.HazelcastSerialClassRunner; 25 | import com.hazelcast.test.annotation.ParallelTest; 26 | import com.hazelcast.test.annotation.QuickTest; 27 | import org.hibernate.cache.spi.CacheKey; 28 | import org.hibernate.cache.spi.entry.CacheEntry; 29 | import org.junit.After; 30 | import org.junit.Test; 31 | import org.junit.experimental.categories.Category; 32 | import org.junit.runner.RunWith; 33 | 34 | import java.lang.reflect.Field; 35 | import java.util.concurrent.ConcurrentMap; 36 | 37 | import static org.junit.Assert.assertTrue; 38 | 39 | @RunWith(HazelcastSerialClassRunner.class) 40 | @Category({QuickTest.class, ParallelTest.class}) 41 | public class HibernateSerializationHookAvailableTest { 42 | 43 | private static final Field ORIGINAL; 44 | private static final Field TYPE_MAP; 45 | 46 | static { 47 | try { 48 | ORIGINAL = HazelcastInstanceProxy.class.getDeclaredField("original"); 49 | ORIGINAL.setAccessible(true); 50 | 51 | TYPE_MAP = AbstractSerializationService.class.getDeclaredField("typeMap"); 52 | TYPE_MAP.setAccessible(true); 53 | } catch (Exception e) { 54 | throw new RuntimeException(e); 55 | } 56 | } 57 | 58 | @After 59 | public void teardown() { 60 | Hazelcast.shutdownAll(); 61 | } 62 | 63 | @Test 64 | public void testAutoregistrationOnHibernate4Available() 65 | throws Exception { 66 | 67 | HazelcastInstance hz = Hazelcast.newHazelcastInstance(); 68 | HazelcastInstanceImpl impl = (HazelcastInstanceImpl) ORIGINAL.get(hz); 69 | SerializationService ss = impl.getSerializationService(); 70 | ConcurrentMap typeMap = (ConcurrentMap) TYPE_MAP.get(ss); 71 | 72 | boolean cacheKeySerializerFound = false; 73 | boolean cacheEntrySerializerFound = false; 74 | for (Class clazz : typeMap.keySet()) { 75 | if (clazz == CacheKey.class) { 76 | cacheKeySerializerFound = true; 77 | } else if (clazz == CacheEntry.class) { 78 | cacheEntrySerializerFound = true; 79 | } 80 | } 81 | 82 | assertTrue("CacheKey serializer not found", cacheKeySerializerFound); 83 | assertTrue("CacheEntry serializer not found", cacheEntrySerializerFound); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/test/resources/com/hazelcast/hibernate/entity/DummyEntity.hbm.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 17 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/test/resources/com/hazelcast/hibernate/entity/DummyProperty.hbm.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 17 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/test/resources/hazelcast-client-custom.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 17 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/test/resources/hazelcast-custom.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 17 | 21 | 22 | dev-custom 23 | dev-pass 24 | 25 | 26 | 5701 27 | 28 | 29 | 224.2.2.3 30 | 54327 31 | 32 | 33 | 127.0.0.1 34 | 35 | 36 | 37 | 10.3.17.* 38 | 39 | 40 | 41 | 46 | 1 47 | 54 | NONE 55 | 61 | 0 62 | 68 | 25 69 | 70 | 71 | 72 | 73 | 0 74 | 50 75 | 76 | 77 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/test/resources/test-hibernate-client.cfg.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 17 | 20 | 21 | 22 | 23 | org.hibernate.dialect.HSQLDialect 24 | org.hsqldb.jdbcDriver 25 | jdbc:hsqldb:mem:testdb 26 | sa 27 | 28 | 29 | 2 30 | false 31 | create-drop 32 | 33 | true 34 | true 35 | true 36 | 37 | true 38 | localhost 39 | dev-custom 40 | dev-pass 41 | true 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /hazelcast-hibernate4/src/test/resources/test-hibernate.cfg.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 17 | 20 | 21 | 22 | 23 | org.hibernate.dialect.HSQLDialect 24 | org.hsqldb.jdbcDriver 25 | jdbc:hsqldb:mem:testdb 26 | sa 27 | 28 | 29 | 2 30 | false 31 | create-drop 32 | 33 | true 34 | true 35 | true 36 | 37 | hazelcast-custom.xml 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /images/HZLocalCacheRgnFactory.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hazelcast/hazelcast-hibernate-3-and-4/d19e12b0d81f88a8059b90cccf7d094a0ecfaa5e/images/HZLocalCacheRgnFactory.jpg -------------------------------------------------------------------------------- /images/NoteSmall.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hazelcast/hazelcast-hibernate-3-and-4/d19e12b0d81f88a8059b90cccf7d094a0ecfaa5e/images/NoteSmall.jpg --------------------------------------------------------------------------------