public SettingsFactory() {
}
public Settings buildSettings(Properties props, ServiceRegistry serviceRegistry) {
final boolean debugEnabled = LOG.isDebugEnabled();
final JdbcServices jdbcServices = serviceRegistry.getService( JdbcServices.class );
Settings settings = new Settings();
//SessionFactory name:
String sessionFactoryName = props.getProperty(Environment.SESSION_FACTORY_NAME);
settings.setSessionFactoryName(sessionFactoryName);
//JDBC and connection settings:
//Interrogate JDBC metadata
ExtractedDatabaseMetaData meta = jdbcServices.getExtractedMetaDataSupport();
settings.setDataDefinitionImplicitCommit( meta.doesDataDefinitionCauseTransactionCommit() );
settings.setDataDefinitionInTransactionSupported( meta.supportsDataDefinitionInTransaction() );
//use dialect default properties
final Properties properties = new Properties();
properties.putAll( jdbcServices.getDialect().getDefaultProperties() );
properties.putAll( props );
// Transaction settings:
settings.setJtaPlatform( serviceRegistry.getService( JtaPlatform.class ) );
boolean flushBeforeCompletion = ConfigurationHelper.getBoolean(Environment.FLUSH_BEFORE_COMPLETION, properties);
if ( debugEnabled ) {
LOG.debugf( "Automatic flush during beforeCompletion(): %s", enabledDisabled(flushBeforeCompletion) );
}
settings.setFlushBeforeCompletionEnabled(flushBeforeCompletion);
boolean autoCloseSession = ConfigurationHelper.getBoolean(Environment.AUTO_CLOSE_SESSION, properties);
if ( debugEnabled ) {
LOG.debugf( "Automatic session close at end of transaction: %s", enabledDisabled(autoCloseSession) );
}
settings.setAutoCloseSessionEnabled(autoCloseSession);
//JDBC and connection settings:
int batchSize = ConfigurationHelper.getInt(Environment.STATEMENT_BATCH_SIZE, properties, 0);
if ( !meta.supportsBatchUpdates() ) {
batchSize = 0;
}
if ( batchSize > 0 && debugEnabled ) {
LOG.debugf( "JDBC batch size: %s", batchSize );
}
settings.setJdbcBatchSize(batchSize);
boolean jdbcBatchVersionedData = ConfigurationHelper.getBoolean(Environment.BATCH_VERSIONED_DATA, properties, false);
if ( batchSize > 0 && debugEnabled ) {
LOG.debugf( "JDBC batch updates for versioned data: %s", enabledDisabled(jdbcBatchVersionedData) );
}
settings.setJdbcBatchVersionedData(jdbcBatchVersionedData);
boolean useScrollableResultSets = ConfigurationHelper.getBoolean(
Environment.USE_SCROLLABLE_RESULTSET,
properties,
meta.supportsScrollableResults()
);
if ( debugEnabled ) {
LOG.debugf( "Scrollable result sets: %s", enabledDisabled(useScrollableResultSets) );
}
settings.setScrollableResultSetsEnabled(useScrollableResultSets);
boolean wrapResultSets = ConfigurationHelper.getBoolean(Environment.WRAP_RESULT_SETS, properties, false);
if ( debugEnabled ) {
LOG.debugf( "Wrap result sets: %s", enabledDisabled(wrapResultSets) );
}
settings.setWrapResultSetsEnabled(wrapResultSets);
boolean useGetGeneratedKeys = ConfigurationHelper.getBoolean(Environment.USE_GET_GENERATED_KEYS, properties, meta.supportsGetGeneratedKeys());
if ( debugEnabled ) {
LOG.debugf( "JDBC3 getGeneratedKeys(): %s", enabledDisabled(useGetGeneratedKeys) );
}
settings.setGetGeneratedKeysEnabled(useGetGeneratedKeys);
Integer statementFetchSize = ConfigurationHelper.getInteger(Environment.STATEMENT_FETCH_SIZE, properties);
if ( statementFetchSize != null && debugEnabled ) {
LOG.debugf( "JDBC result set fetch size: %s", statementFetchSize );
}
settings.setJdbcFetchSize(statementFetchSize);
String releaseModeName = ConfigurationHelper.getString( Environment.RELEASE_CONNECTIONS, properties, "auto" );
if ( debugEnabled ) {
LOG.debugf( "Connection release mode: %s", releaseModeName );
}
ConnectionReleaseMode releaseMode;
if ( "auto".equals(releaseModeName) ) {
releaseMode = serviceRegistry.getService( TransactionFactory.class ).getDefaultReleaseMode();
}
else {
releaseMode = ConnectionReleaseMode.parse( releaseModeName );
if ( releaseMode == ConnectionReleaseMode.AFTER_STATEMENT &&
! jdbcServices.getConnectionProvider().supportsAggressiveRelease() ) {
LOG.unsupportedAfterStatement();
releaseMode = ConnectionReleaseMode.AFTER_TRANSACTION;
}
}
settings.setConnectionReleaseMode( releaseMode );