Java Code Examples for org.hibernate.boot.registry.StandardServiceRegistryBuilder#destroy()

The following examples show how to use org.hibernate.boot.registry.StandardServiceRegistryBuilder#destroy() . These examples are extracted from open source projects. 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 Project: lams   File: SchemaUpdate.java    License: GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) {
	try {
		final CommandLineArgs parsedArgs = CommandLineArgs.parseCommandLineArgs( args );
		final StandardServiceRegistry serviceRegistry = buildStandardServiceRegistry( parsedArgs );

		try {
			final MetadataImplementor metadata = buildMetadata( parsedArgs, serviceRegistry );

			new SchemaUpdate()
					.setOutputFile( parsedArgs.outputFile )
					.setDelimiter( parsedArgs.delimiter )
					.execute( parsedArgs.targetTypes, metadata, serviceRegistry );
		}
		finally {
			StandardServiceRegistryBuilder.destroy( serviceRegistry );
		}
	}
	catch (Exception e) {
		LOG.unableToRunSchemaUpdate( e );
	}
}
 
Example 2
public static void main(String[] args) {
	try {
		final CommandLineArgs parsedArgs = CommandLineArgs.parseCommandLineArgs( args );
		final StandardServiceRegistry serviceRegistry = buildStandardServiceRegistry( parsedArgs );

		try {
			final MetadataImplementor metadata = buildMetadata( parsedArgs, serviceRegistry );
			new SchemaValidator().validate( metadata, serviceRegistry );
		}
		finally {
			StandardServiceRegistryBuilder.destroy( serviceRegistry );
		}
	}
	catch (Exception e) {
		LOG.unableToRunSchemaUpdate( e );
	}
}
 
Example 3
@Before
public void setUp() throws IOException {
    final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure("hibernate-logging.cfg.xml")
        .build();
    try {
        sessionFactory = new MetadataSources(registry).buildMetadata()
            .buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(new Employee("John Smith", "001"));
        session.getTransaction()
            .commit();
        session.close();
    } catch (Exception e) {
        fail(e);
        StandardServiceRegistryBuilder.destroy(registry);
    }
}
 
Example 4
Source Project: tutorials   File: NamedParameterUnitTest.java    License: MIT License 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
            .configure()
            .build();
    try {
        sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(new Event("Event 1"));
        session.save(new Event("Event 2"));
        session.getTransaction().commit();
        session.close();
    } catch (Exception e) {
        fail(e);
        StandardServiceRegistryBuilder.destroy(registry);
    }
}
 
