org.hibernate.service.ServiceRegistryBuilder Java Examples

The following examples show how to use org.hibernate.service.ServiceRegistryBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HibernateSessionFactory.java    From CoolSignIn with Apache License 2.0 6 votes vote down vote up
/**
    *  Rebuild hibernate session factory
    *
    */
public static void rebuildSessionFactory() {
	try {
		configuration.configure();
		serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);
	} catch (Exception e) {
		System.err.println("%%%% Error Creating SessionFactory %%%%");
		e.printStackTrace();
	}
}
 
Example #2
Source File: SlaveNodeInitializer.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * Constructing a new Hibernate SessionFactory for every request would cause very poor performance.  However, 
 * Java servlets must be thread-safe, so we can't use a SessionFactory as an instance variable.  This method provides 
 * thread-safe access to a SessionFactory, so the startup data loader and the search servlet can open Hibernate sessions 
 * more efficiently.
 * 
 * @return Session
 */
public static synchronized Session openSession() {
	if(sessionFactory == null) {
		Configuration configuration = new Configuration();
		configuration.configure("/hibernate-slave.cfg.xml");
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);
	}
	return sessionFactory.openSession();
}
 
Example #3
Source File: HibernateUtil.java    From computational-economy with GNU General Public License v3.0 5 votes vote down vote up
private static SessionFactory buildSessionFactory() {
	if (HibernateUtil.isActive()) {
		final Configuration configuration = new Configuration();
		configuration.configure("hibernate.cfg.xml");
		final ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder()
				.applySettings(configuration.getProperties());
		final SessionFactory sessionFactory = configuration
				.buildSessionFactory(serviceRegistryBuilder.buildServiceRegistry());
		return sessionFactory;
	}

	return null;
}
 
Example #4
Source File: StartupDataLoader.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * Constructing a new Hibernate SessionFactory for every request would cause very poor performance.  However, 
 * Java servlets must be thread-safe, so we can't use a SessionFactory as an instance variable.  This method provides 
 * thread-safe access to a SessionFactory, so the startup data loader and the search servlet can open Hibernate sessions 
 * more efficiently.
 * 
 * @return Session
 */
public static synchronized Session openSession() {
	if(sessionFactory == null) {
		Configuration configuration = new Configuration();
		configuration.configure();
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);			
	}
	return sessionFactory.openSession();
}
 
Example #5
Source File: AppResource.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * The @GET annotation causes this method to be invoked whenever an HTTP GET request is received for 
 * the registered URL. 
 */
@GET
public App getAppData( @PathParam("appId") Long appId ) {
	// Initialize Hibernate
	Configuration configuration = new Configuration();
	configuration.configure();
	ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
	SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
	Session session = sessionFactory.openSession();
	session.beginTransaction();
	
	// Fetch an App for the given ID, using eager fetching.  The conversion to JSON happens after the 
	// Hibernate Session is closed... so if lazy fetching were used, then the JSON converter would fail 
	// when trying to access associated objects.
	Criteria criteria = session.createCriteria(App.class);
	criteria.add( Restrictions.eq("id", appId) );
	criteria.setFetchMode("supportedDevices", FetchMode.SELECT);
	criteria.setFetchMode("customerReviews", FetchMode.SELECT);
	App app = (App) criteria.uniqueResult();
	
	// Cleanup Hibernate
	session.getTransaction().commit();
	session.clear();
	session.close();
	sessionFactory.close();
	
	return app;
}
 
Example #6
Source File: StartupDataLoader.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * Constructing a new Hibernate SessionFactory for every request would cause very poor performance.  However, 
 * Java servlets must be thread-safe, so we can't use a SessionFactory as an instance variable.  This method provides 
 * thread-safe access to a SessionFactory, so the startup data loader and the search servlet can open Hibernate sessions 
 * more efficiently.
 * 
 * @return Session
 */
public static synchronized Session openSession() {
	if(sessionFactory == null) {
		Configuration configuration = new Configuration();
		configuration.configure();
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);			
	}
	return sessionFactory.openSession();
}
 
