Apache-Ignite integration as Hibernate 2nd level cache not starting?

廉价感情. 提交于 2019-12-02 03:22:56

You have to configure caches for all your regions. For example, for userType you can add this to the configuration:

<bean parent="transactional-cache">
    <property name="name" value="userType"/>
</bean>
Rajasekaran Kandhasamy

@Cache annotation will be executed only if you use the entityManager.find() method. That mean this will work only with primary keys. Another approach is to use query cache or ignite API.

Till now, annotations not supported in ignite-cache (as l2 cache for hibernate).

If you don't want to put configuration of each entity in cache configuration, follow below steps

Step 1. Create a new class HibernateRegionFactoryForIgnite as below

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.cache.CacheException;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.CacheAtomicityMode;
import org.apache.ignite.cache.CacheMode;
import org.apache.ignite.cache.CacheWriteSynchronizationMode;
import org.apache.ignite.cache.hibernate.HibernateCollectionRegionForIgnite;
import org.apache.ignite.cache.hibernate.HibernateEntityRegionForIgnite;
import org.apache.ignite.cache.hibernate.HibernateRegionFactory;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.internal.IgniteKernal;
import org.apache.ignite.internal.processors.cache.IgniteInternalCache;
import org.apache.log4j.Logger;
import org.hibernate.boot.spi.SessionFactoryOptions;
import org.hibernate.cache.spi.CacheDataDescription;
import org.hibernate.cache.spi.CollectionRegion;
import org.hibernate.cache.spi.EntityRegion;
import org.hibernate.cache.spi.TimestampsRegion;
import org.hibernate.cfg.Settings;

@SuppressWarnings("deprecation")
public class HibernateRegionFactoryForIgnite extends HibernateRegionFactory {

    private static final long serialVersionUID = 530290669748711933L;

    public static Logger logger =     Logger.getLogger(HibernateRegionFactoryForIgnite.class);

    private IgniteKernal ignite;
    private IgniteInternalCache<Object, Object> dfltCache;
    private final Map<String, String> regionCaches = new HashMap<>();

    @Override
    public void start(SessionFactoryOptions settings, Properties properties) throws CacheException {
        start(new Settings(settings), properties);
        initializeVariables(properties);
    }

    public void initializeVariables(Properties props) {
        ignite = (IgniteKernal) Ignition.ignite("hibernate-grid");
        String dfltCacheName = props.getProperty(DFLT_CACHE_NAME_PROPERTY);

        for (Map.Entry<Object, Object> prop : props.entrySet()) {
            String key = prop.getKey().toString();
            if (key.startsWith(REGION_CACHE_PROPERTY)) {
                String regionName = key.substring(REGION_CACHE_PROPERTY.length());
                String cacheName = prop.getValue().toString();
                regionCaches.put(regionName, cacheName);
            }
        }

        if (dfltCacheName != null) {
            dfltCache = ((IgniteKernal) ignite).getCache(dfltCacheName);
        }
    }

    private IgniteInternalCache<Object, Object> regionCache(String regionName) throws CacheException {
        String cacheName = regionCaches.get(regionName);
        if (cacheName == null) {
            if (dfltCache != null)
                return dfltCache;
            cacheName = regionName;
        }
        IgniteInternalCache<Object, Object> cache = ((IgniteKernal) ignite).getCache(cacheName);
        if (cache == null)
            throw new CacheException("Cache '" + cacheName + "' for region '" + regionName + "' is not configured.");
        return cache;
    }

    @Override
    public EntityRegion buildEntityRegion(String regionName, Properties props, CacheDataDescription metadata)
            throws CacheException {
        EntityRegion entityRegion = null;
        try {
            entityRegion = new HibernateEntityRegion(this, regionName, ignite, regionCache(regionName),
                metadata);
        } catch (Exception e) {
        }
        if (entityRegion == null) {
            ignite.createCache(cacheConfiguration(regionName));
            try {
                entityRegion = new HibernateEntityRegion(this, regionName, ignite, regionCache(regionName),
                    metadata);
            } catch (Exception e) {
                logger.debug("exception occurred");
            }
        }
        return entityRegion;
    }

    @Override
    public CollectionRegion buildCollectionRegion(String regionName, Properties props, CacheDataDescription metadata)
        throws CacheException {
        CollectionRegion collectionRegion = null;
        try {
            collectionRegion = new HibernateCollectionRegion(this, regionName, ignite, regionCache(regionName),
                metadata);
        } catch (Exception e) {
        }
        if (collectionRegion == null) {
            ignite.createCache(cacheConfiguration(regionName));
            try {
                collectionRegion = new HibernateCollectionRegion(this, regionName, ignite,
                    regionCache(regionName), metadata);
            } catch (Exception e) {
                logger.debug("exception occurred");
            }
        }
        return collectionRegion;
    }

    private CacheConfiguration<Object, Object> cacheConfiguration(String cacheName) {
        CacheConfiguration<Object, Object> cfg = new CacheConfiguration<Object, Object>();
        cfg.setName(cacheName);
        cfg.setCacheMode(CacheMode.PARTITIONED);
        cfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
        cfg.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC);

        return cfg;
    }
}

Step 2. Use class HibernateRegionFactoryForIgnite as value for key hibernate.cache.region.factory_class in hibernate configuration properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!