Package org.hibernate.cache.spi

Examples of org.hibernate.cache.spi.RegionFactory


    DEFAULT_CACHE_CONCURRENCY_STRATEGY = strategy;
  }

  private static CacheConcurrencyStrategy determineCacheConcurrencyStrategy(Mappings mappings) {
    if ( DEFAULT_CACHE_CONCURRENCY_STRATEGY == null ) {
      final RegionFactory cacheRegionFactory = SettingsFactory.createRegionFactory(
          mappings.getConfigurationProperties(), true
      );
      DEFAULT_CACHE_CONCURRENCY_STRATEGY = CacheConcurrencyStrategy.fromAccessType( cacheRegionFactory.getDefaultAccessType() );
    }
    return DEFAULT_CACHE_CONCURRENCY_STRATEGY;
  }
View Full Code Here


    ///////////////////////////////////////////////////////////////////////
    // Prepare persisters and link them up with their cache
    // region/access-strategy

    final RegionFactory regionFactory = cacheAccess.getRegionFactory();
    final String cacheRegionPrefix = settings.getCacheRegionPrefix() == null ? "" : settings.getCacheRegionPrefix() + ".";
    final PersisterFactory persisterFactory = serviceRegistry.getService( PersisterFactory.class );

    // todo : consider removing this silliness and just have EntityPersister directly implement ClassMetadata
    //    EntityPersister.getClassMetadata() for the internal impls simply "return this";
    //    collapsing those would allow us to remove this "extra" Map
    //
    // todo : similar for CollectionPersister/CollectionMetadata

    entityPersisters = new HashMap();
    Map entityAccessStrategies = new HashMap();
    Map<String,ClassMetadata> classMeta = new HashMap<String,ClassMetadata>();
    classes = cfg.getClassMappings();
    while ( classes.hasNext() ) {
      final PersistentClass model = (PersistentClass) classes.next();
      model.prepareTemporaryTables( mapping, getDialect() );
      final String cacheRegionName = cacheRegionPrefix + model.getRootClass().getCacheRegionName();
      // cache region is defined by the root-class in the hierarchy...
      EntityRegionAccessStrategy accessStrategy = ( EntityRegionAccessStrategy ) entityAccessStrategies.get( cacheRegionName );
      if ( accessStrategy == null && settings.isSecondLevelCacheEnabled() ) {
        final AccessType accessType = AccessType.fromExternalName( model.getCacheConcurrencyStrategy() );
        if ( accessType != null ) {
          LOG.tracef( "Building shared cache region for entity data [%s]", model.getEntityName() );
          EntityRegion entityRegion = regionFactory.buildEntityRegion( cacheRegionName, properties, CacheDataDescriptionImpl.decode( model ) );
          accessStrategy = entityRegion.buildAccessStrategy( accessType );
          entityAccessStrategies.put( cacheRegionName, accessStrategy );
          cacheAccess.addCacheRegion( cacheRegionName, entityRegion );
        }
      }

      NaturalIdRegionAccessStrategy naturalIdAccessStrategy = null;
      if ( model.hasNaturalId() && model.getNaturalIdCacheRegionName() != null ) {
        final String naturalIdCacheRegionName = cacheRegionPrefix + model.getNaturalIdCacheRegionName();
        naturalIdAccessStrategy = ( NaturalIdRegionAccessStrategy ) entityAccessStrategies.get( naturalIdCacheRegionName );

        if ( naturalIdAccessStrategy == null && settings.isSecondLevelCacheEnabled() ) {
          final CacheDataDescriptionImpl cacheDataDescription = CacheDataDescriptionImpl.decode( model );

          NaturalIdRegion naturalIdRegion = null;
          try {
            naturalIdRegion = regionFactory.buildNaturalIdRegion( naturalIdCacheRegionName, properties,
                cacheDataDescription );
          }
          catch ( UnsupportedOperationException e ) {
            LOG.warnf(
                "Shared cache region factory [%s] does not support natural id caching; " +
                    "shared NaturalId caching will be disabled for not be enabled for %s",
                regionFactory.getClass().getName(),
                model.getEntityName()
            );
          }

          if (naturalIdRegion != null) {
            naturalIdAccessStrategy = naturalIdRegion.buildAccessStrategy( regionFactory.getDefaultAccessType() );
            entityAccessStrategies.put( naturalIdCacheRegionName, naturalIdAccessStrategy );
            cacheAccess.addCacheRegionnaturalIdCacheRegionName, naturalIdRegion );
          }
        }
      }

      EntityPersister cp = persisterFactory.createEntityPersister(
          model,
          accessStrategy,
          naturalIdAccessStrategy,
          this,
          mapping
      );
      entityPersisters.put( model.getEntityName(), cp );
      classMeta.put( model.getEntityName(), cp.getClassMetadata() );
    }
    this.classMetadata = Collections.unmodifiableMap(classMeta);

    Map<String,Set<String>> tmpEntityToCollectionRoleMap = new HashMap<String,Set<String>>();
    collectionPersisters = new HashMap<String,CollectionPersister>();
    Map<String,CollectionMetadata> tmpCollectionMetadata = new HashMap<String,CollectionMetadata>();
    Iterator collections = cfg.getCollectionMappings();
    while ( collections.hasNext() ) {
      Collection model = (Collection) collections.next();
      final String cacheRegionName = cacheRegionPrefix + model.getCacheRegionName();
      final AccessType accessType = AccessType.fromExternalName( model.getCacheConcurrencyStrategy() );
      CollectionRegionAccessStrategy accessStrategy = null;
      if ( accessType != null && settings.isSecondLevelCacheEnabled() ) {
        LOG.tracev( "Building shared cache region for collection data [{0}]", model.getRole() );
        CollectionRegion collectionRegion = regionFactory.buildCollectionRegion( cacheRegionName, properties, CacheDataDescriptionImpl
            .decode( model ) );
        accessStrategy = collectionRegion.buildAccessStrategy( accessType );
        entityAccessStrategies.put( cacheRegionName, accessStrategy );
        cacheAccess.addCacheRegion( cacheRegionName, collectionRegion );
      }
View Full Code Here

    final boolean useQueryCache = ConfigurationHelper.getBoolean(
        AvailableSettings.USE_QUERY_CACHE,
        configurationValues
    );

    RegionFactory regionFactory = NoCachingRegionFactory.INSTANCE;

    // The cache provider is needed when we either have second-level cache enabled
    // or query cache enabled.  Note that useSecondLevelCache is enabled by default
    final String setting = ConfigurationHelper.getString( AvailableSettings.CACHE_REGION_FACTORY,
        configurationValues, null );
    if ( ( useSecondLevelCache || useQueryCache ) && setting != null ) {
      try {
        final Class<? extends RegionFactory> regionFactoryClass = registry.getService( StrategySelector.class )
            .selectStrategyImplementor( RegionFactory.class, setting );
        try {
          regionFactory = regionFactoryClass.getConstructor( Properties.class ).newInstance( p );
        }
        catch ( NoSuchMethodException e ) {
          // no constructor accepting Properties found, try no arg constructor
          LOG.debugf(
              "%s did not provide constructor accepting java.util.Properties; attempting no-arg constructor.",
              regionFactoryClass.getSimpleName() );
          regionFactory = regionFactoryClass.getConstructor().newInstance();
        }
      }
      catch ( Exception e ) {
        throw new HibernateException( "could not instantiate RegionFactory [" + setting + "]", e );
      }
    }

    LOG.debugf( "Cache region factory : %s", regionFactory.getClass().getName() );

    return regionFactory;
  }
