Search in sources :

Example 1 with DialectFactory

use of org.hibernate.engine.jdbc.dialect.spi.DialectFactory in project hibernate-orm by hibernate.

the class JdbcEnvironmentInitiator method initiateService.

@Override
public JdbcEnvironment initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
    final DialectFactory dialectFactory = registry.getService(DialectFactory.class);
    // 'hibernate.temp.use_jdbc_metadata_defaults' is a temporary magic value.
    // The need for it is intended to be alleviated with future development, thus it is
    // not defined as an Environment constant...
    //
    // it is used to control whether we should consult the JDBC metadata to determine
    // certain Settings default values; it is useful to *not* do this when the database
    // may not be available (mainly in tools usage).
    boolean useJdbcMetadata = ConfigurationHelper.getBoolean("hibernate.temp.use_jdbc_metadata_defaults", configurationValues, true);
    if (useJdbcMetadata) {
        final JdbcConnectionAccess jdbcConnectionAccess = buildJdbcConnectionAccess(configurationValues, registry);
        try {
            final Connection connection = jdbcConnectionAccess.obtainConnection();
            try {
                final DatabaseMetaData dbmd = connection.getMetaData();
                if (log.isDebugEnabled()) {
                    log.debugf("Database ->\n" + "       name : %s\n" + "    version : %s\n" + "      major : %s\n" + "      minor : %s", dbmd.getDatabaseProductName(), dbmd.getDatabaseProductVersion(), dbmd.getDatabaseMajorVersion(), dbmd.getDatabaseMinorVersion());
                    log.debugf("Driver ->\n" + "       name : %s\n" + "    version : %s\n" + "      major : %s\n" + "      minor : %s", dbmd.getDriverName(), dbmd.getDriverVersion(), dbmd.getDriverMajorVersion(), dbmd.getDriverMinorVersion());
                    log.debugf("JDBC version : %s.%s", dbmd.getJDBCMajorVersion(), dbmd.getJDBCMinorVersion());
                }
                Dialect dialect = dialectFactory.buildDialect(configurationValues, new DialectResolutionInfoSource() {

                    @Override
                    public DialectResolutionInfo getDialectResolutionInfo() {
                        try {
                            return new DatabaseMetaDataDialectResolutionInfoAdapter(connection.getMetaData());
                        } catch (SQLException sqlException) {
                            throw new HibernateException("Unable to access java.sql.DatabaseMetaData to determine appropriate Dialect to use", sqlException);
                        }
                    }
                });
                return new JdbcEnvironmentImpl(registry, dialect, dbmd);
            } catch (SQLException e) {
                log.unableToObtainConnectionMetadata(e.getMessage());
            } finally {
                try {
                    jdbcConnectionAccess.releaseConnection(connection);
                } catch (SQLException ignore) {
                }
            }
        } catch (Exception e) {
            log.unableToObtainConnectionToQueryMetadata(e.getMessage());
        }
    }
    // if we get here, either we were asked to not use JDBC metadata or accessing the JDBC metadata failed.
    return new JdbcEnvironmentImpl(registry, dialectFactory.buildDialect(configurationValues, null));
}
Also used : SQLException(java.sql.SQLException) HibernateException(org.hibernate.HibernateException) Connection(java.sql.Connection) DatabaseMetaDataDialectResolutionInfoAdapter(org.hibernate.engine.jdbc.dialect.spi.DatabaseMetaDataDialectResolutionInfoAdapter) DialectFactory(org.hibernate.engine.jdbc.dialect.spi.DialectFactory) DatabaseMetaData(java.sql.DatabaseMetaData) SQLException(java.sql.SQLException) HibernateException(org.hibernate.HibernateException) DialectResolutionInfoSource(org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfoSource) JdbcConnectionAccess(org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess) Dialect(org.hibernate.dialect.Dialect) DialectResolutionInfo(org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo)

Aggregations

Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 SQLException (java.sql.SQLException)1 HibernateException (org.hibernate.HibernateException)1 Dialect (org.hibernate.dialect.Dialect)1 JdbcConnectionAccess (org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess)1 DatabaseMetaDataDialectResolutionInfoAdapter (org.hibernate.engine.jdbc.dialect.spi.DatabaseMetaDataDialectResolutionInfoAdapter)1 DialectFactory (org.hibernate.engine.jdbc.dialect.spi.DialectFactory)1 DialectResolutionInfo (org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo)1 DialectResolutionInfoSource (org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfoSource)1