Example #7
Source File: StartupDataLoader.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * Constructing a new Hibernate SessionFactory for every request would cause very poor performance.  However, 
 * Java servlets must be thread-safe, so we can't use a SessionFactory as an instance variable.  This method provides 
 * thread-safe access to a SessionFactory, so the startup data loader and the search servlet can open Hibernate sessions 
 * more efficiently.
 * 
 * @return Session
 */
public static synchronized Session openSession() {
	if(sessionFactory == null) {
		Configuration configuration = new Configuration();
		configuration.configure();
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);			
	}
	return sessionFactory.openSession();
}
 
Example #8
Source File: StartupDataLoader.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * Constructing a new Hibernate SessionFactory for every request would cause very poor performance.  However, 
 * Java servlets must be thread-safe, so we can't use a SessionFactory as an instance variable.  This method provides 
 * thread-safe access to a SessionFactory, so the startup data loader and the search servlet can open Hibernate sessions 
 * more efficiently.
 * 
 * @return Session
 */
public static synchronized Session openSession() {
	if(sessionFactory == null) {
		Configuration configuration = new Configuration();
		configuration.configure();
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);			
	}
	return sessionFactory.openSession();
}
 
Example #9
Source File: StartupDataLoader.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * Constructing a new Hibernate SessionFactory for every request would cause very poor performance.  However, 
 * Java servlets must be thread-safe, so we can't use a SessionFactory as an instance variable.  This method provides 
 * thread-safe access to a SessionFactory, so the startup data loader and the search servlet can open Hibernate sessions 
 * more efficiently.
 * 
 * @return Session
 */
public static synchronized Session openSession() {
	if(sessionFactory == null) {
		Configuration configuration = new Configuration();
		configuration.configure();
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);			
	}
	return sessionFactory.openSession();
}
 
Example #10
Source File: StartupDataLoader.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * Constructing a new Hibernate SessionFactory for every request would cause very poor performance.  However, 
 * Java servlets must be thread-safe, so we can't use a SessionFactory as an instance variable.  This method provides 
 * thread-safe access to a SessionFactory, so the startup data loader and the search servlet can open Hibernate sessions 
 * more efficiently.
 * 
 * @return Session
 */
public static synchronized Session openSession() {
	if(sessionFactory == null) {
		Configuration configuration = new Configuration();
		configuration.configure();
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);			
	}
	return sessionFactory.openSession();
}
 
Example #11
Source File: StartupDataLoader.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * Constructing a new Hibernate SessionFactory for every request would cause very poor performance.  However, 
 * Java servlets must be thread-safe, so we can't use a SessionFactory as an instance variable.  This method provides 
 * thread-safe access to a SessionFactory, so the startup data loader and the search servlet can open Hibernate sessions 
 * more efficiently.
 * 
 * @return Session
 */
public static synchronized Session openSession() {
	if(sessionFactory == null) {
		Configuration configuration = new Configuration();
		configuration.configure();
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);			
	}
	return sessionFactory.openSession();
}
 
Example #12
Source File: StartupDataLoader.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * Constructing a new Hibernate SessionFactory for every request would cause very poor performance.  However, 
 * Java servlets must be thread-safe, so we can't use a SessionFactory as an instance variable.  This method provides 
 * thread-safe access to a SessionFactory, so the startup data loader and the search servlet can open Hibernate sessions 
 * more efficiently.
 * 
 * @return Session
 */
public static synchronized Session openSession() {
	if(sessionFactory == null) {
		Configuration configuration = new Configuration();
		configuration.configure();
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);			
	}
	return sessionFactory.openSession();
}
 
Example #13
Source File: StartupDataLoader.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * Constructing a new Hibernate SessionFactory for every request would cause very poor performance.  However, 
 * Java servlets must be thread-safe, so we can't use a SessionFactory as an instance variable.  This method provides 
 * thread-safe access to a SessionFactory, so the startup data loader and the search servlet can open Hibernate sessions 
 * more efficiently.
 * 
 * @return Session
 */
public static synchronized Session openSession() {
	if(sessionFactory == null) {
		Configuration configuration = new Configuration();
		configuration.configure();
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);			
	}
	return sessionFactory.openSession();
}
 
