Package org.hibernate.connection

Examples of org.hibernate.connection.ConnectionProvider


    String sessionFactoryName = props.getProperty(Environment.SESSION_FACTORY_NAME);
    settings.setSessionFactoryName(sessionFactoryName);

    //JDBC and connection settings:

    ConnectionProvider connections = createConnectionProvider(props);
    settings.setConnectionProvider(connections);

    //Interrogate JDBC metadata

    String databaseName = null;
    int databaseMajorVersion = 0;
    boolean metaSupportsScrollable = false;
    boolean metaSupportsGetGeneratedKeys = false;
    boolean metaSupportsBatchUpdates = false;
    boolean metaReportsDDLCausesTxnCommit = false;
    boolean metaReportsDDLInTxnSupported = true;

    // 'hibernate.temp.use_jdbc_metadata_defaults' is a temporary magic value.
    // The need for it is intended to be alleviated with 3.3 developement, 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 = PropertiesHelper.getBoolean( "hibernate.temp.use_jdbc_metadata_defaults", props, true );
    if ( useJdbcMetadata ) {
      try {
        Connection conn = connections.getConnection();
        try {
          DatabaseMetaData meta = conn.getMetaData();
          databaseName = meta.getDatabaseProductName();
          databaseMajorVersion = getDatabaseMajorVersion(meta);
          log.info("RDBMS: " + databaseName + ", version: " + meta.getDatabaseProductVersion() );
          log.info("JDBC driver: " + meta.getDriverName() + ", version: " + meta.getDriverVersion() );

          metaSupportsScrollable = meta.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE);
          metaSupportsBatchUpdates = meta.supportsBatchUpdates();
          metaReportsDDLCausesTxnCommit = meta.dataDefinitionCausesTransactionCommit();
          metaReportsDDLInTxnSupported = !meta.dataDefinitionIgnoredInTransactions();

          if ( Environment.jvmSupportsGetGeneratedKeys() ) {
            try {
              Boolean result = (Boolean) DatabaseMetaData.class.getMethod("supportsGetGeneratedKeys", null)
                .invoke(meta, null);
              metaSupportsGetGeneratedKeys = result.booleanValue();
            }
            catch (AbstractMethodError ame) {
              metaSupportsGetGeneratedKeys = false;
            }
            catch (Exception e) {
              metaSupportsGetGeneratedKeys = false;
            }
          }

        }
        finally {
          connections.closeConnection(conn);
        }
      }
      catch (SQLException sqle) {
        log.warn("Could not obtain connection metadata", sqle);
      }
      catch (UnsupportedOperationException uoe) {
        // user supplied JDBC connections
      }
    }
    settings.setDataDefinitionImplicitCommit( metaReportsDDLCausesTxnCommit );
    settings.setDataDefinitionInTransactionSupported( metaReportsDDLInTxnSupported );


    //SQL Dialect:
    Dialect dialect = determineDialect( props, databaseName, databaseMajorVersion );
    settings.setDialect(dialect);
   
    //use dialect default properties
    final Properties properties = new Properties();
    properties.putAll( dialect.getDefaultProperties() );
    properties.putAll(props);
   
    // Transaction settings:
   
    TransactionFactory transactionFactory = createTransactionFactory(properties);
    settings.setTransactionFactory(transactionFactory);
    settings.setTransactionManagerLookup( createTransactionManagerLookup(properties) );

    boolean flushBeforeCompletion = PropertiesHelper.getBoolean(Environment.FLUSH_BEFORE_COMPLETION, properties);
    log.info("Automatic flush during beforeCompletion(): " + enabledDisabled(flushBeforeCompletion) );
    settings.setFlushBeforeCompletionEnabled(flushBeforeCompletion);

    boolean autoCloseSession = PropertiesHelper.getBoolean(Environment.AUTO_CLOSE_SESSION, properties);
    log.info("Automatic session close at end of transaction: " + enabledDisabled(autoCloseSession) );
    settings.setAutoCloseSessionEnabled(autoCloseSession);

    //JDBC and connection settings:

    int batchSize = PropertiesHelper.getInt(Environment.STATEMENT_BATCH_SIZE, properties, 0);
    if ( !metaSupportsBatchUpdates ) batchSize = 0;
    if (batchSize>0) log.info("JDBC batch size: " + batchSize);
    settings.setJdbcBatchSize(batchSize);
    boolean jdbcBatchVersionedData = PropertiesHelper.getBoolean(Environment.BATCH_VERSIONED_DATA, properties, false);
    if (batchSize>0) log.info("JDBC batch updates for versioned data: " + enabledDisabled(jdbcBatchVersionedData) );
    settings.setJdbcBatchVersionedData(jdbcBatchVersionedData);
    settings.setBatcherFactory( createBatcherFactory(properties, batchSize) );
   
    boolean useScrollableResultSets = PropertiesHelper.getBoolean(Environment.USE_SCROLLABLE_RESULTSET, properties, metaSupportsScrollable);
    log.info("Scrollable result sets: " + enabledDisabled(useScrollableResultSets) );
    settings.setScrollableResultSetsEnabled(useScrollableResultSets);

    boolean wrapResultSets = PropertiesHelper.getBoolean(Environment.WRAP_RESULT_SETS, properties, false);
    log.debug( "Wrap result sets: " + enabledDisabled(wrapResultSets) );
    settings.setWrapResultSetsEnabled(wrapResultSets);

    boolean useGetGeneratedKeys = PropertiesHelper.getBoolean(Environment.USE_GET_GENERATED_KEYS, properties, metaSupportsGetGeneratedKeys);
    log.info("JDBC3 getGeneratedKeys(): " + enabledDisabled(useGetGeneratedKeys) );
    settings.setGetGeneratedKeysEnabled(useGetGeneratedKeys);

    Integer statementFetchSize = PropertiesHelper.getInteger(Environment.STATEMENT_FETCH_SIZE, properties);
    if (statementFetchSize!=null) log.info("JDBC result set fetch size: " + statementFetchSize);
    settings.setJdbcFetchSize(statementFetchSize);

    String releaseModeName = PropertiesHelper.getString( Environment.RELEASE_CONNECTIONS, properties, "auto" );
    log.info( "Connection release mode: " + releaseModeName );
    ConnectionReleaseMode releaseMode;
    if ( "auto".equals(releaseModeName) ) {
      releaseMode = transactionFactory.getDefaultReleaseMode();
    }
    else {
      releaseMode = ConnectionReleaseMode.parse( releaseModeName );
      if ( releaseMode == ConnectionReleaseMode.AFTER_STATEMENT && !connections.supportsAggressiveRelease() ) {
        log.warn( "Overriding release mode as connection provider does not support 'after_statement'" );
        releaseMode = ConnectionReleaseMode.AFTER_TRANSACTION;
      }
    }
    settings.setConnectionReleaseMode( releaseMode );
