Search in sources :

Example 76 with Configuration

use of org.hibernate.cfg.Configuration in project ignite by apache.

the class CacheHibernateBlobStore method init.

/**
 * Initializes store.
 *
 * @throws IgniteException If failed to initialize.
 */
private void init() throws IgniteException {
    if (initGuard.compareAndSet(false, true)) {
        if (log.isDebugEnabled())
            log.debug("Initializing cache store.");
        try {
            if (sesFactory != null)
                // Session factory has been provided - nothing to do.
                return;
            if (!F.isEmpty(hibernateCfgPath)) {
                try {
                    URL url = new URL(hibernateCfgPath);
                    sesFactory = new Configuration().configure(url).buildSessionFactory();
                    if (log.isDebugEnabled())
                        log.debug("Configured session factory using URL: " + url);
                    // Session factory has been successfully initialized.
                    return;
                } catch (MalformedURLException e) {
                    if (log.isDebugEnabled())
                        log.debug("Caught malformed URL exception: " + e.getMessage());
                }
                // Provided path is not a valid URL. File?
                File cfgFile = new File(hibernateCfgPath);
                if (cfgFile.exists()) {
                    sesFactory = new Configuration().configure(cfgFile).buildSessionFactory();
                    if (log.isDebugEnabled())
                        log.debug("Configured session factory using file: " + hibernateCfgPath);
                    // Session factory has been successfully initialized.
                    return;
                }
                // Provided path is not a file. Classpath resource?
                sesFactory = new Configuration().configure(hibernateCfgPath).buildSessionFactory();
                if (log.isDebugEnabled())
                    log.debug("Configured session factory using classpath resource: " + hibernateCfgPath);
            } else {
                if (hibernateProps == null) {
                    U.warn(log, "No Hibernate configuration has been provided for store (will use default).");
                    hibernateProps = new Properties();
                    hibernateProps.setProperty("hibernate.connection.url", DFLT_CONN_URL);
                    hibernateProps.setProperty("hibernate.show_sql", DFLT_SHOW_SQL);
                    hibernateProps.setProperty("hibernate.hbm2ddl.auto", DFLT_HBM2DDL_AUTO);
                }
                Configuration cfg = new Configuration();
                cfg.setProperties(hibernateProps);
                assert resourceAvailable(MAPPING_RESOURCE) : MAPPING_RESOURCE;
                cfg.addResource(MAPPING_RESOURCE);
                sesFactory = cfg.buildSessionFactory();
                if (log.isDebugEnabled())
                    log.debug("Configured session factory using properties: " + hibernateProps);
            }
        } catch (HibernateException e) {
            throw new IgniteException("Failed to initialize store.", e);
        } finally {
            initLatch.countDown();
        }
    } else if (initLatch.getCount() > 0) {
        try {
            U.await(initLatch);
        } catch (IgniteInterruptedCheckedException e) {
            throw new IgniteException(e);
        }
    }
    if (sesFactory == null)
        throw new IgniteException("Cache store was not properly initialized.");
}
Also used : IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) MalformedURLException(java.net.MalformedURLException) Configuration(org.hibernate.cfg.Configuration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) HibernateException(org.hibernate.HibernateException) IgniteException(org.apache.ignite.IgniteException) Properties(java.util.Properties) File(java.io.File) URL(java.net.URL)

Example 77 with Configuration

use of org.hibernate.cfg.Configuration in project ignite by apache.