Example #14
Source File: MasterNodeInitializer.java    From maven-framework-project with MIT License 5 votes vote down vote up
/**
 * Constructing a new Hibernate SessionFactory for every request would cause very poor performance.  However, 
 * Java servlets must be thread-safe, so we can't use a SessionFactory as an instance variable.  This method provides 
 * thread-safe access to a SessionFactory, so the startup data loader and the search servlet can open Hibernate sessions 
 * more efficiently.
 * 
 * @return Session
 */
public static synchronized Session openSession() {
	if(sessionFactory == null) {
		Configuration configuration = new Configuration();
		configuration.configure();
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);			
	}
	return sessionFactory.openSession();
}
 
Example #15
Source File: TestHibernateTypes.java    From jasypt with Apache License 2.0 5 votes vote down vote up
private void initialize() {
    registerEncryptors();
    
    // Configure Hibernate and open session
	ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder()
	    .applySetting("hibernate.dialect", "org.hibernate.dialect.HSQLDialect")
           .applySetting("hibernate.connection.url", 
               "jdbc:hsqldb:mem:jasypttestdb")
           .applySetting("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver")
           .applySetting("hibernate.connection.username", "sa")
           .applySetting("hibernate.connection.password", "")
           .applySetting("hibernate.connection.pool_size", "10");
	ServiceRegistry serviceRegistry = serviceRegistryBuilder.buildServiceRegistry();
	
	hbConf = new Configuration();
	sessionFactory = hbConf
           .addClass(User.class)
           .setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect")
           .setProperty("hibernate.connection.url", 
                   "jdbc:hsqldb:mem:jasypttestdb")
           .setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver")
           .setProperty("hibernate.connection.username", "sa")
           .setProperty("hibernate.connection.password", "")
           .setProperty("hibernate.connection.pool_size", "10")
           .buildSessionFactory(serviceRegistry);
	session = sessionFactory.openSession();
    
	initDB();		
	
	generateData();
}
 
Example #16
Source File: HibernateSessionFactory.java    From ALLGO with Apache License 2.0 5 votes vote down vote up
/**
    *  Rebuild hibernate session factory
    *
    */
public static void rebuildSessionFactory() {
	try {
		configuration.configure();
		serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);
	} catch (Exception e) {
		System.err.println("%%%% Error Creating SessionFactory %%%%");
		e.printStackTrace();
	}
}
 
Example #17
Source File: HibernateL2CacheSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @return Hibernate registry builder.
 */
protected ServiceRegistryBuilder registryBuilder() {
    ServiceRegistryBuilder builder = new ServiceRegistryBuilder();

    builder.applySetting("hibernate.connection.url", CONNECTION_URL);

    return builder;
}
 
Example #18
Source File: HibernateL2CacheStrategySelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param accessType Cache access typr.
 * @param igniteInstanceName Name of the grid providing caches.
 * @return Session factory.
 */
private SessionFactory startHibernate(AccessType accessType, String igniteInstanceName) {
    Configuration cfg = hibernateConfiguration(accessType, igniteInstanceName);

    ServiceRegistryBuilder builder = new ServiceRegistryBuilder();

    builder.applySetting("hibernate.connection.url", CONNECTION_URL);
    builder.applySetting("hibernate.show_sql", false);

    return cfg.buildSessionFactory(builder.buildServiceRegistry());
}
 
Example #19
Source File: HibernateL2CacheConfigurationSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param igniteInstanceName Name of the grid providing caches.
 * @return Session factory.
 */
private SessionFactory startHibernate(String igniteInstanceName) {
    Configuration cfg = hibernateConfiguration(igniteInstanceName);

    ServiceRegistryBuilder builder = new ServiceRegistryBuilder();

    builder.applySetting("hibernate.connection.url", CONNECTION_URL);
    builder.applySetting("hibernate.show_sql", false);

    return cfg.buildSessionFactory(builder.buildServiceRegistry());
}
 
Example #20
Source File: HibernateFabric.java    From Komondor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Configuration of session factory with Fabric integration.
 */