View Full Code Here


    String sessionFactoryName = props.getProperty(Environment.SESSION_FACTORY_NAME);
    settings.setSessionFactoryName(sessionFactoryName);

    //JDBC and connection settings:

    ConnectionProvider connections = createConnectionProvider(props);
    settings.setConnectionProvider(connections);

    //Interrogate JDBC metadata

    boolean metaSupportsScrollable = false;
    boolean metaSupportsGetGeneratedKeys = false;
    boolean metaSupportsBatchUpdates = false;
    boolean metaReportsDDLCausesTxnCommit = false;
    boolean metaReportsDDLInTxnSupported = true;
    Dialect dialect = null;
    JdbcSupport jdbcSupport = null;
    // 'hibernate.temp.use_jdbc_metadata_defaults' is a temporary magic value.
    // The need for it is intended to be alleviated with future developement, 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 = PropertiesHelper.getBoolean( "hibernate.temp.use_jdbc_metadata_defaults", props, true );
    if ( useJdbcMetadata ) {
      try {
        Connection conn = connections.getConnection();
        try {
          DatabaseMetaData meta = conn.getMetaData();
          log.info( "RDBMS: " + meta.getDatabaseProductName() + ", version: " + meta.getDatabaseProductVersion() );
          log.info( "JDBC driver: " + meta.getDriverName() + ", version: " + meta.getDriverVersion() );

          dialect = DialectFactory.buildDialect( props, conn );
          jdbcSupport = JdbcSupportLoader.loadJdbcSupport( conn );
          metaSupportsScrollable = meta.supportsResultSetType( ResultSet.TYPE_SCROLL_INSENSITIVE );
          metaSupportsBatchUpdates = meta.supportsBatchUpdates();
          metaReportsDDLCausesTxnCommit = meta.dataDefinitionCausesTransactionCommit();
          metaReportsDDLInTxnSupported = !meta.dataDefinitionIgnoredInTransactions();
          metaSupportsGetGeneratedKeys = meta.supportsGetGeneratedKeys();
        }
        catch ( SQLException sqle ) {
          log.warn( "Could not obtain connection metadata", sqle );
        }
        finally {
          connections.closeConnection( conn );
        }
      }
      catch ( SQLException sqle ) {
        log.warn( "Could not obtain connection to query metadata", sqle );
        dialect = DialectFactory.buildDialect( props );
      }
      catch ( UnsupportedOperationException uoe ) {
        // user supplied JDBC connections
        dialect = DialectFactory.buildDialect( props );
      }
    }
    else {
      dialect = DialectFactory.buildDialect( props );
    }

    settings.setDataDefinitionImplicitCommit( metaReportsDDLCausesTxnCommit );
    settings.setDataDefinitionInTransactionSupported( metaReportsDDLInTxnSupported );
    settings.setDialect( dialect );
    if ( jdbcSupport == null ){
      jdbcSupport = JdbcSupportLoader.loadJdbcSupport( null );
    }
    settings.setJdbcSupport( jdbcSupport );
    //use dialect default properties
    final Properties properties = new Properties();
    properties.putAll( dialect.getDefaultProperties() );
    properties.putAll( props );

    // Transaction settings:

    TransactionFactory transactionFactory = createTransactionFactory(properties);
    settings.setTransactionFactory(transactionFactory);
    settings.setTransactionManagerLookup( createTransactionManagerLookup(properties) );

    boolean flushBeforeCompletion = PropertiesHelper.getBoolean(Environment.FLUSH_BEFORE_COMPLETION, properties);
    log.info("Automatic flush during beforeCompletion(): " + enabledDisabled(flushBeforeCompletion) );
    settings.setFlushBeforeCompletionEnabled(flushBeforeCompletion);

    boolean autoCloseSession = PropertiesHelper.getBoolean(Environment.AUTO_CLOSE_SESSION, properties);
    log.info("Automatic session close at end of transaction: " + enabledDisabled(autoCloseSession) );
    settings.setAutoCloseSessionEnabled(autoCloseSession);

    //JDBC and connection settings:

    int batchSize = PropertiesHelper.getInt(Environment.STATEMENT_BATCH_SIZE, properties, 0);
    if ( !metaSupportsBatchUpdates ) batchSize = 0;
    if (batchSize>0) log.info("JDBC batch size: " + batchSize);
    settings.setJdbcBatchSize(batchSize);
    boolean jdbcBatchVersionedData = PropertiesHelper.getBoolean(Environment.BATCH_VERSIONED_DATA, properties, false);
    if (batchSize>0) log.info("JDBC batch updates for versioned data: " + enabledDisabled(jdbcBatchVersionedData) );
    settings.setJdbcBatchVersionedData(jdbcBatchVersionedData);
    settings.setBatcherFactory( createBatcherFactory(properties, batchSize) );

    boolean useScrollableResultSets = PropertiesHelper.getBoolean(Environment.USE_SCROLLABLE_RESULTSET, properties, metaSupportsScrollable);
    log.info("Scrollable result sets: " + enabledDisabled(useScrollableResultSets) );
    settings.setScrollableResultSetsEnabled(useScrollableResultSets);

    boolean wrapResultSets = PropertiesHelper.getBoolean(Environment.WRAP_RESULT_SETS, properties, false);
    log.debug( "Wrap result sets: " + enabledDisabled(wrapResultSets) );
    settings.setWrapResultSetsEnabled(wrapResultSets);

    boolean useGetGeneratedKeys = PropertiesHelper.getBoolean(Environment.USE_GET_GENERATED_KEYS, properties, metaSupportsGetGeneratedKeys);
    log.info("JDBC3 getGeneratedKeys(): " + enabledDisabled(useGetGeneratedKeys) );
    settings.setGetGeneratedKeysEnabled(useGetGeneratedKeys);

    Integer statementFetchSize = PropertiesHelper.getInteger(Environment.STATEMENT_FETCH_SIZE, properties);
    if (statementFetchSize!=null) log.info("JDBC result set fetch size: " + statementFetchSize);
    settings.setJdbcFetchSize(statementFetchSize);

    String releaseModeName = PropertiesHelper.getString( Environment.RELEASE_CONNECTIONS, properties, "auto" );
    log.info( "Connection release mode: " + releaseModeName );
    ConnectionReleaseMode releaseMode;
    if ( "auto".equals(releaseModeName) ) {
      releaseMode = transactionFactory.getDefaultReleaseMode();
    }
    else {
      releaseMode = ConnectionReleaseMode.parse( releaseModeName );
      if ( releaseMode == ConnectionReleaseMode.AFTER_STATEMENT && !connections.supportsAggressiveRelease() ) {
        log.warn( "Overriding release mode as connection provider does not support 'after_statement'" );
        releaseMode = ConnectionReleaseMode.AFTER_TRANSACTION;
      }
    }
    settings.setConnectionReleaseMode( releaseMode );
