[ 
https://issues.apache.org/jira/browse/IGNITE-3603?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15540398#comment-15540398
 ] 

Anil Agrawal commented on IGNITE-3603:
--------------------------------------

As a workaround, extend class HibernateRegionFactory and override function 
buildEntityRegion() as below

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;
        }
}


and use class HibernateRegionFactoryForIgnite as value of key 
hibernate.cache.region.factory_class in hibernate configuration properties

> Hibernate L2 cache should be able to create caches automatically
> ----------------------------------------------------------------
>
>                 Key: IGNITE-3603
>                 URL: https://issues.apache.org/jira/browse/IGNITE-3603
>             Project: Ignite
>          Issue Type: Improvement
>          Components: Hibernate L2 cache
>    Affects Versions: 1.6
>            Reporter: Valentin Kulichenko
>             Fix For: 1.8
>
>
> Hibernate L2 cache is typically configured using annotations, like this:
> {code}
> @Cache(usage = CacheConcurrencyStrategy.READ_ONLY, region = "userType")
> {code}
> Currently we require user to explicitly configure {{userType}} cache, but we 
> can avoid this and create caches automatically.
> Atomicity mode should be chosen based on concurrency strategy, all other 
> settings should be default.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to