View Full Code Here

    DEFAULT_CACHE_CONCURRENCY_STRATEGY = strategy;
  }

  private static CacheConcurrencyStrategy determineCacheConcurrencyStrategy(Mappings mappings) {
    if ( DEFAULT_CACHE_CONCURRENCY_STRATEGY == null ) {
      final RegionFactory cacheRegionFactory = SettingsFactory.createRegionFactory(
          mappings.getConfigurationProperties(), true
      );
      DEFAULT_CACHE_CONCURRENCY_STRATEGY = CacheConcurrencyStrategy.fromAccessType( cacheRegionFactory.getDefaultAccessType() );
    }
    return DEFAULT_CACHE_CONCURRENCY_STRATEGY;
  }
View Full Code Here

    DEFAULT_CACHE_CONCURRENCY_STRATEGY = strategy;
  }

  private static CacheConcurrencyStrategy determineCacheConcurrencyStrategy(Mappings mappings) {
    if ( DEFAULT_CACHE_CONCURRENCY_STRATEGY == null ) {
      final RegionFactory cacheRegionFactory = SettingsFactory.createRegionFactory(
          mappings.getConfigurationProperties(), true
      );
      DEFAULT_CACHE_CONCURRENCY_STRATEGY = CacheConcurrencyStrategy.fromAccessType( cacheRegionFactory.getDefaultAccessType() );
    }
    return DEFAULT_CACHE_CONCURRENCY_STRATEGY;
  }