View Full Code Here

    String sessionFactoryName = props.getProperty(Environment.SESSION_FACTORY_NAME);
    settings.setSessionFactoryName(sessionFactoryName);

    //JDBC and connection settings:

    ConnectionProvider connections = createConnectionProvider(props);
    settings.setConnectionProvider(connections);

    //Interrogate JDBC metadata

    boolean metaSupportsScrollable = false;
    boolean metaSupportsGetGeneratedKeys = false;
    boolean metaSupportsBatchUpdates = false;
    boolean metaReportsDDLCausesTxnCommit = false;
    boolean metaReportsDDLInTxnSupported = true;
    Dialect dialect = null;
    JdbcSupport jdbcSupport = null;

    // 'hibernate.temp.use_jdbc_metadata_defaults' is a temporary magic value.
    // The need for it is intended to be alleviated with future developement, 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 = PropertiesHelper.getBoolean( "hibernate.temp.use_jdbc_metadata_defaults", props, true );
    if ( useJdbcMetadata ) {
      try {
        Connection conn = connections.getConnection();
        try {
          DatabaseMetaData meta = conn.getMetaData();
          log.info( "RDBMS: " + meta.getDatabaseProductName() + ", version: " + meta.getDatabaseProductVersion() );
          log.info( "JDBC driver: " + meta.getDriverName() + ", version: " + meta.getDriverVersion() );

          dialect = DialectFactory.buildDialect( props, conn );
          jdbcSupport = JdbcSupportLoader.loadJdbcSupport( conn );

          metaSupportsScrollable = meta.supportsResultSetType( ResultSet.TYPE_SCROLL_INSENSITIVE );
          metaSupportsBatchUpdates = meta.supportsBatchUpdates();
          metaReportsDDLCausesTxnCommit = meta.dataDefinitionCausesTransactionCommit();
          metaReportsDDLInTxnSupported = !meta.dataDefinitionIgnoredInTransactions();
          metaSupportsGetGeneratedKeys = meta.supportsGetGeneratedKeys();
        }
        catch ( SQLException sqle ) {
          log.warn( "Could not obtain connection metadata", sqle );
        }
        finally {
          connections.closeConnection( conn );
        }
      }
      catch ( SQLException sqle ) {
        log.warn( "Could not obtain connection to query metadata", sqle );
        dialect = DialectFactory.buildDialect( props );
      }
      catch ( UnsupportedOperationException uoe ) {
        // user supplied JDBC connections
        dialect = DialectFactory.buildDialect( props );
      }
    }
    else {
      dialect = DialectFactory.buildDialect( props );
    }

    settings.setDataDefinitionImplicitCommit( metaReportsDDLCausesTxnCommit );
    settings.setDataDefinitionInTransactionSupported( metaReportsDDLInTxnSupported );
    settings.setDialect( dialect );
    if ( jdbcSupport == null ) {
      jdbcSupport = JdbcSupportLoader.loadJdbcSupport( null );
    }
    settings.setJdbcSupport( jdbcSupport );

    //use dialect default properties
    final Properties properties = new Properties();
    properties.putAll( dialect.getDefaultProperties() );
    properties.putAll( props );

    // Transaction settings:

    TransactionFactory transactionFactory = createTransactionFactory(properties);
    settings.setTransactionFactory(transactionFactory);
    settings.setTransactionManagerLookup( createTransactionManagerLookup(properties) );

    boolean flushBeforeCompletion = PropertiesHelper.getBoolean(Environment.FLUSH_BEFORE_COMPLETION, properties);
    log.info("Automatic flush during beforeCompletion(): " + enabledDisabled(flushBeforeCompletion) );
    settings.setFlushBeforeCompletionEnabled(flushBeforeCompletion);

    boolean autoCloseSession = PropertiesHelper.getBoolean(Environment.AUTO_CLOSE_SESSION, properties);
    log.info("Automatic session close at end of transaction: " + enabledDisabled(autoCloseSession) );
    settings.setAutoCloseSessionEnabled(autoCloseSession);

    //JDBC and connection settings:

    int batchSize = PropertiesHelper.getInt(Environment.STATEMENT_BATCH_SIZE, properties, 0);
    if ( !metaSupportsBatchUpdates ) batchSize = 0;
    if (batchSize>0) log.info("JDBC batch size: " + batchSize);
    settings.setJdbcBatchSize(batchSize);
    boolean jdbcBatchVersionedData = PropertiesHelper.getBoolean(Environment.BATCH_VERSIONED_DATA, properties, false);
    if (batchSize>0) log.info("JDBC batch updates for versioned data: " + enabledDisabled(jdbcBatchVersionedData) );
    settings.setJdbcBatchVersionedData(jdbcBatchVersionedData);
    settings.setBatcherFactory( createBatcherFactory(properties, batchSize) );

    boolean useScrollableResultSets = PropertiesHelper.getBoolean(Environment.USE_SCROLLABLE_RESULTSET, properties, metaSupportsScrollable);
    log.info("Scrollable result sets: " + enabledDisabled(useScrollableResultSets) );
    settings.setScrollableResultSetsEnabled(useScrollableResultSets);

    boolean wrapResultSets = PropertiesHelper.getBoolean(Environment.WRAP_RESULT_SETS, properties, false);
    log.debug( "Wrap result sets: " + enabledDisabled(wrapResultSets) );
    settings.setWrapResultSetsEnabled(wrapResultSets);

    boolean useGetGeneratedKeys = PropertiesHelper.getBoolean(Environment.USE_GET_GENERATED_KEYS, properties, metaSupportsGetGeneratedKeys);
    log.info("JDBC3 getGeneratedKeys(): " + enabledDisabled(useGetGeneratedKeys) );
    settings.setGetGeneratedKeysEnabled(useGetGeneratedKeys);

    Integer statementFetchSize = PropertiesHelper.getInteger(Environment.STATEMENT_FETCH_SIZE, properties);
    if (statementFetchSize!=null) log.info("JDBC result set fetch size: " + statementFetchSize);
    settings.setJdbcFetchSize(statementFetchSize);

    String releaseModeName = PropertiesHelper.getString( Environment.RELEASE_CONNECTIONS, properties, "auto" );
    log.info( "Connection release mode: " + releaseModeName );
    ConnectionReleaseMode releaseMode;
    if ( "auto".equals(releaseModeName) ) {
      releaseMode = transactionFactory.getDefaultReleaseMode();
    }
    else {
      releaseMode = ConnectionReleaseMode.parse( releaseModeName );
      if ( releaseMode == ConnectionReleaseMode.AFTER_STATEMENT && !connections.supportsAggressiveRelease() ) {
        log.warn( "Overriding release mode as connection provider does not support 'after_statement'" );
        releaseMode = ConnectionReleaseMode.AFTER_TRANSACTION;
      }
    }
    settings.setConnectionReleaseMode( releaseMode );