Example 5
Source Project: lams   File: SchemaExport.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Intended for test usage only.  Builds a Metadata using the same algorithm  as
 * {@link #main}
 *
 * @param args The "command line args"
 *
 * @return The built Metadata
 *
 * @throws Exception Problems building the Metadata
 */
public static MetadataImplementor buildMetadataFromMainArgs(String[] args) throws Exception {
	final CommandLineArgs commandLineArgs = CommandLineArgs.parseCommandLineArgs( args );
	StandardServiceRegistry serviceRegistry = buildStandardServiceRegistry( commandLineArgs );
	try {
		return buildMetadata( commandLineArgs, serviceRegistry );
	}
	finally {
		StandardServiceRegistryBuilder.destroy( serviceRegistry );
	}
}
 
Example 6
Source Project: lams   File: SchemaUpdate.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Intended for test usage only.  Builds a Metadata using the same algorithm  as
 * {@link #main}
 *
 * @param args The "command line args"
 *
 * @return The built Metadata
 *
 * @throws Exception Problems building the Metadata
 */
public static MetadataImplementor buildMetadataFromMainArgs(String[] args) throws Exception {
	final CommandLineArgs commandLineArgs = CommandLineArgs.parseCommandLineArgs( args );
	StandardServiceRegistry serviceRegistry = buildStandardServiceRegistry( commandLineArgs );
	try {
		return buildMetadata( commandLineArgs, serviceRegistry );
	}
	finally {
		StandardServiceRegistryBuilder.destroy( serviceRegistry );
	}
}
 
Example 7
/**
 * Intended for test usage only.  Builds a Metadata using the same algorithm  as
 * {@link #main}
 *
 * @param args The "command line args"
 *
 * @return The built Metadata
 *
 * @throws Exception Problems building the Metadata
 */
public static MetadataImplementor buildMetadataFromMainArgs(String[] args) throws Exception {
	final CommandLineArgs commandLineArgs = CommandLineArgs.parseCommandLineArgs( args );
	StandardServiceRegistry serviceRegistry = buildStandardServiceRegistry( commandLineArgs );
	try {
		return buildMetadata( commandLineArgs, serviceRegistry );
	}
	finally {
		StandardServiceRegistryBuilder.destroy( serviceRegistry );
	}
}
 
Example 8
public void close() throws Exception{
        if (sessionFactory != null) {
            sessionFactory.close();
        }
        if(serviceRegistry!= null) {
            StandardServiceRegistryBuilder.destroy(serviceRegistry);
        }
}
 
Example 9
Source Project: java-course-ee   File: HibernateManyToOne.java    License: MIT License 4 votes vote down vote up
private static void destroy() {
    StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
 
Example 10
Source Project: modeldb   File: ModelDBHibernateUtil.java    License: Apache License 2.0 4 votes vote down vote up
public static SessionFactory createOrGetSessionFactory() throws ModelDBException {
  if (sessionFactory == null) {
    LOGGER.info("Fetching sessionFactory");
    try {
      App app = App.getInstance();
      Map<String, Object> databasePropMap = app.getDatabasePropMap();

      Map<String, Object> rDBPropMap =
          (Map<String, Object>) databasePropMap.get("RdbConfiguration");

      databaseName = (String) rDBPropMap.get("RdbDatabaseName");
      if (!app.getTraceEnabled()) {
        rDBDriver = (String) rDBPropMap.get("RdbDriver");
      } else {
        rDBDriver = "io.opentracing.contrib.jdbc.TracingDriver";
      }
      rDBUrl = (String) rDBPropMap.get("RdbUrl");
      rDBDialect = (String) rDBPropMap.get("RdbDialect");
      configUsername = (String) rDBPropMap.get("RdbUsername");
      configPassword = (String) rDBPropMap.get("RdbPassword");
      if (databasePropMap.containsKey("timeout")) {
        timeout = (Integer) databasePropMap.get("timeout");
      }
      liquibaseLockThreshold =
          Long.parseLong(databasePropMap.getOrDefault("liquibaseLockThreshold", "60").toString());

      // Change liquibase default table names
      System.getProperties().put("liquibase.databaseChangeLogTableName", "database_change_log");
      System.getProperties()
          .put("liquibase.databaseChangeLogLockTableName", "database_change_log_lock");

      // Hibernate settings equivalent to hibernate.cfg.xml's properties
      Configuration configuration = new Configuration();

      Properties settings = new Properties();

      String connectionString =
          rDBUrl
              + "/"
              + databaseName
              + "?createDatabaseIfNotExist=true&useUnicode=yes&characterEncoding=UTF-8";
      settings.put(Environment.DRIVER, rDBDriver);
      settings.put(Environment.URL, connectionString);
      settings.put(Environment.USER, configUsername);
      settings.put(Environment.PASS, configPassword);
      settings.put(Environment.DIALECT, rDBDialect);
      settings.put(Environment.HBM2DDL_AUTO, "validate");
      settings.put(Environment.SHOW_SQL, "false");
      configuration.setProperties(settings);

      LOGGER.trace("connectionString {}", connectionString);
      // Create registry builder
      StandardServiceRegistryBuilder registryBuilder =
          new StandardServiceRegistryBuilder().applySettings(settings);
      MetadataSources metaDataSrc = new MetadataSources(registryBuilder.build());
      for (Class entity : entities) {
        metaDataSrc.addAnnotatedClass(entity);
      }

      // Check DB is up or not
      boolean dbConnectionStatus =
          checkDBConnection(
              rDBDriver, rDBUrl, databaseName, configUsername, configPassword, timeout);
      if (!dbConnectionStatus) {
        checkDBConnectionInLoop(true);
      }

      releaseLiquibaseLock(metaDataSrc);

      // Run tables liquibase migration
      createTablesLiquibaseMigration(metaDataSrc);

      // Create session factory and validate entity
      sessionFactory = metaDataSrc.buildMetadata().buildSessionFactory();

      // Export schema
      if (ModelDBConstants.EXPORT_SCHEMA) {
        exportSchema(metaDataSrc.buildMetadata());
      }

      // Check if any migration need to be run or not and based on the migration status flag
      runMigration();

      LOGGER.info(ModelDBMessages.READY_STATUS, isReady);
      isReady = true;
      return sessionFactory;
    } catch (Exception e) {
      LOGGER.warn(
          "ModelDBHibernateUtil getSessionFactory() getting error : {}", e.getMessage(), e);
      if (registry != null) {
        StandardServiceRegistryBuilder.destroy(registry);
      }
      throw new ModelDBException(e.getMessage());
    }
  } else {
    return loopBack(sessionFactory);
  }
}
 
Example 11
Source Project: modeldb   File: ModelDBHibernateUtil.java    License: Apache License 2.0 4 votes vote down vote up
public static void shutdown() {
  if (registry != null) {
    StandardServiceRegistryBuilder.destroy(registry);
  }
}
 
Example 12
private static void destroy() {
    StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
 
Example 13
private static void destroy() {
    StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
 
Example 14
private static void destroy() {
    StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
 
Example 15
private static void destroy() {
    StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
 
Example 16
private static void destroy() {
    StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
 
Example 17
private static void destroy() {
    StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
 
Example 18
Source Project: java-course-ee   File: HibernateManyToMany.java    License: MIT License 4 votes vote down vote up
private static void destroy() {
    StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
 
Example 19
Source Project: java-course-ee   File: HibernateCRUD.java    License: MIT License 4 votes vote down vote up
private static void destroy() {
    StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
 
Example 20
Source Project: java-course-ee   File: HibernateOneToOne.java    License: MIT License 4 votes vote down vote up
private static void destroy() {
    StandardServiceRegistryBuilder.destroy(serviceRegistry);
}