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

The following examples show how to use org.hibernate.boot.registry.StandardServiceRegistryBuilder#destroy() . 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: NamedParameterUnitTest.java    From tutorials with 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 2
Source File: SchemaUpdate.java    From lams with 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 3
Source File: HibernateLoggingIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@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 File: SchemaValidator.java    From lams with 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 SchemaValidator().validate( metadata, serviceRegistry );
		}
		finally {
			StandardServiceRegistryBuilder.destroy( serviceRegistry );
		}
	}
	catch (Exception e) {
		LOG.unableToRunSchemaUpdate( e );
	}
}
 
Example 5
Source File: SchemaExport.java    From lams with 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 File: HibernateUtil.java    From pikatimer with GNU General Public License v3.0 5 votes vote down vote up
public void close() throws Exception{
        if (sessionFactory != null) {
            sessionFactory.close();
        }
        if(serviceRegistry!= null) {
            StandardServiceRegistryBuilder.destroy(serviceRegistry);
        }
}
 
Example 7
Source File: SchemaValidator.java    From lams with 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 8
Source File: SchemaUpdate.java    From lams with 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 9
Source File: ModelDBHibernateUtil.java    From modeldb with 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 10
Source File: ModelDBHibernateUtil.java    From modeldb with Apache License 2.0 4 votes vote down vote up
public static void shutdown() {
  if (registry != null) {
    StandardServiceRegistryBuilder.destroy(registry);
  }
}
 
Example 11
Source File: HibernateInheritanceTablePerClass.java    From java-course-ee with MIT License 4 votes vote down vote up
private static void destroy() {
    StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
 
Example 12
Source File: HibernateColumnTransformer.java    From java-course-ee with MIT License 4 votes vote down vote up
private static void destroy() {
    StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
 
Example 13
Source File: HibernateRecursiveParentEntity.java    From java-course-ee with MIT License 4 votes vote down vote up
private static void destroy() {
    StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
 
Example 14
Source File: HibernateInheritanceSingleTable.java    From java-course-ee with MIT License 4 votes vote down vote up
private static void destroy() {
    StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
 
Example 15
Source File: HibernateSecondLevelCache.java    From java-course-ee with MIT License 4 votes vote down vote up
private static void destroy() {
    StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
 
Example 16
Source File: HibernateInheritanceJoined.java    From java-course-ee with MIT License 4 votes vote down vote up
private static void destroy() {
    StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
 
Example 17
Source File: HibernateManyToMany.java    From java-course-ee with MIT License 4 votes vote down vote up
private static void destroy() {
    StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
 
Example 18
Source File: HibernateCRUD.java    From java-course-ee with MIT License 4 votes vote down vote up
private static void destroy() {
    StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
 
Example 19
Source File: HibernateOneToOne.java    From java-course-ee with MIT License 4 votes vote down vote up
private static void destroy() {
    StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
 
Example 20
Source File: HibernateManyToOne.java    From java-course-ee with MIT License 4 votes vote down vote up
private static void destroy() {
    StandardServiceRegistryBuilder.destroy(serviceRegistry);
}