View Full Code Here

    String sessionFactoryName = props.getProperty(Environment.SESSION_FACTORY_NAME);
    settings.setSessionFactoryName(sessionFactoryName);

    //JDBC and connection settings:

    ConnectionProvider connections = createConnectionProvider(props);
    settings.setConnectionProvider(connections);

    //Interrogate JDBC metadata

    boolean metaSupportsScrollable = false;
    boolean metaSupportsGetGeneratedKeys = false;
    boolean metaSupportsBatchUpdates = false;
    boolean metaReportsDDLCausesTxnCommit = false;
    boolean metaReportsDDLInTxnSupported = true;
    Dialect dialect = null;
    JdbcSupport jdbcSupport = null;

    // '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 = PropertiesHelper.getBoolean( "hibernate.temp.use_jdbc_metadata_defaults", props, true );
    if ( useJdbcMetadata ) {
      try {
        Connection conn = connections.getConnection();
        try {
          DatabaseMetaData meta = conn.getMetaData();

          dialect = DialectFactory.buildDialect( props, conn );
          jdbcSupport = JdbcSupportLoader.loadJdbcSupport( conn );

          metaSupportsScrollable = meta.supportsResultSetType( ResultSet.TYPE_SCROLL_INSENSITIVE );
          metaSupportsBatchUpdates = meta.supportsBatchUpdates();
          metaReportsDDLCausesTxnCommit = meta.dataDefinitionCausesTransactionCommit();
          metaReportsDDLInTxnSupported = !meta.dataDefinitionIgnoredInTransactions();
          metaSupportsGetGeneratedKeys = meta.supportsGetGeneratedKeys();
         
          log.info( "Database ->\n" +
              "       name : " + meta.getDatabaseProductName() + '\n' +
              "    version : " +  meta.getDatabaseProductVersion() + '\n' +
              "      major : " + meta.getDatabaseMajorVersion() + '\n' +
              "      minor : " + meta.getDatabaseMinorVersion()
          );
          log.info( "Driver ->\n" +
              "       name : " + meta.getDriverName() + '\n' +
              "    version : " + meta.getDriverVersion() + '\n' +
              "      major : " + meta.getDriverMajorVersion() + '\n' +
              "      minor : " + meta.getDriverMinorVersion()
          );
        }
        catch ( SQLException sqle ) {
          log.warn( "Could not obtain connection metadata", sqle );
        }
        finally {
          connections.closeConnection( conn );
        }
      }
      catch ( SQLException sqle ) {
        log.warn( "Could not obtain connection to query metadata", sqle );
        dialect = DialectFactory.buildDialect( props );
      }
      catch ( UnsupportedOperationException uoe ) {
        // user supplied JDBC connections
        dialect = DialectFactory.buildDialect( props );
      }
    }
    else {
      dialect = DialectFactory.buildDialect( props );
    }

    settings.setDataDefinitionImplicitCommit( metaReportsDDLCausesTxnCommit );
    settings.setDataDefinitionInTransactionSupported( metaReportsDDLInTxnSupported );
    settings.setDialect( dialect );
    if ( jdbcSupport == null ) {
      jdbcSupport = JdbcSupportLoader.loadJdbcSupport( null );
    }
    settings.setJdbcSupport( jdbcSupport );

    //use dialect default properties
    final Properties properties = new Properties();
    properties.putAll( dialect.getDefaultProperties() );
    properties.putAll( props );

    // Transaction settings:

    TransactionFactory transactionFactory = createTransactionFactory(properties);
    settings.setTransactionFactory(transactionFactory);
    settings.setTransactionManagerLookup( createTransactionManagerLookup(properties) );

    boolean flushBeforeCompletion = PropertiesHelper.getBoolean(Environment.FLUSH_BEFORE_COMPLETION, properties);
    log.info("Automatic flush during beforeCompletion(): " + enabledDisabled(flushBeforeCompletion) );
    settings.setFlushBeforeCompletionEnabled(flushBeforeCompletion);

    boolean autoCloseSession = PropertiesHelper.getBoolean(Environment.AUTO_CLOSE_SESSION, properties);
    log.info("Automatic session close at end of transaction: " + enabledDisabled(autoCloseSession) );
    settings.setAutoCloseSessionEnabled(autoCloseSession);

    //JDBC and connection settings:

    int batchSize = PropertiesHelper.getInt(Environment.STATEMENT_BATCH_SIZE, properties, 0);
    if ( !metaSupportsBatchUpdates ) batchSize = 0;
    if (batchSize>0) log.info("JDBC batch size: " + batchSize);
    settings.setJdbcBatchSize(batchSize);
    boolean jdbcBatchVersionedData = PropertiesHelper.getBoolean(Environment.BATCH_VERSIONED_DATA, properties, false);
    if (batchSize>0) log.info("JDBC batch updates for versioned data: " + enabledDisabled(jdbcBatchVersionedData) );
    settings.setJdbcBatchVersionedData(jdbcBatchVersionedData);
    settings.setBatcherFactory( createBatcherFactory(properties, batchSize) );

    boolean useScrollableResultSets = PropertiesHelper.getBoolean(Environment.USE_SCROLLABLE_RESULTSET, properties, metaSupportsScrollable);
    log.info("Scrollable result sets: " + enabledDisabled(useScrollableResultSets) );
    settings.setScrollableResultSetsEnabled(useScrollableResultSets);

    boolean wrapResultSets = PropertiesHelper.getBoolean(Environment.WRAP_RESULT_SETS, properties, false);
    log.debug( "Wrap result sets: " + enabledDisabled(wrapResultSets) );
    settings.setWrapResultSetsEnabled(wrapResultSets);

    boolean useGetGeneratedKeys = PropertiesHelper.getBoolean(Environment.USE_GET_GENERATED_KEYS, properties, metaSupportsGetGeneratedKeys);
    log.info("JDBC3 getGeneratedKeys(): " + enabledDisabled(useGetGeneratedKeys) );
    settings.setGetGeneratedKeysEnabled(useGetGeneratedKeys);

    Integer statementFetchSize = PropertiesHelper.getInteger(Environment.STATEMENT_FETCH_SIZE, properties);
    if (statementFetchSize!=null) log.info("JDBC result set fetch size: " + statementFetchSize);
    settings.setJdbcFetchSize(statementFetchSize);

    String releaseModeName = PropertiesHelper.getString( Environment.RELEASE_CONNECTIONS, properties, "auto" );
    log.info( "Connection release mode: " + releaseModeName );
    ConnectionReleaseMode releaseMode;
    if ( "auto".equals(releaseModeName) ) {
      releaseMode = transactionFactory.getDefaultReleaseMode();
    }
    else {
      releaseMode = ConnectionReleaseMode.parse( releaseModeName );
      if ( releaseMode == ConnectionReleaseMode.AFTER_STATEMENT && !connections.supportsAggressiveRelease() ) {
        log.warn( "Overriding release mode as connection provider does not support 'after_statement'" );
        releaseMode = ConnectionReleaseMode.AFTER_TRANSACTION;
      }
    }
    settings.setConnectionReleaseMode( releaseMode );
View Full Code Here

    String sessionFactoryName = props.getProperty(Environment.SESSION_FACTORY_NAME);
    settings.setSessionFactoryName(sessionFactoryName);

    //JDBC and connection settings:

    ConnectionProvider connections = createConnectionProvider(props);
    settings.setConnectionProvider(connections);

    //Interrogate JDBC metadata

    boolean metaSupportsScrollable = false;
    boolean metaSupportsGetGeneratedKeys = false;
    boolean metaSupportsBatchUpdates = false;
    boolean metaReportsDDLCausesTxnCommit = false;
    boolean metaReportsDDLInTxnSupported = true;
    Dialect dialect = null;

    // 'hibernate.temp.use_jdbc_metadata_defaults' is a temporary magic value.
    // The need for it is intended to be alleviated with future developement, 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 = PropertiesHelper.getBoolean( "hibernate.temp.use_jdbc_metadata_defaults", props, true );
    if ( useJdbcMetadata ) {
      try {
        Connection conn = connections.getConnection();
        try {
          DatabaseMetaData meta = conn.getMetaData();
          log.info( "RDBMS: " + meta.getDatabaseProductName() + ", version: " + meta.getDatabaseProductVersion() );
          log.info( "JDBC driver: " + meta.getDriverName() + ", version: " + meta.getDriverVersion() );

          dialect = DialectFactory.buildDialect( props, conn );

          metaSupportsScrollable = meta.supportsResultSetType( ResultSet.TYPE_SCROLL_INSENSITIVE );
          metaSupportsBatchUpdates = meta.supportsBatchUpdates();
          metaReportsDDLCausesTxnCommit = meta.dataDefinitionCausesTransactionCommit();
          metaReportsDDLInTxnSupported = !meta.dataDefinitionIgnoredInTransactions();
          metaSupportsGetGeneratedKeys = meta.supportsGetGeneratedKeys();
        }
        catch ( SQLException sqle ) {
          log.warn( "Could not obtain connection metadata", sqle );
        }
        finally {
          connections.closeConnection( conn );
        }
      }
      catch ( SQLException sqle ) {
        log.warn( "Could not obtain connection to query metadata", sqle );
        dialect = DialectFactory.buildDialect( props );
      }
      catch ( UnsupportedOperationException uoe ) {
        // user supplied JDBC connections
        dialect = DialectFactory.buildDialect( props );
      }
    }
    else {
      dialect = DialectFactory.buildDialect( props );
    }

    settings.setDataDefinitionImplicitCommit( metaReportsDDLCausesTxnCommit );
    settings.setDataDefinitionInTransactionSupported( metaReportsDDLInTxnSupported );
    settings.setDialect( dialect );

    //use dialect default properties
    final Properties properties = new Properties();
    properties.putAll( dialect.getDefaultProperties() );
    properties.putAll( props );

    // Transaction settings:

    TransactionFactory transactionFactory = createTransactionFactory(properties);
    settings.setTransactionFactory(transactionFactory);
    settings.setTransactionManagerLookup( createTransactionManagerLookup(properties) );

    boolean flushBeforeCompletion = PropertiesHelper.getBoolean(Environment.FLUSH_BEFORE_COMPLETION, properties);
    log.info("Automatic flush during beforeCompletion(): " + enabledDisabled(flushBeforeCompletion) );
    settings.setFlushBeforeCompletionEnabled(flushBeforeCompletion);

    boolean autoCloseSession = PropertiesHelper.getBoolean(Environment.AUTO_CLOSE_SESSION, properties);
    log.info("Automatic session close at end of transaction: " + enabledDisabled(autoCloseSession) );
    settings.setAutoCloseSessionEnabled(autoCloseSession);

    //JDBC and connection settings:

    int batchSize = PropertiesHelper.getInt(Environment.STATEMENT_BATCH_SIZE, properties, 0);
    if ( !metaSupportsBatchUpdates ) batchSize = 0;
    if (batchSize>0) log.info("JDBC batch size: " + batchSize);
    settings.setJdbcBatchSize(batchSize);
    boolean jdbcBatchVersionedData = PropertiesHelper.getBoolean(Environment.BATCH_VERSIONED_DATA, properties, false);
    if (batchSize>0) log.info("JDBC batch updates for versioned data: " + enabledDisabled(jdbcBatchVersionedData) );
    settings.setJdbcBatchVersionedData(jdbcBatchVersionedData);
    settings.setBatcherFactory( createBatcherFactory(properties, batchSize) );

    boolean useScrollableResultSets = PropertiesHelper.getBoolean(Environment.USE_SCROLLABLE_RESULTSET, properties, metaSupportsScrollable);
    log.info("Scrollable result sets: " + enabledDisabled(useScrollableResultSets) );
    settings.setScrollableResultSetsEnabled(useScrollableResultSets);

    boolean wrapResultSets = PropertiesHelper.getBoolean(Environment.WRAP_RESULT_SETS, properties, false);
    log.debug( "Wrap result sets: " + enabledDisabled(wrapResultSets) );
    settings.setWrapResultSetsEnabled(wrapResultSets);

    boolean useGetGeneratedKeys = PropertiesHelper.getBoolean(Environment.USE_GET_GENERATED_KEYS, properties, metaSupportsGetGeneratedKeys);
    log.info("JDBC3 getGeneratedKeys(): " + enabledDisabled(useGetGeneratedKeys) );
    settings.setGetGeneratedKeysEnabled(useGetGeneratedKeys);

    Integer statementFetchSize = PropertiesHelper.getInteger(Environment.STATEMENT_FETCH_SIZE, properties);
    if (statementFetchSize!=null) log.info("JDBC result set fetch size: " + statementFetchSize);
    settings.setJdbcFetchSize(statementFetchSize);

    String releaseModeName = PropertiesHelper.getString( Environment.RELEASE_CONNECTIONS, properties, "auto" );
    log.info( "Connection release mode: " + releaseModeName );
    ConnectionReleaseMode releaseMode;
    if ( "auto".equals(releaseModeName) ) {
      releaseMode = transactionFactory.getDefaultReleaseMode();
    }
    else {
      releaseMode = ConnectionReleaseMode.parse( releaseModeName );
      if ( releaseMode == ConnectionReleaseMode.AFTER_STATEMENT && !connections.supportsAggressiveRelease() ) {
        log.warn( "Overriding release mode as connection provider does not support 'after_statement'" );
        releaseMode = ConnectionReleaseMode.AFTER_TRANSACTION;
      }
    }
    settings.setConnectionReleaseMode( releaseMode );