public static SessionFactory createSessionFactory(String fabricUrl, String username, String password, String fabricUser, String fabricPassword)
        throws Exception {
    // creating this here allows passing needed params to the constructor
    FabricMultiTenantConnectionProvider connProvider = new FabricMultiTenantConnectionProvider(fabricUrl, "employees", "employees", username, password,
            fabricUser, fabricPassword);
    ServiceRegistryBuilder srb = new ServiceRegistryBuilder();
    srb.addService(org.hibernate.service.jdbc.connections.spi.MultiTenantConnectionProvider.class, connProvider);
    srb.applySetting("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect");

    Configuration config = new Configuration();
    config.setProperty("hibernate.multiTenancy", "DATABASE");
    config.addResource("com/mysql/fabric/demo/employee.hbm.xml");
    return config.buildSessionFactory(srb.buildServiceRegistry());
}
 
Example #21
Source File: HibernateFabric.java    From r-course with MIT License 5 votes vote down vote up
/**
 * Configuration of session factory with Fabric integration.
 */
public static SessionFactory createSessionFactory(String fabricUrl, String username, String password, String fabricUser, String fabricPassword)
        throws Exception {
    // creating this here allows passing needed params to the constructor
    FabricMultiTenantConnectionProvider connProvider = new FabricMultiTenantConnectionProvider(fabricUrl, "employees", "employees", username, password,
            fabricUser, fabricPassword);
    ServiceRegistryBuilder srb = new ServiceRegistryBuilder();
    srb.addService(org.hibernate.service.jdbc.connections.spi.MultiTenantConnectionProvider.class, connProvider);
    srb.applySetting("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect");

    Configuration config = new Configuration();
    config.setProperty("hibernate.multiTenancy", "DATABASE");
    config.addResource("com/mysql/fabric/demo/employee.hbm.xml");
    return config.buildSessionFactory(srb.buildServiceRegistry());
}
 
Example #22
Source File: MamuteDatabaseConfiguration.java    From mamute with Apache License 2.0 4 votes vote down vote up
public SessionFactory buildSessionFactory() {
	ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
	return cfg.buildSessionFactory(serviceRegistry);
}
 
Example #23
Source File: HibernateL2CacheMultiJvmTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @param igniteInstanceName Name of the grid providing caches.
 * @return Session factory.
 */
SessionFactory startHibernate(String igniteInstanceName) {
    log.info("Start hibernate on node: " + igniteInstanceName);

    Configuration cfg = hibernateConfiguration(igniteInstanceName);

    ServiceRegistryBuilder builder = new ServiceRegistryBuilder();

    builder.applySetting("hibernate.connection.url", CONNECTION_URL);

    return cfg.buildSessionFactory(builder.buildServiceRegistry());
}
 
Example #24
Source File: HibernateL2CacheSelfTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * Starts Hibernate.
 *
 * @param accessType Cache access type.
 * @param igniteInstanceName Ignite instance name.
 * @return Session factory.
 */
private SessionFactory startHibernate(AccessType accessType, String igniteInstanceName) {
    Configuration cfg = hibernateConfiguration(accessType, igniteInstanceName);

    ServiceRegistryBuilder builder = registryBuilder();

    builder.applySetting("hibernate.show_sql", false);

    return cfg.buildSessionFactory(builder.buildServiceRegistry());
}
 
Example #25
Source File: HibernateL2CacheTransactionalSelfTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/** {@inheritDoc} */
@Nullable @Override protected ServiceRegistryBuilder registryBuilder() {
    ServiceRegistryBuilder builder = new ServiceRegistryBuilder();

    DatasourceConnectionProviderImpl connProvider = new DatasourceConnectionProviderImpl();

    BasicManagedDataSource dataSrc = new BasicManagedDataSource(); // JTA-aware data source.

    dataSrc.setTransactionManager(jotm.getTransactionManager());

    dataSrc.setDefaultAutoCommit(false);

    JdbcDataSource h2DataSrc = new JdbcDataSource();

    h2DataSrc.setURL(CONNECTION_URL);

    dataSrc.setXaDataSourceInstance(h2DataSrc);

    connProvider.setDataSource(dataSrc);

    connProvider.configure(Collections.emptyMap());

    builder.addService(ConnectionProvider.class, connProvider);

    builder.addService(JtaPlatform.class, new TestJtaPlatform());

    builder.addService(TransactionFactory.class, new JtaTransactionFactory());

    return builder;
}