Search in sources :

Example 1 with FederatedStoreManager

use of org.datanucleus.store.federation.FederatedStoreManager in project datanucleus-core by datanucleus.

the class PersistenceNucleusContextImpl method initialise.

public synchronized void initialise() {
    final ClassLoaderResolver clr = getClassLoaderResolver(null);
    clr.registerUserClassLoader((ClassLoader) config.getProperty(PropertyNames.PROPERTY_CLASSLOADER_PRIMARY));
    boolean generateSchema = false;
    boolean generateScripts = false;
    String generateModeStr = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_DATABASE_MODE);
    if (generateModeStr == null || generateModeStr.equalsIgnoreCase("none")) {
        // Try scripts instead of database since that wasn't set
        generateModeStr = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_MODE);
        generateScripts = true;
    }
    if (generateModeStr != null && !generateModeStr.equalsIgnoreCase("none")) {
        generateSchema = true;
        // Add any properties that are needed by schema generation (before we create StoreManager)
        if (!config.getBooleanProperty(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_ALL)) {
            config.setProperty(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_ALL, "true");
        }
        if (!config.getBooleanProperty(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_TABLES)) {
            config.setProperty(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_TABLES, "true");
        }
        if (!config.getBooleanProperty(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_COLUMNS)) {
            config.setProperty(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_COLUMNS, "true");
        }
        if (!config.getBooleanProperty(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_CONSTRAINTS)) {
            config.setProperty(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_CONSTRAINTS, "true");
        }
        if (!config.getBooleanProperty(PropertyNames.PROPERTY_DATASTORE_READONLY)) {
            config.setProperty(PropertyNames.PROPERTY_DATASTORE_READONLY, "false");
        }
    }
    // Create the StoreManager
    try {
        Set<String> propNamesWithDatastore = config.getPropertyNamesWithPrefix("datanucleus.datastore.");
        if (propNamesWithDatastore == null) {
            // Find the StoreManager using the persistence property if specified
            NucleusLogger.DATASTORE.debug("Creating StoreManager for datastore");
            Map<String, Object> datastoreProps = config.getDatastoreProperties();
            this.storeMgr = NucleusContextHelper.createStoreManagerForProperties(config.getPersistenceProperties(), datastoreProps, clr, this);
            // Make sure the isolation level is valid for this StoreManager and correct if necessary
            String transactionIsolation = config.getStringProperty(PropertyNames.PROPERTY_TRANSACTION_ISOLATION);
            if (transactionIsolation != null) {
                String reqdIsolation = NucleusContextHelper.getTransactionIsolationForStoreManager(storeMgr, transactionIsolation);
                if (!transactionIsolation.equalsIgnoreCase(reqdIsolation)) {
                    config.setProperty(PropertyNames.PROPERTY_TRANSACTION_ISOLATION, reqdIsolation);
                }
            }
        } else {
            NucleusLogger.DATASTORE.debug("Creating FederatedStoreManager to handle federation of primary StoreManager and " + propNamesWithDatastore.size() + " secondary datastores");
            this.storeMgr = new FederatedStoreManager(clr, this);
            this.federated = true;
        }
    } catch (NucleusException ne) {
        NucleusLogger.DATASTORE.error("Exception thrown creating StoreManager : " + StringUtils.getMessageFromRootCauseOfThrowable(ne));
        throw ne;
    }
    NucleusLogger.DATASTORE.debug("StoreManager now created");
    // Make sure MetaDataManager is initialised
    MetaDataManager mmgr = getMetaDataManager();
    final Level2Cache cache = getLevel2Cache();
    if (cache != null) {
        // Add listener for metadata loading so we can pin any classes in the L2 cache that are marked for that
        mmgr.registerListener(new MetaDataListener() {

            @Override
            public void loaded(AbstractClassMetaData cmd) {
                if (cmd.hasExtension("cache-pin") && cmd.getValueForExtension("cache-pin").equalsIgnoreCase("true")) {
                    // Register as auto-pinned in the L2 cache
                    Class cls = clr.classForName(cmd.getFullClassName());
                    cache.pinAll(cls, false);
                }
            }
        });
    }
    // ========== Initialise the StoreManager contents ==========
    // A). Load any classes specified by auto-start
    String autoStartMechanism = config.getStringProperty(PropertyNames.PROPERTY_AUTOSTART_MECHANISM);
    if (autoStartMechanism != null && !autoStartMechanism.equals("None")) {
        initialiseAutoStart(clr);
    }
    // B). Schema Generation
    if (generateSchema) {
        initialiseSchema(generateModeStr, generateScripts);
    }
    // C). Load up internal representations of all persistence-unit classes
    if (config.getStringProperty(PropertyNames.PROPERTY_PERSISTENCE_UNIT_NAME) != null && config.getBooleanProperty(PropertyNames.PROPERTY_PERSISTENCE_UNIT_LOAD_CLASSES)) {
        // User is using a persistence-unit, so load up its persistence-unit classes into StoreManager
        Collection<String> loadedClasses = getMetaDataManager().getClassesWithMetaData();
        this.storeMgr.manageClasses(clr, loadedClasses.toArray(new String[loadedClasses.size()]));
    }
    if (config.getBooleanProperty(PropertyNames.PROPERTY_QUERY_COMPILE_NAMED_QUERIES_AT_STARTUP)) {
        initialiseNamedQueries(clr);
    }
    if (ecPool == null) {
        ecPool = new ExecutionContextPool(this);
    }
    if (opFactory == null) {
        opFactory = new ObjectProviderFactoryImpl(this);
    }
    if (config.hasProperty(PropertyNames.PROPERTY_MAPPING_TENANT_PROVIDER)) {
        try {
            multiTenancyProvider = (MultiTenancyProvider) config.getProperty(PropertyNames.PROPERTY_MAPPING_TENANT_PROVIDER);
        } catch (Throwable thr) {
            NucleusLogger.PERSISTENCE.warn("Error accessing property " + PropertyNames.PROPERTY_MAPPING_TENANT_PROVIDER + "; should be an instance of MultiTenancyProvider but isnt! Ignored");
        }
    }
    if (config.hasProperty(PropertyNames.PROPERTY_MAPPING_CURRENT_USER_PROVIDER)) {
        try {
            currentUserProvider = (CurrentUserProvider) config.getProperty(PropertyNames.PROPERTY_MAPPING_CURRENT_USER_PROVIDER);
        } catch (Throwable thr) {
            NucleusLogger.PERSISTENCE.warn("Error accessing property " + PropertyNames.PROPERTY_MAPPING_CURRENT_USER_PROVIDER + "; should be an instance of CurrentUserProvider but isnt! Ignored");
        }
    }
    super.initialise();
}
Also used : FederatedStoreManager(org.datanucleus.store.federation.FederatedStoreManager) JavaxCacheLevel2Cache(org.datanucleus.cache.JavaxCacheLevel2Cache) WeakLevel2Cache(org.datanucleus.cache.WeakLevel2Cache) NullLevel2Cache(org.datanucleus.cache.NullLevel2Cache) SoftLevel2Cache(org.datanucleus.cache.SoftLevel2Cache) Level2Cache(org.datanucleus.cache.Level2Cache) MetaDataManager(org.datanucleus.metadata.MetaDataManager) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ObjectProviderFactoryImpl(org.datanucleus.state.ObjectProviderFactoryImpl) MetaDataListener(org.datanucleus.metadata.MetaDataListener) NucleusException(org.datanucleus.exceptions.NucleusException)

Aggregations

JavaxCacheLevel2Cache (org.datanucleus.cache.JavaxCacheLevel2Cache)1 Level2Cache (org.datanucleus.cache.Level2Cache)1 NullLevel2Cache (org.datanucleus.cache.NullLevel2Cache)1 SoftLevel2Cache (org.datanucleus.cache.SoftLevel2Cache)1 WeakLevel2Cache (org.datanucleus.cache.WeakLevel2Cache)1 NucleusException (org.datanucleus.exceptions.NucleusException)1 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)1 MetaDataListener (org.datanucleus.metadata.MetaDataListener)1 MetaDataManager (org.datanucleus.metadata.MetaDataManager)1 ObjectProviderFactoryImpl (org.datanucleus.state.ObjectProviderFactoryImpl)1 FederatedStoreManager (org.datanucleus.store.federation.FederatedStoreManager)1