View Full Code Here

    String sessionFactoryName = props.getProperty(Environment.SESSION_FACTORY_NAME);
    settings.setSessionFactoryName(sessionFactoryName);

    //JDBC and connection settings:

    ConnectionProvider connections = createConnectionProvider(props);
    settings.setConnectionProvider(connections);

    //Interrogate JDBC metadata

    String databaseName = null;
    int databaseMajorVersion = 0;
    boolean metaSupportsScrollable = false;
    boolean metaSupportsGetGeneratedKeys = false;
    boolean metaSupportsBatchUpdates = false;
    boolean metaReportsDDLCausesTxnCommit = false;
    boolean metaReportsDDLInTxnSupported = true;

    // 'hibernate.temp.use_jdbc_metadata_defaults' is a temporary magic value.
    // The need for it is intended to be alleviated with 3.3 developement, 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 = PropertiesHelper.getBoolean( "hibernate.temp.use_jdbc_metadata_defaults", props, true );
    if ( useJdbcMetadata ) {
      try {
        Connection conn = connections.getConnection();
        try {
          DatabaseMetaData meta = conn.getMetaData();
          databaseName = meta.getDatabaseProductName();
          databaseMajorVersion = getDatabaseMajorVersion(meta);
          log.info("RDBMS: " + databaseName + ", version: " + meta.getDatabaseProductVersion() );
          log.info("JDBC driver: " + meta.getDriverName() + ", version: " + meta.getDriverVersion() );

          metaSupportsScrollable = meta.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE);
          metaSupportsBatchUpdates = meta.supportsBatchUpdates();
          metaReportsDDLCausesTxnCommit = meta.dataDefinitionCausesTransactionCommit();
          metaReportsDDLInTxnSupported = !meta.dataDefinitionIgnoredInTransactions();

          if ( Environment.jvmSupportsGetGeneratedKeys() ) {
            try {
              Boolean result = (Boolean) DatabaseMetaData.class.getMethod("supportsGetGeneratedKeys", null)
                .invoke(meta, null);
              metaSupportsGetGeneratedKeys = result.booleanValue();
            }
            catch (AbstractMethodError ame) {
              metaSupportsGetGeneratedKeys = false;
            }
            catch (Exception e) {
              metaSupportsGetGeneratedKeys = false;
            }
          }

        }
        finally {
          connections.closeConnection(conn);
        }
      }
      catch (SQLException sqle) {
        log.warn("Could not obtain connection metadata", sqle);
      }
      catch (UnsupportedOperationException uoe) {
        // user supplied JDBC connections
      }
    }
    settings.setDataDefinitionImplicitCommit( metaReportsDDLCausesTxnCommit );
    settings.setDataDefinitionInTransactionSupported( metaReportsDDLInTxnSupported );


    //SQL Dialect:
    Dialect dialect = determineDialect( props, databaseName, databaseMajorVersion );
    settings.setDialect(dialect);
   
    //use dialect default properties
    final Properties properties = new Properties();
    properties.putAll( dialect.getDefaultProperties() );
    properties.putAll(props);
   
    // Transaction settings:
   
    TransactionFactory transactionFactory = createTransactionFactory(properties);
    settings.setTransactionFactory(transactionFactory);
    settings.setTransactionManagerLookup( createTransactionManagerLookup(properties) );

    boolean flushBeforeCompletion = PropertiesHelper.getBoolean(Environment.FLUSH_BEFORE_COMPLETION, properties);
    log.info("Automatic flush during beforeCompletion(): " + enabledDisabled(flushBeforeCompletion) );
    settings.setFlushBeforeCompletionEnabled(flushBeforeCompletion);

    boolean autoCloseSession = PropertiesHelper.getBoolean(Environment.AUTO_CLOSE_SESSION, properties);
    log.info("Automatic session close at end of transaction: " + enabledDisabled(autoCloseSession) );
    settings.setAutoCloseSessionEnabled(autoCloseSession);

    //JDBC and connection settings:

    int batchSize = PropertiesHelper.getInt(Environment.STATEMENT_BATCH_SIZE, properties, 0);
    if ( !metaSupportsBatchUpdates ) batchSize = 0;
    if (batchSize>0) log.info("JDBC batch size: " + batchSize);
    settings.setJdbcBatchSize(batchSize);
    boolean jdbcBatchVersionedData = PropertiesHelper.getBoolean(Environment.BATCH_VERSIONED_DATA, properties, false);
    if (batchSize>0) log.info("JDBC batch updates for versioned data: " + enabledDisabled(jdbcBatchVersionedData) );
    settings.setJdbcBatchVersionedData(jdbcBatchVersionedData);
    settings.setBatcherFactory( createBatcherFactory(properties, batchSize) );
   
    boolean useScrollableResultSets = PropertiesHelper.getBoolean(Environment.USE_SCROLLABLE_RESULTSET, properties, metaSupportsScrollable);
    log.info("Scrollable result sets: " + enabledDisabled(useScrollableResultSets) );
    settings.setScrollableResultSetsEnabled(useScrollableResultSets);

    boolean wrapResultSets = PropertiesHelper.getBoolean(Environment.WRAP_RESULT_SETS, properties, false);
    log.debug( "Wrap result sets: " + enabledDisabled(wrapResultSets) );
    settings.setWrapResultSetsEnabled(wrapResultSets);

    boolean useGetGeneratedKeys = PropertiesHelper.getBoolean(Environment.USE_GET_GENERATED_KEYS, properties, metaSupportsGetGeneratedKeys);
    log.info("JDBC3 getGeneratedKeys(): " + enabledDisabled(useGetGeneratedKeys) );
    settings.setGetGeneratedKeysEnabled(useGetGeneratedKeys);

    Integer statementFetchSize = PropertiesHelper.getInteger(Environment.STATEMENT_FETCH_SIZE, properties);
    if (statementFetchSize!=null) log.info("JDBC result set fetch size: " + statementFetchSize);
    settings.setJdbcFetchSize(statementFetchSize);

    String releaseModeName = PropertiesHelper.getString( Environment.RELEASE_CONNECTIONS, properties, "auto" );
    log.info( "Connection release mode: " + releaseModeName );
    ConnectionReleaseMode releaseMode;
    if ( "auto".equals(releaseModeName) ) {
      releaseMode = transactionFactory.getDefaultReleaseMode();
    }
    else {
      releaseMode = ConnectionReleaseMode.parse( releaseModeName );
      if ( releaseMode == ConnectionReleaseMode.AFTER_STATEMENT && !connections.supportsAggressiveRelease() ) {
        log.warn( "Overriding release mode as connection provider does not support 'after_statement'" );
        releaseMode = ConnectionReleaseMode.AFTER_TRANSACTION;
      }
    }
    settings.setConnectionReleaseMode( releaseMode );