the class CacheHibernateStoreSessionListener method start.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("deprecation")
@Override
public void start() throws IgniteException {
    if (sesFactory == null && F.isEmpty(hibernateCfgPath))
        throw new IgniteException("Either session factory or Hibernate configuration file is required by " + getClass().getSimpleName() + '.');
    if (!F.isEmpty(hibernateCfgPath)) {
        if (sesFactory == null) {
            try {
                URL url = new URL(hibernateCfgPath);
                sesFactory = new Configuration().configure(url).buildSessionFactory();
            } catch (MalformedURLException ignored) {
            // No-op.
            }
            if (sesFactory == null) {
                File cfgFile = new File(hibernateCfgPath);
                if (cfgFile.exists())
                    sesFactory = new Configuration().configure(cfgFile).buildSessionFactory();
            }
            if (sesFactory == null)
                sesFactory = new Configuration().configure(hibernateCfgPath).buildSessionFactory();
            if (sesFactory == null)
                throw new IgniteException("Failed to resolve Hibernate configuration file: " + hibernateCfgPath);
            closeSesOnStop = true;
        } else
            U.warn(log, "Hibernate configuration file configured in " + getClass().getSimpleName() + " will be ignored (session factory is already set).");
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Configuration(org.hibernate.cfg.Configuration) IgniteException(org.apache.ignite.IgniteException) File(java.io.File) URL(java.net.URL)

Example 78 with Configuration

use of org.hibernate.cfg.Configuration in project ignite by apache.

the class HibernateL2CacheConfigurationSelfTest method hibernateConfiguration.

/**
 * @param igniteInstanceName Ignite instance name.
 * @return Hibernate configuration.
 */
protected Configuration hibernateConfiguration(String igniteInstanceName) {
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass(Entity1.class);
    cfg.addAnnotatedClass(Entity2.class);
    cfg.addAnnotatedClass(Entity3.class);
    cfg.addAnnotatedClass(Entity4.class);
    cfg.setProperty(HibernateAccessStrategyFactory.DFLT_ACCESS_TYPE_PROPERTY, AccessType.NONSTRICT_READ_WRITE.name());
    cfg.setProperty(HBM2DDL_AUTO, "create");
    cfg.setProperty(GENERATE_STATISTICS, "true");
    cfg.setProperty(USE_SECOND_LEVEL_CACHE, "true");
    cfg.setProperty(USE_QUERY_CACHE, "true");
    cfg.setProperty(CACHE_REGION_FACTORY, HibernateRegionFactory.class.getName());
    cfg.setProperty(RELEASE_CONNECTIONS, "on_close");
    cfg.setProperty(HibernateAccessStrategyFactory.IGNITE_INSTANCE_NAME_PROPERTY, igniteInstanceName);
    cfg.setProperty(REGION_CACHE_PROPERTY + ENTITY1_NAME, "cache1");
    cfg.setProperty(REGION_CACHE_PROPERTY + ENTITY2_NAME, "cache2");
    cfg.setProperty(REGION_CACHE_PROPERTY + TIMESTAMP_CACHE, TIMESTAMP_CACHE);
    cfg.setProperty(REGION_CACHE_PROPERTY + QUERY_CACHE, QUERY_CACHE);
    if (dfltCache)
        cfg.setProperty(DFLT_CACHE_NAME_PROPERTY, "cache3");
    return cfg;
}
Also used : Configuration(org.hibernate.cfg.Configuration) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 79 with Configuration

use of org.hibernate.cfg.Configuration in project ignite by apache.

the class HibernateL2CacheConfigurationSelfTest method startHibernate.

/**
 * @param igniteInstanceName Name of the grid providing caches.
 * @return Session factory.
 */
private SessionFactory startHibernate(String igniteInstanceName) {
    Configuration cfg = hibernateConfiguration(igniteInstanceName);
    StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
    builder.applySetting("hibernate.connection.url", CONNECTION_URL);
    builder.applySetting("hibernate.show_sql", false);
    builder.applySettings(cfg.getProperties());
    return cfg.buildSessionFactory(builder.build());
}
Also used : Configuration(org.hibernate.cfg.Configuration) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder)

Example 80 with Configuration

use of org.hibernate.cfg.Configuration in project scheduling by ow2-proactive.

the class RMDBManager method createInMemoryRMDBManager.

public static RMDBManager createInMemoryRMDBManager() {
    Configuration config = new Configuration();
    config.setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbc.JDBCDriver");
    config.setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:" + System.currentTimeMillis() + ";hsqldb.tx=mvcc");
    config.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
    return new RMDBManager(config, true, true);
}
Also used : Configuration(org.hibernate.cfg.Configuration)

Aggregations

Configuration (org.hibernate.cfg.Configuration)190 Test (org.junit.Test)66 Session (org.hibernate.Session)37 SessionFactory (org.hibernate.SessionFactory)35 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)27 Before (org.junit.Before)20 BeforeClass (org.junit.BeforeClass)20 File (java.io.File)18 ServiceRegistry (org.hibernate.service.ServiceRegistry)14 Properties (java.util.Properties)13 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)9 HibernateException (org.hibernate.HibernateException)8 EncapsulatedCompositeIdResultSetProcessorTest (org.hibernate.test.loadplans.process.EncapsulatedCompositeIdResultSetProcessorTest)8 MappingException (org.hibernate.MappingException)7 Transaction (org.hibernate.Transaction)7 URL (java.net.URL)6 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)6 TestForIssue (org.hibernate.testing.TestForIssue)6 EntityTuplizer (org.hibernate.tuple.entity.EntityTuplizer)6 BigDecimal (java.math.BigDecimal)5