View Full Code Here

    LOG.debugf( "Session factory constructed with filter configurations : %s", filters );
    LOG.debugf( "Instantiating session factory with properties: %s", properties );

    // Caches
    final RegionFactory regionFactory = settings.getRegionFactory();
    regionFactory.start( settings, properties );
    this.queryPlanCache = new QueryPlanCache( this );

    // todo : everything above here consider implementing as standard SF service.  specifically: stats, caches, types, function-reg

    class IntegratorObserver implements SessionFactoryObserver {
      private ArrayList<Integrator> integrators = new ArrayList<Integrator>();

      @Override
      public void sessionFactoryCreated(SessionFactory factory) {
      }

      @Override
      public void sessionFactoryClosed(SessionFactory factory) {
        for ( Integrator integrator : integrators ) {
          integrator.disintegrate( SessionFactoryImpl.this, SessionFactoryImpl.this.serviceRegistry );
        }
      }
    }

    final IntegratorObserver integratorObserver = new IntegratorObserver();
    this.observer.addObserver( integratorObserver );
    for ( Integrator integrator : serviceRegistry.getService( IntegratorService.class ).getIntegrators() ) {
      integrator.integrate( cfg, this, this.serviceRegistry );
      integratorObserver.integrators.add( integrator );
    }

    //Generators:

    identifierGenerators = new HashMap();
    Iterator classes = cfg.getClassMappings();
    while ( classes.hasNext() ) {
      PersistentClass model = (PersistentClass) classes.next();
      if ( !model.isInherited() ) {
        IdentifierGenerator generator = model.getIdentifier().createIdentifierGenerator(
            cfg.getIdentifierGeneratorFactory(),
            getDialect(),
                settings.getDefaultCatalogName(),
                settings.getDefaultSchemaName(),
                (RootClass) model
        );
        identifierGenerators.put( model.getEntityName(), generator );
      }
    }


    ///////////////////////////////////////////////////////////////////////
    // Prepare persisters and link them up with their cache
    // region/access-strategy

    final String cacheRegionPrefix = settings.getCacheRegionPrefix() == null ? "" : settings.getCacheRegionPrefix() + ".";

    entityPersisters = new HashMap();
    Map entityAccessStrategies = new HashMap();
    Map<String,ClassMetadata> classMeta = new HashMap<String,ClassMetadata>();
    classes = cfg.getClassMappings();
    while ( classes.hasNext() ) {
      final PersistentClass model = (PersistentClass) classes.next();
      model.prepareTemporaryTables( mapping, getDialect() );
      final String cacheRegionName = cacheRegionPrefix + model.getRootClass().getCacheRegionName();
      // cache region is defined by the root-class in the hierarchy...
      EntityRegionAccessStrategy accessStrategy = ( EntityRegionAccessStrategy ) entityAccessStrategies.get( cacheRegionName );
      if ( accessStrategy == null && settings.isSecondLevelCacheEnabled() ) {
        final AccessType accessType = AccessType.fromExternalName( model.getCacheConcurrencyStrategy() );
        if ( accessType != null ) {
          LOG.tracef( "Building shared cache region for entity data [%s]", model.getEntityName() );
          EntityRegion entityRegion = regionFactory.buildEntityRegion( cacheRegionName, properties, CacheDataDescriptionImpl.decode( model ) );
          accessStrategy = entityRegion.buildAccessStrategy( accessType );
          entityAccessStrategies.put( cacheRegionName, accessStrategy );
          allCacheRegions.put( cacheRegionName, entityRegion );
        }
      }
     
      NaturalIdRegionAccessStrategy naturalIdAccessStrategy = null;
      if ( model.hasNaturalId() && model.getNaturalIdCacheRegionName() != null ) {
        final String naturalIdCacheRegionName = cacheRegionPrefix + model.getNaturalIdCacheRegionName();
        naturalIdAccessStrategy = ( NaturalIdRegionAccessStrategy ) entityAccessStrategies.get( naturalIdCacheRegionName );
       
        if ( naturalIdAccessStrategy == null && settings.isSecondLevelCacheEnabled() ) {
          final CacheDataDescriptionImpl cacheDataDescription = CacheDataDescriptionImpl.decode( model );
         
          NaturalIdRegion naturalIdRegion = null;
          try {
            naturalIdRegion = regionFactory.buildNaturalIdRegion( naturalIdCacheRegionName, properties,
                cacheDataDescription );
          }
          catch ( UnsupportedOperationException e ) {
            LOG.warnf(
                "Shared cache region factory [%s] does not support natural id caching; " +
                    "shared NaturalId caching will be disabled for not be enabled for %s",
                regionFactory.getClass().getName(),
                model.getEntityName()
            );
          }
         
          if (naturalIdRegion != null) {
            naturalIdAccessStrategy = naturalIdRegion.buildAccessStrategy( regionFactory.getDefaultAccessType() );
            entityAccessStrategies.put( naturalIdCacheRegionName, naturalIdAccessStrategy );
            allCacheRegions.put( naturalIdCacheRegionName, naturalIdRegion );
          }
        }
      }
     
      EntityPersister cp = serviceRegistry.getService( PersisterFactory.class ).createEntityPersister(
          model,
          accessStrategy,
          naturalIdAccessStrategy,
          this,
          mapping
      );
      entityPersisters.put( model.getEntityName(), cp );
      classMeta.put( model.getEntityName(), cp.getClassMetadata() );
    }
    this.classMetadata = Collections.unmodifiableMap(classMeta);

    Map<String,Set<String>> tmpEntityToCollectionRoleMap = new HashMap<String,Set<String>>();
    collectionPersisters = new HashMap<String,CollectionPersister>();
    Map<String,CollectionMetadata> tmpCollectionMetadata = new HashMap<String,CollectionMetadata>();
    Iterator collections = cfg.getCollectionMappings();
    while ( collections.hasNext() ) {
      Collection model = (Collection) collections.next();
      final String cacheRegionName = cacheRegionPrefix + model.getCacheRegionName();
      final AccessType accessType = AccessType.fromExternalName( model.getCacheConcurrencyStrategy() );
      CollectionRegionAccessStrategy accessStrategy = null;
      if ( accessType != null && settings.isSecondLevelCacheEnabled() ) {
        LOG.tracev( "Building shared cache region for collection data [{0}]", model.getRole() );
        CollectionRegion collectionRegion = regionFactory.buildCollectionRegion( cacheRegionName, properties, CacheDataDescriptionImpl
            .decode( model ) );
        accessStrategy = collectionRegion.buildAccessStrategy( accessType );
        entityAccessStrategies.put( cacheRegionName, accessStrategy );
        allCacheRegions.put( cacheRegionName, collectionRegion );
      }
View Full Code Here

    DEFAULT_CACHE_CONCURRENCY_STRATEGY = strategy;
  }

  private static CacheConcurrencyStrategy determineCacheConcurrencyStrategy(Mappings mappings) {
    if ( DEFAULT_CACHE_CONCURRENCY_STRATEGY == null ) {
      final RegionFactory cacheRegionFactory = SettingsFactory.createRegionFactory(
          mappings.getConfigurationProperties(), true
      );
      DEFAULT_CACHE_CONCURRENCY_STRATEGY = CacheConcurrencyStrategy.fromAccessType( cacheRegionFactory.getDefaultAccessType() );
    }
    return DEFAULT_CACHE_CONCURRENCY_STRATEGY;
  }