View Full Code Here

    String sessionFactoryName = props.getProperty(Environment.SESSION_FACTORY_NAME);
    settings.setSessionFactoryName(sessionFactoryName);

    //JDBC and connection settings:

    ConnectionProvider connections = createConnectionProvider(props);
    settings.setConnectionProvider(connections);

    //Interrogate JDBC metadata

    String databaseName = null;
    int databaseMajorVersion = 0;
    boolean metaSupportsScrollable = false;
    boolean metaSupportsGetGeneratedKeys = false;
    boolean metaSupportsBatchUpdates = false;
    boolean metaReportsDDLCausesTxnCommit = false;
    boolean metaReportsDDLInTxnSupported = true;

    // 'hibernate.temp.use_jdbc_metadata_defaults' is a temporary magic value.
    // The need for it is intended to be alleviated with 3.3 developement, 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 = PropertiesHelper.getBoolean( "hibernate.temp.use_jdbc_metadata_defaults", props, true );
    if ( useJdbcMetadata ) {
      try {
        Connection conn = connections.getConnection();
        try {
          DatabaseMetaData meta = conn.getMetaData();
          databaseName = meta.getDatabaseProductName();
          databaseMajorVersion = getDatabaseMajorVersion(meta);
          log.info("RDBMS: " + databaseName + ", version: " + meta.getDatabaseProductVersion() );
          log.info("JDBC driver: " + meta.getDriverName() + ", version: " + meta.getDriverVersion() );

          metaSupportsScrollable = meta.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE);
          metaSupportsBatchUpdates = meta.supportsBatchUpdates();
          metaReportsDDLCausesTxnCommit = meta.dataDefinitionCausesTransactionCommit();
          metaReportsDDLInTxnSupported = !meta.dataDefinitionIgnoredInTransactions();

          if ( Environment.jvmSupportsGetGeneratedKeys() ) {
            try {
              Boolean result = (Boolean) DatabaseMetaData.class.getMethod("supportsGetGeneratedKeys", null)
                .invoke(meta, null);
              metaSupportsGetGeneratedKeys = result.booleanValue();
            }
            catch (AbstractMethodError ame) {
              metaSupportsGetGeneratedKeys = false;
            }
            catch (Exception e) {
              metaSupportsGetGeneratedKeys = false;
            }
          }

        }
        finally {
          connections.closeConnection(conn);
        }
      }
      catch (SQLException sqle) {
        log.warn("Could not obtain connection metadata", sqle);
      }
      catch (UnsupportedOperationException uoe) {
        // user supplied JDBC connections
      }
    }
    settings.setDataDefinitionImplicitCommit( metaReportsDDLCausesTxnCommit );
    settings.setDataDefinitionInTransactionSupported( metaReportsDDLInTxnSupported );


    //SQL Dialect:
    Dialect dialect = determineDialect( props, databaseName, databaseMajorVersion );
    settings.setDialect(dialect);
   
    //use dialect default properties
    final Properties properties = new Properties();
    properties.putAll( dialect.getDefaultProperties() );
    properties.putAll(props);
   
    // Transaction settings:
   
    TransactionFactory transactionFactory = createTransactionFactory(properties);
    settings.setTransactionFactory(transactionFactory);
    settings.setTransactionManagerLookup( createTransactionManagerLookup(properties) );

    boolean flushBeforeCompletion = PropertiesHelper.getBoolean(Environment.FLUSH_BEFORE_COMPLETION, properties);
    log.info("Automatic flush during beforeCompletion(): " + enabledDisabled(flushBeforeCompletion) );
    settings.setFlushBeforeCompletionEnabled(flushBeforeCompletion);

    boolean autoCloseSession = PropertiesHelper.getBoolean(Environment.AUTO_CLOSE_SESSION, properties);
    log.info("Automatic session close at end of transaction: " + enabledDisabled(autoCloseSession) );
    settings.setAutoCloseSessionEnabled(autoCloseSession);

    //JDBC and connection settings:

    int batchSize = PropertiesHelper.getInt(Environment.STATEMENT_BATCH_SIZE, properties, 0);
    if ( !metaSupportsBatchUpdates ) batchSize = 0;
    if (batchSize>0) log.info("JDBC batch size: " + batchSize);
    settings.setJdbcBatchSize(batchSize);
    boolean jdbcBatchVersionedData = PropertiesHelper.getBoolean(Environment.BATCH_VERSIONED_DATA, properties, false);
    if (batchSize>0) log.info("JDBC batch updates for versioned data: " + enabledDisabled(jdbcBatchVersionedData) );
    settings.setJdbcBatchVersionedData(jdbcBatchVersionedData);
    settings.setBatcherFactory( createBatcherFactory(properties, batchSize) );
   
    boolean useScrollableResultSets = PropertiesHelper.getBoolean(Environment.USE_SCROLLABLE_RESULTSET, properties, metaSupportsScrollable);
    log.info("Scrollable result sets: " + enabledDisabled(useScrollableResultSets) );
    settings.setScrollableResultSetsEnabled(useScrollableResultSets);

    boolean wrapResultSets = PropertiesHelper.getBoolean(Environment.WRAP_RESULT_SETS, properties, false);
    log.debug( "Wrap result sets: " + enabledDisabled(wrapResultSets) );
    settings.setWrapResultSetsEnabled(wrapResultSets);

    boolean useGetGeneratedKeys = PropertiesHelper.getBoolean(Environment.USE_GET_GENERATED_KEYS, properties, metaSupportsGetGeneratedKeys);
    log.info("JDBC3 getGeneratedKeys(): " + enabledDisabled(useGetGeneratedKeys) );
    settings.setGetGeneratedKeysEnabled(useGetGeneratedKeys);

    Integer statementFetchSize = PropertiesHelper.getInteger(Environment.STATEMENT_FETCH_SIZE, properties);
    if (statementFetchSize!=null) log.info("JDBC result set fetch size: " + statementFetchSize);
    settings.setJdbcFetchSize(statementFetchSize);

    String releaseModeName = PropertiesHelper.getString( Environment.RELEASE_CONNECTIONS, properties, "auto" );
    log.info( "Connection release mode: " + releaseModeName );
    ConnectionReleaseMode releaseMode;
    if ( "auto".equals(releaseModeName) ) {
      releaseMode = transactionFactory.getDefaultReleaseMode();
    }
    else {
      releaseMode = ConnectionReleaseMode.parse( releaseModeName );
      if ( releaseMode == ConnectionReleaseMode.AFTER_STATEMENT && !connections.supportsAggressiveRelease() ) {
        log.warn( "Overriding release mode as connection provider does not support 'after_statement'" );
        releaseMode = ConnectionReleaseMode.AFTER_TRANSACTION;
      }
    }
    settings.setConnectionReleaseMode( releaseMode );
View Full Code Here

      log.error("could not write file: " + outputFile, e);
    }
  }

  private String[] generateScript(boolean doUpdate) throws SQLException {
    ConnectionProvider connectionProvider = settings.getConnectionProvider();
    try {
      log.info("acquiring a connection");
      Connection connection = connectionProvider.getConnection();
      try {
        if (!connection.getAutoCommit()) {
          connection.commit();
          connection.setAutoCommit(true);
        }
        log.info("fetching database metadata");
        Dialect dialect = settings.getDialect();
        DatabaseMetadata meta = new DatabaseMetadata(connection, dialect);

        log.info("generating schema update script");
        String[] script = configuration.generateSchemaUpdateScript(dialect, meta);

        if (doUpdate) {
          log.info("updating schema");
          doUpdate(script, connection);
          log.info("schema update complete");
        }

        return script;
      }
      finally {
        JDBCExceptionReporter.logAndClearWarnings(connection);
        connectionProvider.closeConnection(connection);
      }
    }
    finally {
      connectionProvider.close();
    }
  }
View Full Code Here

    String sessionFactoryName = props.getProperty(Environment.SESSION_FACTORY_NAME);
    settings.setSessionFactoryName(sessionFactoryName);

    //JDBC and connection settings:

    ConnectionProvider connections = createConnectionProvider(props);
    settings.setConnectionProvider(connections);

    //Interrogate JDBC metadata

    boolean metaSupportsScrollable = false;
    boolean metaSupportsGetGeneratedKeys = false;
    boolean metaSupportsBatchUpdates = false;
    boolean metaReportsDDLCausesTxnCommit = false;
    boolean metaReportsDDLInTxnSupported = true;
    Dialect dialect = null;
    JdbcSupport jdbcSupport = null;

    // '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 = PropertiesHelper.getBoolean( "hibernate.temp.use_jdbc_metadata_defaults", props, true );
    if ( useJdbcMetadata ) {
      try {
        Connection conn = connections.getConnection();
        try {
          DatabaseMetaData meta = conn.getMetaData();
          log.info( "Database ->\n" +
              "       name : " + meta.getDatabaseProductName() + '\n' +
              "    version : " +  meta.getDatabaseProductVersion() + '\n' +
              "      major : " + meta.getDatabaseMajorVersion() + '\n' +
              "      minor : " + meta.getDatabaseMinorVersion()
          );
          log.info( "Driver ->\n" +
              "       name : " + meta.getDriverName() + '\n' +
              "    version : " + meta.getDriverVersion() + '\n' +
              "      major : " + meta.getDriverMajorVersion() + '\n' +
              "      minor : " + meta.getDriverMinorVersion()
          );

          dialect = DialectFactory.buildDialect( props, conn );
          jdbcSupport = JdbcSupportLoader.loadJdbcSupport( conn );

          metaSupportsScrollable = meta.supportsResultSetType( ResultSet.TYPE_SCROLL_INSENSITIVE );
          metaSupportsBatchUpdates = meta.supportsBatchUpdates();
          metaReportsDDLCausesTxnCommit = meta.dataDefinitionCausesTransactionCommit();
          metaReportsDDLInTxnSupported = !meta.dataDefinitionIgnoredInTransactions();
          metaSupportsGetGeneratedKeys = meta.supportsGetGeneratedKeys();
        }
        catch ( SQLException sqle ) {
          log.warn( "Could not obtain connection metadata", sqle );
        }
        finally {
          connections.closeConnection( conn );
        }
      }
      catch ( SQLException sqle ) {
        log.warn( "Could not obtain connection to query metadata", sqle );
        dialect = DialectFactory.buildDialect( props );
      }
      catch ( UnsupportedOperationException uoe ) {
        // user supplied JDBC connections
        dialect = DialectFactory.buildDialect( props );
      }
    }
    else {
      dialect = DialectFactory.buildDialect( props );
    }

    settings.setDataDefinitionImplicitCommit( metaReportsDDLCausesTxnCommit );
    settings.setDataDefinitionInTransactionSupported( metaReportsDDLInTxnSupported );
    settings.setDialect( dialect );
    if ( jdbcSupport == null ) {
      jdbcSupport = JdbcSupportLoader.loadJdbcSupport( null );
    }
    settings.setJdbcSupport( jdbcSupport );

    //use dialect default properties
    final Properties properties = new Properties();
    properties.putAll( dialect.getDefaultProperties() );
    properties.putAll( props );

    // Transaction settings:

    TransactionFactory transactionFactory = createTransactionFactory(properties);
    settings.setTransactionFactory(transactionFactory);
    settings.setTransactionManagerLookup( createTransactionManagerLookup(properties) );

    boolean flushBeforeCompletion = PropertiesHelper.getBoolean(Environment.FLUSH_BEFORE_COMPLETION, properties);
    log.info("Automatic flush during beforeCompletion(): " + enabledDisabled(flushBeforeCompletion) );
    settings.setFlushBeforeCompletionEnabled(flushBeforeCompletion);

    boolean autoCloseSession = PropertiesHelper.getBoolean(Environment.AUTO_CLOSE_SESSION, properties);
    log.info("Automatic session close at end of transaction: " + enabledDisabled(autoCloseSession) );
    settings.setAutoCloseSessionEnabled(autoCloseSession);

    //JDBC and connection settings:

    int batchSize = PropertiesHelper.getInt(Environment.STATEMENT_BATCH_SIZE, properties, 0);
    if ( !metaSupportsBatchUpdates ) batchSize = 0;
    if (batchSize>0) log.info("JDBC batch size: " + batchSize);
    settings.setJdbcBatchSize(batchSize);
    boolean jdbcBatchVersionedData = PropertiesHelper.getBoolean(Environment.BATCH_VERSIONED_DATA, properties, false);
    if (batchSize>0) log.info("JDBC batch updates for versioned data: " + enabledDisabled(jdbcBatchVersionedData) );
    settings.setJdbcBatchVersionedData(jdbcBatchVersionedData);
    settings.setBatcherFactory( createBatcherFactory(properties, batchSize) );

    boolean useScrollableResultSets = PropertiesHelper.getBoolean(Environment.USE_SCROLLABLE_RESULTSET, properties, metaSupportsScrollable);
    log.info("Scrollable result sets: " + enabledDisabled(useScrollableResultSets) );
    settings.setScrollableResultSetsEnabled(useScrollableResultSets);

    boolean wrapResultSets = PropertiesHelper.getBoolean(Environment.WRAP_RESULT_SETS, properties, false);
    log.debug( "Wrap result sets: " + enabledDisabled(wrapResultSets) );
    settings.setWrapResultSetsEnabled(wrapResultSets);

    boolean useGetGeneratedKeys = PropertiesHelper.getBoolean(Environment.USE_GET_GENERATED_KEYS, properties, metaSupportsGetGeneratedKeys);
    log.info("JDBC3 getGeneratedKeys(): " + enabledDisabled(useGetGeneratedKeys) );
    settings.setGetGeneratedKeysEnabled(useGetGeneratedKeys);

    Integer statementFetchSize = PropertiesHelper.getInteger(Environment.STATEMENT_FETCH_SIZE, properties);
    if (statementFetchSize!=null) log.info("JDBC result set fetch size: " + statementFetchSize);
    settings.setJdbcFetchSize(statementFetchSize);

    String releaseModeName = PropertiesHelper.getString( Environment.RELEASE_CONNECTIONS, properties, "auto" );
    log.info( "Connection release mode: " + releaseModeName );
    ConnectionReleaseMode releaseMode;
    if ( "auto".equals(releaseModeName) ) {
      releaseMode = transactionFactory.getDefaultReleaseMode();
    }
    else {
      releaseMode = ConnectionReleaseMode.parse( releaseModeName );
      if ( releaseMode == ConnectionReleaseMode.AFTER_STATEMENT && !connections.supportsAggressiveRelease() ) {
        log.warn( "Overriding release mode as connection provider does not support 'after_statement'" );
        releaseMode = ConnectionReleaseMode.AFTER_TRANSACTION;
      }
    }
    settings.setConnectionReleaseMode( releaseMode );