View Full Code Here

        cfg
    );
        this.jdbcServices = this.serviceRegistry.getService( JdbcServices.class );
        this.dialect = this.jdbcServices.getDialect();
    this.cacheAccess = this.serviceRegistry.getService( CacheImplementor.class );
    final RegionFactory regionFactory = cacheAccess.getRegionFactory();
    this.sqlFunctionRegistry = new SQLFunctionRegistry( getDialect(), cfg.getSqlFunctions() );
    if ( observer != null ) {
      this.observer.addObserver( observer );
    }

    this.typeResolver = cfg.getTypeResolver().scope( this );
    this.typeHelper = new TypeLocatorImpl( typeResolver );

    this.filters = new HashMap<String, FilterDefinition>();
    this.filters.putAll( cfg.getFilterDefinitions() );

    LOG.debugf( "Session factory constructed with filter configurations : %s", filters );
    LOG.debugf( "Instantiating session factory with properties: %s", properties );


    this.queryPlanCache = new QueryPlanCache( this );

    // todo : everything above here consider implementing as standard SF service.  specifically: stats, caches, types, function-reg

    class IntegratorObserver implements SessionFactoryObserver {
      private ArrayList<Integrator> integrators = new ArrayList<Integrator>();

      @Override
      public void sessionFactoryCreated(SessionFactory factory) {
      }

      @Override
      public void sessionFactoryClosed(SessionFactory factory) {
        for ( Integrator integrator : integrators ) {
          integrator.disintegrate( SessionFactoryImpl.this, SessionFactoryImpl.this.serviceRegistry );
        }
                integrators.clear();
      }
    }

    final IntegratorObserver integratorObserver = new IntegratorObserver();
    this.observer.addObserver( integratorObserver );
    for ( Integrator integrator : serviceRegistry.getService( IntegratorService.class ).getIntegrators() ) {
      integrator.integrate( cfg, this, this.serviceRegistry );
      integratorObserver.integrators.add( integrator );
    }

    //Generators:

    identifierGenerators = new HashMap();
    Iterator classes = cfg.getClassMappings();
    while ( classes.hasNext() ) {
      PersistentClass model = (PersistentClass) classes.next();
      if ( !model.isInherited() ) {
        IdentifierGenerator generator = model.getIdentifier().createIdentifierGenerator(
            cfg.getIdentifierGeneratorFactory(),
            getDialect(),
                settings.getDefaultCatalogName(),
                settings.getDefaultSchemaName(),
                (RootClass) model
        );
        identifierGenerators.put( model.getEntityName(), generator );
      }
    }


    ///////////////////////////////////////////////////////////////////////
    // Prepare persisters and link them up with their cache
    // region/access-strategy

    final String cacheRegionPrefix = settings.getCacheRegionPrefix() == null ? "" : settings.getCacheRegionPrefix() + ".";

    final PersisterFactory persisterFactory = serviceRegistry.getService( PersisterFactory.class );

    entityPersisters = new HashMap();
    Map entityAccessStrategies = new HashMap();
    Map<String,ClassMetadata> classMeta = new HashMap<String,ClassMetadata>();
    classes = cfg.getClassMappings();
    while ( classes.hasNext() ) {
      final PersistentClass model = (PersistentClass) classes.next();
      model.prepareTemporaryTables( mapping, getDialect() );
      final String cacheRegionName = cacheRegionPrefix + model.getRootClass().getCacheRegionName();
      // cache region is defined by the root-class in the hierarchy...
      EntityRegionAccessStrategy accessStrategy = ( EntityRegionAccessStrategy ) entityAccessStrategies.get( cacheRegionName );
      if ( accessStrategy == null && settings.isSecondLevelCacheEnabled() ) {
        final AccessType accessType = AccessType.fromExternalName( model.getCacheConcurrencyStrategy() );
        if ( accessType != null ) {
          LOG.tracef( "Building shared cache region for entity data [%s]", model.getEntityName() );
          EntityRegion entityRegion = regionFactory.buildEntityRegion( cacheRegionName, properties, CacheDataDescriptionImpl.decode( model ) );
          accessStrategy = entityRegion.buildAccessStrategy( accessType );
          entityAccessStrategies.put( cacheRegionName, accessStrategy );
          cacheAccess.addCacheRegion( cacheRegionName, entityRegion );
        }
      }
     
      NaturalIdRegionAccessStrategy naturalIdAccessStrategy = null;
      if ( model.hasNaturalId() && model.getNaturalIdCacheRegionName() != null ) {
        final String naturalIdCacheRegionName = cacheRegionPrefix + model.getNaturalIdCacheRegionName();
        naturalIdAccessStrategy = ( NaturalIdRegionAccessStrategy ) entityAccessStrategies.get( naturalIdCacheRegionName );
       
        if ( naturalIdAccessStrategy == null && settings.isSecondLevelCacheEnabled() ) {
          final CacheDataDescriptionImpl cacheDataDescription = CacheDataDescriptionImpl.decode( model );
         
          NaturalIdRegion naturalIdRegion = null;
          try {
            naturalIdRegion = regionFactory.buildNaturalIdRegion( naturalIdCacheRegionName, properties,
                cacheDataDescription );
          }
          catch ( UnsupportedOperationException e ) {
            LOG.warnf(
                "Shared cache region factory [%s] does not support natural id caching; " +
                    "shared NaturalId caching will be disabled for not be enabled for %s",
                regionFactory.getClass().getName(),
                model.getEntityName()
            );
          }
         
          if (naturalIdRegion != null) {
            naturalIdAccessStrategy = naturalIdRegion.buildAccessStrategy( regionFactory.getDefaultAccessType() );
            entityAccessStrategies.put( naturalIdCacheRegionName, naturalIdAccessStrategy );
            cacheAccess.addCacheRegionnaturalIdCacheRegionName, naturalIdRegion );
          }
        }
      }
     
      EntityPersister cp = persisterFactory.createEntityPersister(
          model,
          accessStrategy,
          naturalIdAccessStrategy,
          this,
          mapping
      );
      entityPersisters.put( model.getEntityName(), cp );
      classMeta.put( model.getEntityName(), cp.getClassMetadata() );
    }
    this.classMetadata = Collections.unmodifiableMap(classMeta);

    Map<String,Set<String>> tmpEntityToCollectionRoleMap = new HashMap<String,Set<String>>();
    collectionPersisters = new HashMap<String,CollectionPersister>();
    Map<String,CollectionMetadata> tmpCollectionMetadata = new HashMap<String,CollectionMetadata>();
    Iterator collections = cfg.getCollectionMappings();
    while ( collections.hasNext() ) {
      Collection model = (Collection) collections.next();
      final String cacheRegionName = cacheRegionPrefix + model.getCacheRegionName();
      final AccessType accessType = AccessType.fromExternalName( model.getCacheConcurrencyStrategy() );
      CollectionRegionAccessStrategy accessStrategy = null;
      if ( accessType != null && settings.isSecondLevelCacheEnabled() ) {
        LOG.tracev( "Building shared cache region for collection data [{0}]", model.getRole() );
        CollectionRegion collectionRegion = regionFactory.buildCollectionRegion( cacheRegionName, properties, CacheDataDescriptionImpl
            .decode( model ) );
        accessStrategy = collectionRegion.buildAccessStrategy( accessType );
        entityAccessStrategies.put( cacheRegionName, accessStrategy );
        cacheAccess.addCacheRegion( cacheRegionName, collectionRegion );
      }
View Full Code Here

        cfg
    );
        this.jdbcServices = this.serviceRegistry.getService( JdbcServices.class );
        this.dialect = this.jdbcServices.getDialect();
    this.cacheAccess = this.serviceRegistry.getService( CacheImplementor.class );
    final RegionFactory regionFactory = cacheAccess.getRegionFactory();
    this.sqlFunctionRegistry = new SQLFunctionRegistry( getDialect(), cfg.getSqlFunctions() );
    if ( observer != null ) {
      this.observer.addObserver( observer );
    }

    this.typeResolver = cfg.getTypeResolver().scope( this );
    this.typeHelper = new TypeLocatorImpl( typeResolver );

    this.filters = new HashMap<String, FilterDefinition>();
    this.filters.putAll( cfg.getFilterDefinitions() );

    LOG.debugf( "Session factory constructed with filter configurations : %s", filters );
    LOG.debugf( "Instantiating session factory with properties: %s", properties );


    this.queryPlanCache = new QueryPlanCache( this );

    // todo : everything above here consider implementing as standard SF service.  specifically: stats, caches, types, function-reg

    class IntegratorObserver implements SessionFactoryObserver {
      private ArrayList<Integrator> integrators = new ArrayList<Integrator>();

      @Override
      public void sessionFactoryCreated(SessionFactory factory) {
      }

      @Override
      public void sessionFactoryClosed(SessionFactory factory) {
        for ( Integrator integrator : integrators ) {
          integrator.disintegrate( SessionFactoryImpl.this, SessionFactoryImpl.this.serviceRegistry );
        }
                integrators.clear();
      }
    }

    final IntegratorObserver integratorObserver = new IntegratorObserver();
    this.observer.addObserver( integratorObserver );
    for ( Integrator integrator : serviceRegistry.getService( IntegratorService.class ).getIntegrators() ) {
      integrator.integrate( cfg, this, this.serviceRegistry );
      integratorObserver.integrators.add( integrator );
    }

    //Generators:

    identifierGenerators = new HashMap();
    Iterator classes = cfg.getClassMappings();
    while ( classes.hasNext() ) {
      PersistentClass model = (PersistentClass) classes.next();
      if ( !model.isInherited() ) {
        IdentifierGenerator generator = model.getIdentifier().createIdentifierGenerator(
            cfg.getIdentifierGeneratorFactory(),
            getDialect(),
                settings.getDefaultCatalogName(),
                settings.getDefaultSchemaName(),
                (RootClass) model
        );
        identifierGenerators.put( model.getEntityName(), generator );
      }
    }


    ///////////////////////////////////////////////////////////////////////
    // Prepare persisters and link them up with their cache
    // region/access-strategy

    final String cacheRegionPrefix = settings.getCacheRegionPrefix() == null ? "" : settings.getCacheRegionPrefix() + ".";

    final PersisterFactory persisterFactory = serviceRegistry.getService( PersisterFactory.class );

    entityPersisters = new HashMap();
    Map entityAccessStrategies = new HashMap();
    Map<String,ClassMetadata> classMeta = new HashMap<String,ClassMetadata>();
    classes = cfg.getClassMappings();
    while ( classes.hasNext() ) {
      final PersistentClass model = (PersistentClass) classes.next();
      model.prepareTemporaryTables( mapping, getDialect() );
      final String cacheRegionName = cacheRegionPrefix + model.getRootClass().getCacheRegionName();
      // cache region is defined by the root-class in the hierarchy...
      EntityRegionAccessStrategy accessStrategy = ( EntityRegionAccessStrategy ) entityAccessStrategies.get( cacheRegionName );
      if ( accessStrategy == null && settings.isSecondLevelCacheEnabled() ) {
        final AccessType accessType = AccessType.fromExternalName( model.getCacheConcurrencyStrategy() );
        if ( accessType != null ) {
          LOG.tracef( "Building shared cache region for entity data [%s]", model.getEntityName() );
          EntityRegion entityRegion = regionFactory.buildEntityRegion( cacheRegionName, properties, CacheDataDescriptionImpl.decode( model ) );
          accessStrategy = entityRegion.buildAccessStrategy( accessType );
          entityAccessStrategies.put( cacheRegionName, accessStrategy );
          cacheAccess.addCacheRegion( cacheRegionName, entityRegion );
        }
      }
     
      NaturalIdRegionAccessStrategy naturalIdAccessStrategy = null;
      if ( model.hasNaturalId() && model.getNaturalIdCacheRegionName() != null ) {
        final String naturalIdCacheRegionName = cacheRegionPrefix + model.getNaturalIdCacheRegionName();
        naturalIdAccessStrategy = ( NaturalIdRegionAccessStrategy ) entityAccessStrategies.get( naturalIdCacheRegionName );
       
        if ( naturalIdAccessStrategy == null && settings.isSecondLevelCacheEnabled() ) {
          final CacheDataDescriptionImpl cacheDataDescription = CacheDataDescriptionImpl.decode( model );
         
          NaturalIdRegion naturalIdRegion = null;
          try {
            naturalIdRegion = regionFactory.buildNaturalIdRegion( naturalIdCacheRegionName, properties,
                cacheDataDescription );
          }
          catch ( UnsupportedOperationException e ) {
            LOG.warnf(
                "Shared cache region factory [%s] does not support natural id caching; " +
                    "shared NaturalId caching will be disabled for not be enabled for %s",
                regionFactory.getClass().getName(),
                model.getEntityName()
            );
          }
         
          if (naturalIdRegion != null) {
            naturalIdAccessStrategy = naturalIdRegion.buildAccessStrategy( regionFactory.getDefaultAccessType() );
            entityAccessStrategies.put( naturalIdCacheRegionName, naturalIdAccessStrategy );
            cacheAccess.addCacheRegionnaturalIdCacheRegionName, naturalIdRegion );
          }
        }
      }
     
      EntityPersister cp = persisterFactory.createEntityPersister(
          model,
          accessStrategy,
          naturalIdAccessStrategy,
          this,
          mapping
      );
      entityPersisters.put( model.getEntityName(), cp );
      classMeta.put( model.getEntityName(), cp.getClassMetadata() );
    }
    this.classMetadata = Collections.unmodifiableMap(classMeta);

    Map<String,Set<String>> tmpEntityToCollectionRoleMap = new HashMap<String,Set<String>>();
    collectionPersisters = new HashMap<String,CollectionPersister>();
    Map<String,CollectionMetadata> tmpCollectionMetadata = new HashMap<String,CollectionMetadata>();
    Iterator collections = cfg.getCollectionMappings();
    while ( collections.hasNext() ) {
      Collection model = (Collection) collections.next();
      final String cacheRegionName = cacheRegionPrefix + model.getCacheRegionName();
      final AccessType accessType = AccessType.fromExternalName( model.getCacheConcurrencyStrategy() );
      CollectionRegionAccessStrategy accessStrategy = null;
      if ( accessType != null && settings.isSecondLevelCacheEnabled() ) {
        LOG.tracev( "Building shared cache region for collection data [{0}]", model.getRole() );
        CollectionRegion collectionRegion = regionFactory.buildCollectionRegion( cacheRegionName, properties, CacheDataDescriptionImpl
            .decode( model ) );
        accessStrategy = collectionRegion.buildAccessStrategy( accessType );
        entityAccessStrategies.put( cacheRegionName, accessStrategy );
        cacheAccess.addCacheRegion( cacheRegionName, collectionRegion );
      }
View Full Code Here

    DEFAULT_CACHE_CONCURRENCY_STRATEGY = strategy;
  }

  private static CacheConcurrencyStrategy determineCacheConcurrencyStrategy(Mappings mappings) {
    if ( DEFAULT_CACHE_CONCURRENCY_STRATEGY == null ) {
      final RegionFactory cacheRegionFactory = SettingsFactory.createRegionFactory(
          mappings.getConfigurationProperties(), true
      );
      DEFAULT_CACHE_CONCURRENCY_STRATEGY = CacheConcurrencyStrategy.fromAccessType( cacheRegionFactory.getDefaultAccessType() );
    }
    return DEFAULT_CACHE_CONCURRENCY_STRATEGY;
  }
View Full Code Here

TOP

Related Classes of org.hibernate.cache.spi.RegionFactory

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.