View Full Code Here

    String sessionFactoryName = props.getProperty(Environment.SESSION_FACTORY_NAME);
    settings.setSessionFactoryName(sessionFactoryName);

    //JDBC and connection settings:

    ConnectionProvider connections = createConnectionProvider(props);
    settings.setConnectionProvider(connections);

    //Interrogate JDBC metadata

    boolean metaSupportsScrollable = false;
    boolean metaSupportsGetGeneratedKeys = false;
    boolean metaSupportsBatchUpdates = false;
    boolean metaReportsDDLCausesTxnCommit = false;
    boolean metaReportsDDLInTxnSupported = true;
    Dialect dialect = null;
    JdbcSupport jdbcSupport = null;

    // 'hibernate.temp.use_jdbc_metadata_defaults' is a temporary magic value.
    // The need for it is intended to be alleviated with future developement, 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 = PropertiesHelper.getBoolean( "hibernate.temp.use_jdbc_metadata_defaults", props, true );
    if ( useJdbcMetadata ) {
      try {
        Connection conn = connections.getConnection();
        try {
          DatabaseMetaData meta = conn.getMetaData();
          log.info( "RDBMS: " + meta.getDatabaseProductName() + ", version: " + meta.getDatabaseProductVersion() );
          log.info( "JDBC driver: " + meta.getDriverName() + ", version: " + meta.getDriverVersion() );

          dialect = DialectFactory.buildDialect( props, conn );
          jdbcSupport = JdbcSupportLoader.loadJdbcSupport( conn );

          metaSupportsScrollable = meta.supportsResultSetType( ResultSet.TYPE_SCROLL_INSENSITIVE );
          metaSupportsBatchUpdates = meta.supportsBatchUpdates();
          metaReportsDDLCausesTxnCommit = meta.dataDefinitionCausesTransactionCommit();
          metaReportsDDLInTxnSupported = !meta.dataDefinitionIgnoredInTransactions();
          metaSupportsGetGeneratedKeys = meta.supportsGetGeneratedKeys();
        }
        catch ( SQLException sqle ) {
          log.warn( "Could not obtain connection metadata", sqle );
        }
        finally {
          connections.closeConnection( conn );
        }
      }
      catch ( SQLException sqle ) {
        log.warn( "Could not obtain connection to query metadata", sqle );
        dialect = DialectFactory.buildDialect( props );
      }
      catch ( UnsupportedOperationException uoe ) {
        // user supplied JDBC connections
        dialect = DialectFactory.buildDialect( props );
      }
    }
    else {
      dialect = DialectFactory.buildDialect( props );
    }

    settings.setDataDefinitionImplicitCommit( metaReportsDDLCausesTxnCommit );
    settings.setDataDefinitionInTransactionSupported( metaReportsDDLInTxnSupported );
    settings.setDialect( dialect );
    if ( jdbcSupport == null ) {
      jdbcSupport = JdbcSupportLoader.loadJdbcSupport( null );
    }
    settings.setJdbcSupport( jdbcSupport );

    //use dialect default properties
    final Properties properties = new Properties();
    properties.putAll( dialect.getDefaultProperties() );
    properties.putAll( props );

    // Transaction settings:

    TransactionFactory transactionFactory = createTransactionFactory(properties);
    settings.setTransactionFactory(transactionFactory);
    settings.setTransactionManagerLookup( createTransactionManagerLookup(properties) );

    boolean flushBeforeCompletion = PropertiesHelper.getBoolean(Environment.FLUSH_BEFORE_COMPLETION, properties);
    log.info("Automatic flush during beforeCompletion(): " + enabledDisabled(flushBeforeCompletion) );
    settings.setFlushBeforeCompletionEnabled(flushBeforeCompletion);

    boolean autoCloseSession = PropertiesHelper.getBoolean(Environment.AUTO_CLOSE_SESSION, properties);
    log.info("Automatic session close at end of transaction: " + enabledDisabled(autoCloseSession) );
    settings.setAutoCloseSessionEnabled(autoCloseSession);

    //JDBC and connection settings:

    int batchSize = PropertiesHelper.getInt(Environment.STATEMENT_BATCH_SIZE, properties, 0);
    if ( !metaSupportsBatchUpdates ) batchSize = 0;
    if (batchSize>0) log.info("JDBC batch size: " + batchSize);
    settings.setJdbcBatchSize(batchSize);
    boolean jdbcBatchVersionedData = PropertiesHelper.getBoolean(Environment.BATCH_VERSIONED_DATA, properties, false);
    if (batchSize>0) log.info("JDBC batch updates for versioned data: " + enabledDisabled(jdbcBatchVersionedData) );
    settings.setJdbcBatchVersionedData(jdbcBatchVersionedData);
    settings.setBatcherFactory( createBatcherFactory(properties, batchSize) );

    boolean useScrollableResultSets = PropertiesHelper.getBoolean(Environment.USE_SCROLLABLE_RESULTSET, properties, metaSupportsScrollable);
    log.info("Scrollable result sets: " + enabledDisabled(useScrollableResultSets) );
    settings.setScrollableResultSetsEnabled(useScrollableResultSets);

    boolean wrapResultSets = PropertiesHelper.getBoolean(Environment.WRAP_RESULT_SETS, properties, false);
    log.debug( "Wrap result sets: " + enabledDisabled(wrapResultSets) );
    settings.setWrapResultSetsEnabled(wrapResultSets);

    boolean useGetGeneratedKeys = PropertiesHelper.getBoolean(Environment.USE_GET_GENERATED_KEYS, properties, metaSupportsGetGeneratedKeys);
    log.info("JDBC3 getGeneratedKeys(): " + enabledDisabled(useGetGeneratedKeys) );
    settings.setGetGeneratedKeysEnabled(useGetGeneratedKeys);

    Integer statementFetchSize = PropertiesHelper.getInteger(Environment.STATEMENT_FETCH_SIZE, properties);
    if (statementFetchSize!=null) log.info("JDBC result set fetch size: " + statementFetchSize);
    settings.setJdbcFetchSize(statementFetchSize);

    String releaseModeName = PropertiesHelper.getString( Environment.RELEASE_CONNECTIONS, properties, "auto" );
    log.info( "Connection release mode: " + releaseModeName );
    ConnectionReleaseMode releaseMode;
    if ( "auto".equals(releaseModeName) ) {
      releaseMode = transactionFactory.getDefaultReleaseMode();
    }
    else {
      releaseMode = ConnectionReleaseMode.parse( releaseModeName );
      if ( releaseMode == ConnectionReleaseMode.AFTER_STATEMENT && !connections.supportsAggressiveRelease() ) {
        log.warn( "Overriding release mode as connection provider does not support 'after_statement'" );
        releaseMode = ConnectionReleaseMode.AFTER_TRANSACTION;
      }
    }
    settings.setConnectionReleaseMode( releaseMode );
View Full Code Here

TOP

Related Classes of org.hibernate.connection.ConnectionProvider

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.