HHH-5765 : Wire in dialect factory and resolvers from service registry
This commit is contained in:
parent
88543c7a58
commit
91d444423b
|
@ -1840,7 +1840,7 @@ public class Configuration implements Serializable {
|
|||
Properties copy = new Properties();
|
||||
copy.putAll( properties );
|
||||
ConfigurationHelper.resolvePlaceHolders( copy );
|
||||
Settings settings = buildSettings( copy, serviceRegistry.getService( JdbcServices.class ).getConnectionProvider() );
|
||||
Settings settings = buildSettings( copy, serviceRegistry.getService( JdbcServices.class ) );
|
||||
|
||||
return new SessionFactoryImpl(
|
||||
this,
|
||||
|
@ -2823,18 +2823,18 @@ public class Configuration implements Serializable {
|
|||
*
|
||||
* @return The build settings
|
||||
*/
|
||||
public Settings buildSettings(ConnectionProvider connectionProvider) {
|
||||
public Settings buildSettings(JdbcServices jdbcServices) {
|
||||
Properties clone = ( Properties ) properties.clone();
|
||||
ConfigurationHelper.resolvePlaceHolders( clone );
|
||||
return buildSettingsInternal( clone, connectionProvider );
|
||||
return buildSettingsInternal( clone, jdbcServices );
|
||||
}
|
||||
|
||||
public Settings buildSettings(Properties props, ConnectionProvider connectionProvider) throws HibernateException {
|
||||
return buildSettingsInternal( props, connectionProvider );
|
||||
public Settings buildSettings(Properties props, JdbcServices jdbcServices) throws HibernateException {
|
||||
return buildSettingsInternal( props, jdbcServices );
|
||||
}
|
||||
|
||||
private Settings buildSettingsInternal(Properties props, ConnectionProvider connectionProvider) {
|
||||
final Settings settings = settingsFactory.buildSettings( props, connectionProvider );
|
||||
private Settings buildSettingsInternal(Properties props, JdbcServices jdbcServices) {
|
||||
final Settings settings = settingsFactory.buildSettings( props, jdbcServices );
|
||||
settings.setEntityTuplizerFactory( this.getEntityTuplizerFactory() );
|
||||
// settings.setComponentTuplizerFactory( this.getComponentTuplizerFactory() );
|
||||
return settings;
|
||||
|
|
|
@ -239,8 +239,8 @@ public final class Environment {
|
|||
public static final String DIALECT ="hibernate.dialect";
|
||||
|
||||
/**
|
||||
* {@link org.hibernate.dialect.resolver.DialectResolver} classes to register with the
|
||||
* {@link org.hibernate.dialect.resolver.DialectFactory}
|
||||
* {@link org.hibernate.service.jdbc.dialect.spi.DialectResolver} classes to register with the
|
||||
* {@link org.hibernate.service.jdbc.dialect.spi.DialectFactory}
|
||||
*/
|
||||
public static final String DIALECT_RESOLVERS = "hibernate.dialect_resolvers";
|
||||
|
||||
|
|
|
@ -24,10 +24,6 @@
|
|||
package org.hibernate.cfg;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
|
@ -43,9 +39,9 @@ import org.hibernate.cache.QueryCacheFactory;
|
|||
import org.hibernate.cache.RegionFactory;
|
||||
import org.hibernate.cache.impl.NoCachingRegionFactory;
|
||||
import org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge;
|
||||
import org.hibernate.service.jdbc.connections.spi.ConnectionProvider;
|
||||
import org.hibernate.engine.jdbc.spi.ExtractedDatabaseMetaData;
|
||||
import org.hibernate.engine.jdbc.spi.JdbcServices;
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.dialect.resolver.DialectFactory;
|
||||
import org.hibernate.exception.SQLExceptionConverter;
|
||||
import org.hibernate.exception.SQLExceptionConverterFactory;
|
||||
import org.hibernate.hql.QueryTranslatorFactory;
|
||||
|
@ -75,7 +71,7 @@ public class SettingsFactory implements Serializable {
|
|||
protected SettingsFactory() {
|
||||
}
|
||||
|
||||
public Settings buildSettings(Properties props, ConnectionProvider connections) {
|
||||
public Settings buildSettings(Properties props, JdbcServices jdbcServices) {
|
||||
Settings settings = new Settings();
|
||||
|
||||
//SessionFactory name:
|
||||
|
@ -87,69 +83,11 @@ public class SettingsFactory implements Serializable {
|
|||
|
||||
//Interrogate JDBC metadata
|
||||
|
||||
boolean metaSupportsScrollable = false;
|
||||
boolean metaSupportsGetGeneratedKeys = false;
|
||||
boolean metaSupportsBatchUpdates = false;
|
||||
boolean metaReportsDDLCausesTxnCommit = false;
|
||||
boolean metaReportsDDLInTxnSupported = true;
|
||||
Dialect dialect = null;
|
||||
Dialect dialect = jdbcServices.getDialect();
|
||||
ExtractedDatabaseMetaData meta = jdbcServices.getExtractedMetaDataSupport();
|
||||
|
||||
// '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", 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 );
|
||||
|
||||
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.setDataDefinitionImplicitCommit( meta.doesDataDefinitionCauseTransactionCommit() );
|
||||
settings.setDataDefinitionInTransactionSupported( meta.supportsDataDefinitionInTransaction() );
|
||||
settings.setDialect( dialect );
|
||||
|
||||
//use dialect default properties
|
||||
|
@ -176,7 +114,7 @@ public class SettingsFactory implements Serializable {
|
|||
//JDBC and connection settings:
|
||||
|
||||
int batchSize = ConfigurationHelper.getInt(Environment.STATEMENT_BATCH_SIZE, properties, 0);
|
||||
if ( !metaSupportsBatchUpdates ) batchSize = 0;
|
||||
if ( !meta.supportsBatchUpdates() ) batchSize = 0;
|
||||
if (batchSize>0) log.info("JDBC batch size: " + batchSize);
|
||||
settings.setJdbcBatchSize(batchSize);
|
||||
boolean jdbcBatchVersionedData = ConfigurationHelper.getBoolean(Environment.BATCH_VERSIONED_DATA, properties, false);
|
||||
|
@ -184,7 +122,7 @@ public class SettingsFactory implements Serializable {
|
|||
settings.setJdbcBatchVersionedData(jdbcBatchVersionedData);
|
||||
settings.setBatcherFactory( createBatcherFactory(properties, batchSize) );
|
||||
|
||||
boolean useScrollableResultSets = ConfigurationHelper.getBoolean(Environment.USE_SCROLLABLE_RESULTSET, properties, metaSupportsScrollable);
|
||||
boolean useScrollableResultSets = ConfigurationHelper.getBoolean(Environment.USE_SCROLLABLE_RESULTSET, properties, meta.supportsScrollableResults());
|
||||
log.info("Scrollable result sets: " + enabledDisabled(useScrollableResultSets) );
|
||||
settings.setScrollableResultSetsEnabled(useScrollableResultSets);
|
||||
|
||||
|
@ -192,7 +130,7 @@ public class SettingsFactory implements Serializable {
|
|||
log.debug( "Wrap result sets: " + enabledDisabled(wrapResultSets) );
|
||||
settings.setWrapResultSetsEnabled(wrapResultSets);
|
||||
|
||||
boolean useGetGeneratedKeys = ConfigurationHelper.getBoolean(Environment.USE_GET_GENERATED_KEYS, properties, metaSupportsGetGeneratedKeys);
|
||||
boolean useGetGeneratedKeys = ConfigurationHelper.getBoolean(Environment.USE_GET_GENERATED_KEYS, properties, meta.supportsGetGeneratedKeys());
|
||||
log.info("JDBC3 getGeneratedKeys(): " + enabledDisabled(useGetGeneratedKeys) );
|
||||
settings.setGetGeneratedKeysEnabled(useGetGeneratedKeys);
|
||||
|
||||
|
@ -208,7 +146,8 @@ public class SettingsFactory implements Serializable {
|
|||
}
|
||||
else {
|
||||
releaseMode = ConnectionReleaseMode.parse( releaseModeName );
|
||||
if ( releaseMode == ConnectionReleaseMode.AFTER_STATEMENT && !connections.supportsAggressiveRelease() ) {
|
||||
if ( releaseMode == ConnectionReleaseMode.AFTER_STATEMENT &&
|
||||
! jdbcServices.getConnectionProvider().supportsAggressiveRelease() ) {
|
||||
log.warn( "Overriding release mode as connection provider does not support 'after_statement'" );
|
||||
releaseMode = ConnectionReleaseMode.AFTER_TRANSACTION;
|
||||
}
|
||||
|
|
|
@ -1,79 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.dialect.resolver;
|
||||
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.exception.JDBCConnectionException;
|
||||
import org.hibernate.JDBCException;
|
||||
|
||||
/**
|
||||
* A templated resolver impl which delegates to the {@link #resolveDialectInternal} method
|
||||
* and handles any thrown {@link SQLException}s.
|
||||
*
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public abstract class AbstractDialectResolver implements DialectResolver {
|
||||
private static final Logger log = LoggerFactory.getLogger( AbstractDialectResolver.class );
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* <p/>
|
||||
* Here we template the resolution, delegating to {@link #resolveDialectInternal} and handling
|
||||
* {@link java.sql.SQLException}s properly.
|
||||
*/
|
||||
public final Dialect resolveDialect(DatabaseMetaData metaData) {
|
||||
try {
|
||||
return resolveDialectInternal( metaData );
|
||||
}
|
||||
catch ( SQLException sqlException ) {
|
||||
JDBCException jdbcException = BasicSQLExceptionConverter.INSTANCE.convert( sqlException );
|
||||
if ( jdbcException instanceof JDBCConnectionException ) {
|
||||
throw jdbcException;
|
||||
}
|
||||
else {
|
||||
log.warn( BasicSQLExceptionConverter.MSG + " : " + sqlException.getMessage() );
|
||||
return null;
|
||||
}
|
||||
}
|
||||
catch ( Throwable t ) {
|
||||
log.warn( "Error executing resolver [" + this + "] : " + t.getMessage() );
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform the actual resolution without caring about handling {@link SQLException}s.
|
||||
*
|
||||
* @param metaData The database metadata
|
||||
* @return The resolved dialect, or null if we could not resolve.
|
||||
* @throws SQLException Indicates problems accessing the metadata.
|
||||
*/
|
||||
protected abstract Dialect resolveDialectInternal(DatabaseMetaData metaData) throws SQLException;
|
||||
}
|
|
@ -1,165 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.dialect.resolver;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.dialect.resolver.DialectResolver;
|
||||
import org.hibernate.dialect.resolver.DialectResolverSet;
|
||||
import org.hibernate.dialect.resolver.StandardDialectResolver;
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.util.ReflectHelper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* A factory for generating Dialect instances.
|
||||
*
|
||||
* @author Steve Ebersole
|
||||
* @author Tomoto Shimizu Washio
|
||||
*/
|
||||
public class DialectFactory {
|
||||
private static final Logger log = LoggerFactory.getLogger( DialectFactory.class );
|
||||
|
||||
private static DialectResolverSet DIALECT_RESOLVERS = new DialectResolverSet();
|
||||
|
||||
static {
|
||||
// register the standard dialect resolver
|
||||
DIALECT_RESOLVERS.addResolver( new StandardDialectResolver() );
|
||||
|
||||
// register resolvers set via Environment property
|
||||
String userSpecifedResolverSetting = Environment.getProperties().getProperty( Environment.DIALECT_RESOLVERS );
|
||||
if ( userSpecifedResolverSetting != null ) {
|
||||
String[] userSpecifedResolvers = userSpecifedResolverSetting.split( "\\s+" );
|
||||
for ( int i = 0; i < userSpecifedResolvers.length; i++ ) {
|
||||
registerDialectResolver( userSpecifedResolvers[i] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*package*/ static void registerDialectResolver(String resolverName) {
|
||||
try {
|
||||
DialectResolver resolver = ( DialectResolver ) ReflectHelper.classForName( resolverName ).newInstance();
|
||||
DIALECT_RESOLVERS.addResolverAtFirst( resolver );
|
||||
}
|
||||
catch ( ClassNotFoundException cnfe ) {
|
||||
log.warn( "Dialect resolver class not found: " + resolverName );
|
||||
}
|
||||
catch ( Exception e ) {
|
||||
log.warn( "Could not instantiate dialect resolver class", e );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds an appropriate Dialect instance.
|
||||
* <p/>
|
||||
* If a dialect is explicitly named in the incoming properties, it is used. Otherwise, it is
|
||||
* determined by dialect resolvers based on the passed connection.
|
||||
* <p/>
|
||||
* An exception is thrown if a dialect was not explicitly set and no resolver could make
|
||||
* the determination from the given connection.
|
||||
*
|
||||
* @param properties The configuration properties.
|
||||
* @param connection The configured connection.
|
||||
* @return The appropriate dialect instance.
|
||||
* @throws HibernateException No dialect specified and no resolver could make the determination.
|
||||
*/
|
||||
public static Dialect buildDialect(Properties properties, Connection connection) throws HibernateException {
|
||||
String dialectName = properties.getProperty( Environment.DIALECT );
|
||||
if ( dialectName == null ) {
|
||||
return determineDialect( connection );
|
||||
}
|
||||
else {
|
||||
return constructDialect( dialectName );
|
||||
}
|
||||
}
|
||||
|
||||
public static Dialect buildDialect(Properties properties) {
|
||||
String dialectName = properties.getProperty( Environment.DIALECT );
|
||||
if ( dialectName == null ) {
|
||||
throw new HibernateException( "'hibernate.dialect' must be set when no Connection available" );
|
||||
}
|
||||
return constructDialect( dialectName );
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine the appropriate Dialect to use given the connection.
|
||||
*
|
||||
* @param connection The configured connection.
|
||||
* @return The appropriate dialect instance.
|
||||
*
|
||||
* @throws HibernateException No connection given or no resolver could make
|
||||
* the determination from the given connection.
|
||||
*/
|
||||
private static Dialect determineDialect(Connection connection) {
|
||||
if ( connection == null ) {
|
||||
throw new HibernateException( "Connection cannot be null when 'hibernate.dialect' not set" );
|
||||
}
|
||||
|
||||
try {
|
||||
final DatabaseMetaData databaseMetaData = connection.getMetaData();
|
||||
final Dialect dialect = DIALECT_RESOLVERS.resolveDialect( databaseMetaData );
|
||||
|
||||
if ( dialect == null ) {
|
||||
throw new HibernateException(
|
||||
"Unable to determine Dialect to use [name=" + databaseMetaData.getDatabaseProductName() +
|
||||
", majorVersion=" + databaseMetaData.getDatabaseMajorVersion() +
|
||||
"]; user must register resolver or explicitly set 'hibernate.dialect'"
|
||||
);
|
||||
}
|
||||
|
||||
return dialect;
|
||||
}
|
||||
catch ( SQLException sqlException ) {
|
||||
throw new HibernateException(
|
||||
"Unable to access java.sql.DatabaseMetaData to determine appropriate Dialect to use",
|
||||
sqlException
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a dialect instance given the name of the class to use.
|
||||
*
|
||||
* @param dialectName The name of the dialect class.
|
||||
*
|
||||
* @return The dialect instance.
|
||||
*/
|
||||
public static Dialect constructDialect(String dialectName) {
|
||||
try {
|
||||
return ( Dialect ) ReflectHelper.classForName( dialectName ).newInstance();
|
||||
}
|
||||
catch ( ClassNotFoundException cnfe ) {
|
||||
throw new HibernateException( "Dialect class not found: " + dialectName, cnfe );
|
||||
}
|
||||
catch ( Exception e ) {
|
||||
throw new HibernateException( "Could not instantiate dialect class", e );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.dialect.resolver;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.exception.JDBCConnectionException;
|
||||
|
||||
/**
|
||||
* Contract for determining the {@link Dialect} to use based on a JDBC {@link Connection}.
|
||||
*
|
||||
* @author Tomoto Shimizu Washio
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public interface DialectResolver {
|
||||
/**
|
||||
* Determine the {@link Dialect} to use based on the given JDBC {@link DatabaseMetaData}. Implementations are
|
||||
* expected to return the {@link Dialect} instance to use, or null if the {@link DatabaseMetaData} does not match
|
||||
* the criteria handled by this impl.
|
||||
*
|
||||
* @param metaData The JDBC metadata.
|
||||
* @return The dialect to use, or null.
|
||||
* @throws JDBCConnectionException Indicates a 'non transient connection problem', which indicates that
|
||||
* we should stop resolution attempts.
|
||||
*/
|
||||
public Dialect resolveDialect(DatabaseMetaData metaData) throws JDBCConnectionException;
|
||||
}
|
|
@ -1,91 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.dialect.resolver;
|
||||
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.exception.JDBCConnectionException;
|
||||
|
||||
/**
|
||||
* A {@link DialectResolver} implementation which coordinates resolution by delegating to its
|
||||
* registered sub-resolvers. Sub-resolvers may be registered by calling either {@link #addResolver} or
|
||||
* {@link #addResolverAtFirst}.
|
||||
*
|
||||
* @author Tomoto Shimizu Washio
|
||||
*/
|
||||
public class DialectResolverSet implements DialectResolver {
|
||||
private static Logger log = LoggerFactory.getLogger( DialectResolverSet.class );
|
||||
|
||||
private List resolvers = new ArrayList();
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public Dialect resolveDialect(DatabaseMetaData metaData) {
|
||||
Iterator i = resolvers.iterator();
|
||||
while ( i.hasNext() ) {
|
||||
final DialectResolver resolver = ( DialectResolver ) i.next();
|
||||
try {
|
||||
Dialect dialect = resolver.resolveDialect( metaData );
|
||||
if ( dialect != null ) {
|
||||
return dialect;
|
||||
}
|
||||
}
|
||||
catch ( JDBCConnectionException e ) {
|
||||
throw e;
|
||||
}
|
||||
catch ( Throwable t ) {
|
||||
log.info( "sub-resolver threw unexpected exception, continuing to next : " + t.getMessage() );
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a resolver at the end of the underlying resolver list. The resolver added by this method is at lower
|
||||
* priority than any other existing resolvers.
|
||||
*
|
||||
* @param resolver The resolver to add.
|
||||
*/
|
||||
public void addResolver(DialectResolver resolver) {
|
||||
resolvers.add( resolver );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a resolver at the beginning of the underlying resolver list. The resolver added by this method is at higher
|
||||
* priority than any other existing resolvers.
|
||||
*
|
||||
* @param resolver The resolver to add.
|
||||
*/
|
||||
public void addResolverAtFirst(DialectResolver resolver) {
|
||||
resolvers.add( 0, resolver );
|
||||
}
|
||||
}
|
|
@ -1,137 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.dialect.resolver;
|
||||
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.dialect.HSQLDialect;
|
||||
import org.hibernate.dialect.H2Dialect;
|
||||
import org.hibernate.dialect.MySQLDialect;
|
||||
import org.hibernate.dialect.PostgreSQLDialect;
|
||||
import org.hibernate.dialect.DerbyDialect;
|
||||
import org.hibernate.dialect.Ingres10Dialect;
|
||||
import org.hibernate.dialect.Ingres9Dialect;
|
||||
import org.hibernate.dialect.IngresDialect;
|
||||
import org.hibernate.dialect.SQLServerDialect;
|
||||
import org.hibernate.dialect.InformixDialect;
|
||||
import org.hibernate.dialect.DB2Dialect;
|
||||
import org.hibernate.dialect.Oracle10gDialect;
|
||||
import org.hibernate.dialect.Oracle9iDialect;
|
||||
import org.hibernate.dialect.Oracle8iDialect;
|
||||
import org.hibernate.dialect.SybaseAnywhereDialect;
|
||||
import org.hibernate.dialect.SybaseASE15Dialect;
|
||||
|
||||
/**
|
||||
* The standard Hibernate resolver.
|
||||
*
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class StandardDialectResolver extends AbstractDialectResolver{
|
||||
private static final Logger log = LoggerFactory.getLogger( StandardDialectResolver.class );
|
||||
|
||||
protected Dialect resolveDialectInternal(DatabaseMetaData metaData) throws SQLException {
|
||||
String databaseName = metaData.getDatabaseProductName();
|
||||
int databaseMajorVersion = metaData.getDatabaseMajorVersion();
|
||||
|
||||
if ( "HSQL Database Engine".equals( databaseName ) ) {
|
||||
return new HSQLDialect();
|
||||
}
|
||||
|
||||
if ( "H2".equals( databaseName ) ) {
|
||||
return new H2Dialect();
|
||||
}
|
||||
|
||||
if ( "MySQL".equals( databaseName ) ) {
|
||||
return new MySQLDialect();
|
||||
}
|
||||
|
||||
if ( "PostgreSQL".equals( databaseName ) ) {
|
||||
return new PostgreSQLDialect();
|
||||
}
|
||||
|
||||
if ( "Apache Derby".equals( databaseName ) ) {
|
||||
return new DerbyDialect();
|
||||
}
|
||||
|
||||
if ( "ingres".equalsIgnoreCase( databaseName ) ) {
|
||||
switch( databaseMajorVersion ) {
|
||||
case 9:
|
||||
int databaseMinorVersion = metaData.getDatabaseMinorVersion();
|
||||
if (databaseMinorVersion > 2) {
|
||||
return new Ingres9Dialect();
|
||||
}
|
||||
return new IngresDialect();
|
||||
case 10:
|
||||
log.warn( "Ingres " + databaseMajorVersion + " is not yet fully supported; using Ingres 9.3 dialect" );
|
||||
return new Ingres10Dialect();
|
||||
default:
|
||||
log.warn( "Unknown Ingres major version [" + databaseMajorVersion + "] using Ingres 9.2 dialect" );
|
||||
}
|
||||
return new IngresDialect();
|
||||
}
|
||||
|
||||
if ( databaseName.startsWith( "Microsoft SQL Server" ) ) {
|
||||
return new SQLServerDialect();
|
||||
}
|
||||
|
||||
if ( "Sybase SQL Server".equals( databaseName ) || "Adaptive Server Enterprise".equals( databaseName ) ) {
|
||||
return new SybaseASE15Dialect();
|
||||
}
|
||||
|
||||
if ( databaseName.startsWith( "Adaptive Server Anywhere" ) ) {
|
||||
return new SybaseAnywhereDialect();
|
||||
}
|
||||
|
||||
if ( "Informix Dynamic Server".equals( databaseName ) ) {
|
||||
return new InformixDialect();
|
||||
}
|
||||
|
||||
if ( databaseName.startsWith( "DB2/" ) ) {
|
||||
return new DB2Dialect();
|
||||
}
|
||||
|
||||
if ( "Oracle".equals( databaseName ) ) {
|
||||
switch ( databaseMajorVersion ) {
|
||||
case 11:
|
||||
log.warn( "Oracle 11g is not yet fully supported; using 10g dialect" );
|
||||
return new Oracle10gDialect();
|
||||
case 10:
|
||||
return new Oracle10gDialect();
|
||||
case 9:
|
||||
return new Oracle9iDialect();
|
||||
case 8:
|
||||
return new Oracle8iDialect();
|
||||
default:
|
||||
log.warn( "unknown Oracle major version [" + databaseMajorVersion + "]" );
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -40,6 +40,7 @@ import org.hibernate.Query;
|
|||
import org.hibernate.ScrollMode;
|
||||
import org.hibernate.ScrollableResults;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.engine.jdbc.spi.JdbcServices;
|
||||
import org.hibernate.engine.query.sql.NativeSQLQuerySpecification;
|
||||
import org.hibernate.collection.PersistentCollection;
|
||||
import org.hibernate.event.EventListeners;
|
||||
|
|
|
@ -198,7 +198,7 @@ public abstract class AbstractStatementExecutor implements StatementExecutor {
|
|||
if ( getFactory().getDialect().dropTemporaryTableAfterUse() ) {
|
||||
IsolatedWork work = new IsolatedWork() {
|
||||
public void doWork(Connection connection) throws HibernateException {
|
||||
final String command = session.getFactory().getSettings().getDialect().getDropTemporaryTableString()
|
||||
final String command = session.getFactory().getDialect().getDropTemporaryTableString()
|
||||
+ ' ' + persister.getTemporaryIdTableName();
|
||||
try {
|
||||
Statement statement = connection.createStatement();
|
||||
|
|
|
@ -212,7 +212,7 @@ public final class SessionFactoryImpl implements SessionFactory, SessionFactoryI
|
|||
this.interceptor = cfg.getInterceptor();
|
||||
this.serviceRegistry = serviceRegistry;
|
||||
this.settings = settings;
|
||||
this.sqlFunctionRegistry = new SQLFunctionRegistry(settings.getDialect(), cfg.getSqlFunctions());
|
||||
this.sqlFunctionRegistry = new SQLFunctionRegistry( getDialect(), cfg.getSqlFunctions() );
|
||||
this.eventListeners = listeners;
|
||||
this.observer = observer != null ? observer : new SessionFactoryObserver() {
|
||||
public void sessionFactoryCreated(SessionFactory factory) {
|
||||
|
@ -250,7 +250,7 @@ public final class SessionFactoryImpl implements SessionFactory, SessionFactoryI
|
|||
if ( !model.isInherited() ) {
|
||||
IdentifierGenerator generator = model.getIdentifier().createIdentifierGenerator(
|
||||
cfg.getIdentifierGeneratorFactory(),
|
||||
settings.getDialect(),
|
||||
getDialect(),
|
||||
settings.getDefaultCatalogName(),
|
||||
settings.getDefaultSchemaName(),
|
||||
(RootClass) model
|
||||
|
@ -272,7 +272,7 @@ public final class SessionFactoryImpl implements SessionFactory, SessionFactoryI
|
|||
classes = cfg.getClassMappings();
|
||||
while ( classes.hasNext() ) {
|
||||
final PersistentClass model = (PersistentClass) classes.next();
|
||||
model.prepareTemporaryTables( mapping, settings.getDialect() );
|
||||
model.prepareTemporaryTables( mapping, getDialect() );
|
||||
final String cacheRegionName = cacheRegionPrefix + model.getRootClass().getCacheRegionName();
|
||||
// cache region is defined by the root-class in the hierarchy...
|
||||
EntityRegionAccessStrategy accessStrategy = ( EntityRegionAccessStrategy ) entityAccessStrategies.get( cacheRegionName );
|
||||
|
@ -711,7 +711,10 @@ public final class SessionFactoryImpl implements SessionFactory, SessionFactoryI
|
|||
}
|
||||
|
||||
public Dialect getDialect() {
|
||||
return settings.getDialect();
|
||||
if ( serviceRegistry == null ) {
|
||||
throw new IllegalStateException( "Cannot determine dialect because serviceRegistry is null." );
|
||||
}
|
||||
return serviceRegistry.getService( JdbcServices.class ).getDialect();
|
||||
}
|
||||
|
||||
public Interceptor getInterceptor()
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.dialect.resolver;
|
||||
package org.hibernate.service.jdbc.dialect.internal;
|
||||
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.SQLException;
|
||||
|
@ -35,6 +35,8 @@ import org.hibernate.HibernateException;
|
|||
* @author Steve Ebersole
|
||||
*/
|
||||
public class BasicDialectResolver extends AbstractDialectResolver {
|
||||
// TODO: should this disappear???
|
||||
|
||||
public static final int VERSION_INSENSITIVE_VERSION = -9999;
|
||||
|
||||
private final String matchingName;
|
|
@ -27,9 +27,9 @@ package org.hibernate.dialect;
|
|||
import java.sql.SQLException;
|
||||
import java.sql.DatabaseMetaData;
|
||||
|
||||
import org.hibernate.dialect.resolver.AbstractDialectResolver;
|
||||
import org.hibernate.dialect.resolver.BasicDialectResolver;
|
||||
import org.hibernate.service.jdbc.dialect.internal.BasicDialectResolver;
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.service.jdbc.dialect.internal.AbstractDialectResolver;
|
||||
|
||||
/**
|
||||
* TODO : javadoc
|
||||
|
|
|
@ -49,6 +49,11 @@ import org.hibernate.dialect.Mocks;
|
|||
import org.hibernate.dialect.SybaseASE15Dialect;
|
||||
import org.hibernate.dialect.SybaseAnywhereDialect;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl;
|
||||
import org.hibernate.service.jdbc.dialect.internal.DialectResolverSet;
|
||||
import org.hibernate.service.jdbc.dialect.internal.StandardDialectResolver;
|
||||
import org.hibernate.service.jdbc.dialect.spi.DialectResolver;
|
||||
import org.hibernate.test.common.ServiceRegistryHolder;
|
||||
|
||||
/**
|
||||
* TODO : javadoc
|
||||
|
@ -56,14 +61,28 @@ import org.hibernate.cfg.Environment;
|
|||
* @author Steve Ebersole
|
||||
*/
|
||||
public class DialectFactoryTest extends TestCase {
|
||||
private ServiceRegistryHolder serviceRegistryHolder;
|
||||
|
||||
public DialectFactoryTest(String name) {
|
||||
super( name );
|
||||
}
|
||||
|
||||
protected void setUp() {
|
||||
serviceRegistryHolder = new ServiceRegistryHolder( Environment.getProperties() );
|
||||
}
|
||||
|
||||
protected void tearDown() {
|
||||
if ( serviceRegistryHolder != null ) {
|
||||
serviceRegistryHolder.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
return new TestSuite( DialectFactoryTest.class );
|
||||
}
|
||||
|
||||
// TODO: is it still possible to build a dialect using a class name???
|
||||
/*
|
||||
public void testBuildDialectByClass() {
|
||||
assertEquals(
|
||||
HSQLDialect.class,
|
||||
|
@ -86,12 +105,13 @@ public class DialectFactoryTest extends TestCase {
|
|||
assertEquals( "unexpected exception type", e.getCause().getClass(), ClassCastException.class );
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
public void testBuildDialectByProperties() {
|
||||
Properties props = new Properties();
|
||||
|
||||
try {
|
||||
DialectFactory.buildDialect( props, null );
|
||||
getDialectFactoryImpl( new StandardDialectResolver() ).buildDialect( props, null );
|
||||
fail();
|
||||
}
|
||||
catch ( HibernateException e ) {
|
||||
|
@ -99,57 +119,65 @@ public class DialectFactoryTest extends TestCase {
|
|||
}
|
||||
|
||||
props.setProperty( Environment.DIALECT, "org.hibernate.dialect.HSQLDialect" );
|
||||
assertTrue( DialectFactory.buildDialect( props, null ) instanceof HSQLDialect );
|
||||
assertTrue( getDialectFactoryImpl( new StandardDialectResolver() ).buildDialect( props, null ) instanceof HSQLDialect );
|
||||
}
|
||||
|
||||
private DialectFactoryImpl getDialectFactoryImpl(DialectResolver dialectResolver) {
|
||||
DialectFactoryImpl dialectFactoryImpl = new DialectFactoryImpl();
|
||||
dialectFactoryImpl.setClassLoaderService( serviceRegistryHolder.getClassLoaderService() );
|
||||
dialectFactoryImpl.setDialectResolver( dialectResolver );
|
||||
return dialectFactoryImpl;
|
||||
}
|
||||
|
||||
public void testPreregisteredDialects() {
|
||||
testDetermination( "HSQL Database Engine", HSQLDialect.class );
|
||||
testDetermination( "H2", H2Dialect.class );
|
||||
testDetermination( "MySQL", MySQLDialect.class );
|
||||
testDetermination( "PostgreSQL", PostgreSQLDialect.class );
|
||||
testDetermination( "Apache Derby", DerbyDialect.class );
|
||||
testDetermination( "Ingres", IngresDialect.class );
|
||||
testDetermination( "ingres", IngresDialect.class );
|
||||
testDetermination( "INGRES", IngresDialect.class );
|
||||
testDetermination( "Microsoft SQL Server Database", SQLServerDialect.class );
|
||||
testDetermination( "Microsoft SQL Server", SQLServerDialect.class );
|
||||
testDetermination( "Sybase SQL Server", SybaseASE15Dialect.class );
|
||||
testDetermination( "Adaptive Server Enterprise", SybaseASE15Dialect.class );
|
||||
testDetermination( "Adaptive Server Anywhere", SybaseAnywhereDialect.class );
|
||||
testDetermination( "Informix Dynamic Server", InformixDialect.class );
|
||||
testDetermination( "DB2/NT", DB2Dialect.class );
|
||||
testDetermination( "DB2/LINUX", DB2Dialect.class );
|
||||
testDetermination( "DB2/6000", DB2Dialect.class );
|
||||
testDetermination( "DB2/HPUX", DB2Dialect.class );
|
||||
testDetermination( "DB2/SUN", DB2Dialect.class );
|
||||
testDetermination( "DB2/LINUX390", DB2Dialect.class );
|
||||
testDetermination( "DB2/AIX64", DB2Dialect.class );
|
||||
testDetermination( "Oracle", 8, Oracle8iDialect.class );
|
||||
testDetermination( "Oracle", 9, Oracle9iDialect.class );
|
||||
testDetermination( "Oracle", 10, Oracle10gDialect.class );
|
||||
testDetermination( "Oracle", 11, Oracle10gDialect.class );
|
||||
DialectResolver resolver = new StandardDialectResolver();
|
||||
testDetermination( "HSQL Database Engine", HSQLDialect.class, resolver );
|
||||
testDetermination( "H2", H2Dialect.class, resolver );
|
||||
testDetermination( "MySQL", MySQLDialect.class, resolver );
|
||||
testDetermination( "PostgreSQL", PostgreSQLDialect.class, resolver );
|
||||
testDetermination( "Apache Derby", DerbyDialect.class, resolver );
|
||||
testDetermination( "Ingres", IngresDialect.class, resolver );
|
||||
testDetermination( "ingres", IngresDialect.class, resolver );
|
||||
testDetermination( "INGRES", IngresDialect.class, resolver );
|
||||
testDetermination( "Microsoft SQL Server Database", SQLServerDialect.class, resolver );
|
||||
testDetermination( "Microsoft SQL Server", SQLServerDialect.class, resolver );
|
||||
testDetermination( "Sybase SQL Server", SybaseASE15Dialect.class, resolver );
|
||||
testDetermination( "Adaptive Server Enterprise", SybaseASE15Dialect.class, resolver );
|
||||
testDetermination( "Adaptive Server Anywhere", SybaseAnywhereDialect.class, resolver );
|
||||
testDetermination( "Informix Dynamic Server", InformixDialect.class, resolver );
|
||||
testDetermination( "DB2/NT", DB2Dialect.class, resolver );
|
||||
testDetermination( "DB2/LINUX", DB2Dialect.class, resolver );
|
||||
testDetermination( "DB2/6000", DB2Dialect.class, resolver );
|
||||
testDetermination( "DB2/HPUX", DB2Dialect.class, resolver );
|
||||
testDetermination( "DB2/SUN", DB2Dialect.class, resolver );
|
||||
testDetermination( "DB2/LINUX390", DB2Dialect.class, resolver );
|
||||
testDetermination( "DB2/AIX64", DB2Dialect.class, resolver );
|
||||
testDetermination( "Oracle", 8, Oracle8iDialect.class, resolver );
|
||||
testDetermination( "Oracle", 9, Oracle9iDialect.class, resolver );
|
||||
testDetermination( "Oracle", 10, Oracle10gDialect.class, resolver );
|
||||
testDetermination( "Oracle", 11, Oracle10gDialect.class, resolver );
|
||||
}
|
||||
|
||||
public void testCustomDialects() {
|
||||
DialectFactory.registerDialectResolver( TestingDialects.MyDialectResolver1.class.getName() );
|
||||
DialectFactory.registerDialectResolver( TestingDialects.MyDialectResolver2.class.getName() );
|
||||
DialectFactory.registerDialectResolver( TestingDialects.ErrorDialectResolver1.class.getName() );
|
||||
DialectFactory.registerDialectResolver( TestingDialects.ErrorDialectResolver2.class.getName() );
|
||||
DialectFactory.registerDialectResolver( TestingDialects.MyOverridingDialectResolver1.class.getName() );
|
||||
DialectFactory.registerDialectResolver( "org.hibernate.dialect.NoSuchDialectResolver" );
|
||||
DialectFactory.registerDialectResolver( "java.lang.Object" );
|
||||
DialectResolverSet resolvers = new DialectResolverSet();
|
||||
resolvers.addResolver( new TestingDialects.MyDialectResolver1() );
|
||||
resolvers.addResolver( new TestingDialects.MyDialectResolver2() );
|
||||
resolvers.addResolver( new TestingDialects.ErrorDialectResolver1() );
|
||||
resolvers.addResolver( new TestingDialects.ErrorDialectResolver2() );
|
||||
resolvers.addResolver( new TestingDialects.MyOverridingDialectResolver1() );
|
||||
//DialectFactory.registerDialectResolver( "org.hibernate.dialect.NoSuchDialectResolver" );
|
||||
//DialectFactory.registerDialectResolver( "java.lang.Object" );
|
||||
|
||||
|
||||
testDetermination( "MyDatabase1", TestingDialects.MyDialect1.class );
|
||||
testDetermination( "MyDatabase2", 1, TestingDialects.MyDialect21.class );
|
||||
testDetermination( "MyTrickyDatabase1", TestingDialects.MyDialect1.class );
|
||||
testDetermination( "MyDatabase1", TestingDialects.MyDialect1.class, resolvers );
|
||||
testDetermination( "MyDatabase2", 1, TestingDialects.MyDialect21.class, resolvers );
|
||||
testDetermination( "MyTrickyDatabase1", TestingDialects.MyDialect1.class, resolvers );
|
||||
|
||||
// This should be mapped to DB2Dialect by default, but actually it will be
|
||||
// my custom dialect because I have registered MyOverridingDialectResolver1.
|
||||
testDetermination( "DB2/MySpecialPlatform", TestingDialects.MySpecialDB2Dialect.class );
|
||||
testDetermination( "DB2/MySpecialPlatform", TestingDialects.MySpecialDB2Dialect.class, resolvers );
|
||||
|
||||
try {
|
||||
testDetermination( "ErrorDatabase1", Void.TYPE );
|
||||
testDetermination( "ErrorDatabase1", Void.TYPE, resolvers );
|
||||
fail();
|
||||
}
|
||||
catch ( HibernateException e ) {
|
||||
|
@ -157,7 +185,7 @@ public class DialectFactoryTest extends TestCase {
|
|||
}
|
||||
|
||||
try {
|
||||
testDetermination( "ErrorDatabase2", Void.TYPE );
|
||||
testDetermination( "ErrorDatabase2", Void.TYPE, resolvers );
|
||||
fail();
|
||||
}
|
||||
catch ( HibernateException e ) {
|
||||
|
@ -168,7 +196,7 @@ public class DialectFactoryTest extends TestCase {
|
|||
public void testDialectNotFound() {
|
||||
Properties properties = new Properties();
|
||||
try {
|
||||
DialectFactory.buildDialect( properties, Mocks.createConnection( "NoSuchDatabase", 666 ) );
|
||||
getDialectFactoryImpl( new StandardDialectResolver() ).buildDialect( properties, Mocks.createConnection( "NoSuchDatabase", 666 ) );
|
||||
fail();
|
||||
}
|
||||
catch ( HibernateException e ) {
|
||||
|
@ -176,13 +204,15 @@ public class DialectFactoryTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
private void testDetermination(String databaseName, Class clazz) {
|
||||
testDetermination( databaseName, -9999, clazz );
|
||||
private void testDetermination(String databaseName, Class clazz, DialectResolver resolver) {
|
||||
testDetermination( databaseName, -9999, clazz, resolver );
|
||||
}
|
||||
|
||||
private void testDetermination(String databaseName, int databaseMajorVersion, Class clazz) {
|
||||
private void testDetermination(String databaseName, int databaseMajorVersion, Class clazz, DialectResolver resolver) {
|
||||
DialectFactoryImpl dialectFactoryImpl = getDialectFactoryImpl( new StandardDialectResolver() );
|
||||
dialectFactoryImpl.setDialectResolver( resolver );
|
||||
Properties properties = new Properties();
|
||||
Connection conn = Mocks.createConnection( databaseName, databaseMajorVersion );
|
||||
assertEquals( clazz, DialectFactory.buildDialect( properties, conn ).getClass() );
|
||||
assertEquals( clazz, dialectFactoryImpl.buildDialect( properties, conn ).getClass() );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,6 +34,9 @@ import org.hibernate.dialect.Dialect;
|
|||
import org.hibernate.dialect.TestingDialects;
|
||||
import org.hibernate.dialect.Mocks;
|
||||
import org.hibernate.exception.JDBCConnectionException;
|
||||
import org.hibernate.service.jdbc.dialect.internal.BasicDialectResolver;
|
||||
import org.hibernate.service.jdbc.dialect.internal.DialectResolverSet;
|
||||
import org.hibernate.service.jdbc.dialect.spi.DialectResolver;
|
||||
|
||||
/**
|
||||
* TODO : javadoc
|
||||
|
|
|
@ -65,7 +65,7 @@ public abstract class AbstractEntityCollectionRegionTestCase extends AbstractReg
|
|||
String entityCfg = "entity";
|
||||
cfg.setProperty(InfinispanRegionFactory.ENTITY_CACHE_RESOURCE_PROP, entityCfg);
|
||||
InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(
|
||||
getConnectionProvider(), cfg, getCacheTestSupport()
|
||||
getJdbcServices(), cfg, getCacheTestSupport()
|
||||
);
|
||||
supportedAccessTypeTest(regionFactory, cfg.getProperties());
|
||||
}
|
||||
|
@ -85,7 +85,7 @@ public abstract class AbstractEntityCollectionRegionTestCase extends AbstractReg
|
|||
public void testIsTransactionAware() throws Exception {
|
||||
Configuration cfg = CacheTestUtil.buildConfiguration("test", InfinispanRegionFactory.class, true, false);
|
||||
InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(
|
||||
getConnectionProvider(), cfg, getCacheTestSupport()
|
||||
getJdbcServices(), cfg, getCacheTestSupport()
|
||||
);
|
||||
TransactionalDataRegion region = (TransactionalDataRegion) createRegion(regionFactory, "test/test", cfg.getProperties(), getCacheDataDescription());
|
||||
assertTrue("Region is transaction-aware", region.isTransactionAware());
|
||||
|
@ -94,7 +94,7 @@ public abstract class AbstractEntityCollectionRegionTestCase extends AbstractReg
|
|||
// Make it non-transactional
|
||||
cfg.getProperties().remove(Environment.TRANSACTION_MANAGER_STRATEGY);
|
||||
regionFactory = CacheTestUtil.startRegionFactory(
|
||||
getConnectionProvider(), cfg, getCacheTestSupport()
|
||||
getJdbcServices(), cfg, getCacheTestSupport()
|
||||
);
|
||||
region = (TransactionalDataRegion) createRegion(regionFactory, "test/test", cfg.getProperties(), getCacheDataDescription());
|
||||
assertFalse("Region is not transaction-aware", region.isTransactionAware());
|
||||
|
@ -104,7 +104,7 @@ public abstract class AbstractEntityCollectionRegionTestCase extends AbstractReg
|
|||
public void testGetCacheDataDescription() throws Exception {
|
||||
Configuration cfg = CacheTestUtil.buildConfiguration("test", InfinispanRegionFactory.class, true, false);
|
||||
InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(
|
||||
getConnectionProvider(), cfg, getCacheTestSupport()
|
||||
getJdbcServices(), cfg, getCacheTestSupport()
|
||||
);
|
||||
TransactionalDataRegion region = (TransactionalDataRegion) createRegion(regionFactory, "test/test", cfg.getProperties(), getCacheDataDescription());
|
||||
CacheDataDescription cdd = region.getCacheDataDescription();
|
||||
|
|
|
@ -73,7 +73,7 @@ public abstract class AbstractGeneralDataRegionTestCase extends AbstractRegionIm
|
|||
private void evictOrRemoveTest() throws Exception {
|
||||
Configuration cfg = createConfiguration();
|
||||
InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(
|
||||
getConnectionProvider(), cfg, getCacheTestSupport()
|
||||
getJdbcServices(), cfg, getCacheTestSupport()
|
||||
);
|
||||
CacheAdapter localCache = getInfinispanCache(regionFactory);
|
||||
boolean invalidation = localCache.isClusteredInvalidation();
|
||||
|
@ -86,7 +86,7 @@ public abstract class AbstractGeneralDataRegionTestCase extends AbstractRegionIm
|
|||
|
||||
cfg = createConfiguration();
|
||||
regionFactory = CacheTestUtil.startRegionFactory(
|
||||
getConnectionProvider(), cfg, getCacheTestSupport()
|
||||
getJdbcServices(), cfg, getCacheTestSupport()
|
||||
);
|
||||
|
||||
GeneralDataRegion remoteRegion = (GeneralDataRegion) createRegion(regionFactory,
|
||||
|
@ -126,7 +126,7 @@ public abstract class AbstractGeneralDataRegionTestCase extends AbstractRegionIm
|
|||
private void evictOrRemoveAllTest(String configName) throws Exception {
|
||||
Configuration cfg = createConfiguration();
|
||||
InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(
|
||||
getConnectionProvider(), cfg, getCacheTestSupport()
|
||||
getJdbcServices(), cfg, getCacheTestSupport()
|
||||
);
|
||||
CacheAdapter localCache = getInfinispanCache(regionFactory);
|
||||
|
||||
|
@ -138,7 +138,7 @@ public abstract class AbstractGeneralDataRegionTestCase extends AbstractRegionIm
|
|||
|
||||
cfg = createConfiguration();
|
||||
regionFactory = CacheTestUtil.startRegionFactory(
|
||||
getConnectionProvider(), cfg, getCacheTestSupport()
|
||||
getJdbcServices(), cfg, getCacheTestSupport()
|
||||
);
|
||||
CacheAdapter remoteCache = getInfinispanCache(regionFactory);
|
||||
|
||||
|
|
|
@ -548,13 +548,13 @@ public abstract class AbstractCollectionRegionAccessStrategyTestCase extends Abs
|
|||
|
||||
localCfg = createConfiguration(configName, configResource);
|
||||
localRegionFactory = CacheTestUtil.startRegionFactory(
|
||||
serviceRegistryHolder.getJdbcServicesImpl().getConnectionProvider(),
|
||||
serviceRegistryHolder.getJdbcServicesImpl(),
|
||||
localCfg
|
||||
);
|
||||
|
||||
remoteCfg = createConfiguration(configName, configResource);
|
||||
remoteRegionFactory = CacheTestUtil.startRegionFactory(
|
||||
serviceRegistryHolder.getJdbcServicesImpl().getConnectionProvider(),
|
||||
serviceRegistryHolder.getJdbcServicesImpl(),
|
||||
remoteCfg
|
||||
);
|
||||
}
|
||||
|
|
|
@ -55,7 +55,7 @@ public class TransactionalExtraAPITestCase extends AbstractNonFunctionalTestCase
|
|||
if (getCollectionAccessStrategy() == null) {
|
||||
Configuration cfg = createConfiguration();
|
||||
InfinispanRegionFactory rf = CacheTestUtil.startRegionFactory(
|
||||
getConnectionProvider(), cfg, getCacheTestSupport()
|
||||
getJdbcServices(), cfg, getCacheTestSupport()
|
||||
);
|
||||
|
||||
// Sleep a bit to avoid concurrent FLUSH problem
|
||||
|
|
|
@ -665,13 +665,13 @@ public abstract class AbstractEntityRegionAccessStrategyTestCase extends Abstrac
|
|||
|
||||
localCfg = createConfiguration(configName);
|
||||
localRegionFactory = CacheTestUtil.startRegionFactory(
|
||||
serviceRegistryHolder.getJdbcServicesImpl().getConnectionProvider(),
|
||||
serviceRegistryHolder.getJdbcServicesImpl(),
|
||||
localCfg
|
||||
);
|
||||
|
||||
remoteCfg = createConfiguration(configName);
|
||||
remoteRegionFactory = CacheTestUtil.startRegionFactory(
|
||||
serviceRegistryHolder.getJdbcServicesImpl().getConnectionProvider(),
|
||||
serviceRegistryHolder.getJdbcServicesImpl(),
|
||||
remoteCfg
|
||||
);
|
||||
}
|
||||
|
|
|
@ -61,7 +61,7 @@ public class TransactionalExtraAPITestCase extends AbstractNonFunctionalTestCase
|
|||
if (getEntityAccessStrategy() == null) {
|
||||
Configuration cfg = createConfiguration();
|
||||
InfinispanRegionFactory rf = CacheTestUtil.startRegionFactory(
|
||||
getConnectionProvider(), cfg, getCacheTestSupport()
|
||||
getJdbcServices(), cfg, getCacheTestSupport()
|
||||
);
|
||||
|
||||
// Sleep a bit to avoid concurrent FLUSH problem
|
||||
|
|
|
@ -91,7 +91,7 @@ public class QueryRegionImplTestCase extends AbstractGeneralDataRegionTestCase {
|
|||
private void putDoesNotBlockGetTest() throws Exception {
|
||||
Configuration cfg = createConfiguration();
|
||||
InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(
|
||||
getConnectionProvider(), cfg, getCacheTestSupport());
|
||||
getJdbcServices(), cfg, getCacheTestSupport());
|
||||
|
||||
// Sleep a bit to avoid concurrent FLUSH problem
|
||||
avoidConcurrentFlush();
|
||||
|
@ -177,7 +177,7 @@ public class QueryRegionImplTestCase extends AbstractGeneralDataRegionTestCase {
|
|||
private void getDoesNotBlockPutTest() throws Exception {
|
||||
Configuration cfg = createConfiguration();
|
||||
InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(
|
||||
getConnectionProvider(), cfg, getCacheTestSupport()
|
||||
getJdbcServices(), cfg, getCacheTestSupport()
|
||||
);
|
||||
|
||||
// Sleep a bit to avoid concurrent FLUSH problem
|
||||
|
|
|
@ -87,14 +87,14 @@ public class TimestampsRegionImplTestCase extends AbstractGeneralDataRegionTestC
|
|||
public void testClearTimestampsRegionInIsolated() throws Exception {
|
||||
Configuration cfg = createConfiguration();
|
||||
InfinispanRegionFactory regionFactory = CacheTestUtil.startRegionFactory(
|
||||
getConnectionProvider(), cfg, getCacheTestSupport()
|
||||
getJdbcServices(), cfg, getCacheTestSupport()
|
||||
);
|
||||
// Sleep a bit to avoid concurrent FLUSH problem
|
||||
avoidConcurrentFlush();
|
||||
|
||||
Configuration cfg2 = createConfiguration();
|
||||
InfinispanRegionFactory regionFactory2 = CacheTestUtil.startRegionFactory(
|
||||
getConnectionProvider(), cfg2, getCacheTestSupport()
|
||||
getJdbcServices(), cfg2, getCacheTestSupport()
|
||||
);
|
||||
// Sleep a bit to avoid concurrent FLUSH problem
|
||||
avoidConcurrentFlush();
|
||||
|
|
|
@ -36,6 +36,7 @@ import org.hibernate.cache.infinispan.InfinispanRegionFactory;
|
|||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.cfg.Settings;
|
||||
import org.hibernate.engine.jdbc.spi.JdbcServices;
|
||||
import org.hibernate.service.jdbc.connections.spi.ConnectionProvider;
|
||||
import org.hibernate.service.spi.ServicesRegistry;
|
||||
|
||||
|
@ -73,11 +74,11 @@ public class CacheTestUtil {
|
|||
return cfg;
|
||||
}
|
||||
|
||||
public static InfinispanRegionFactory startRegionFactory(ConnectionProvider connectionProvider,
|
||||
public static InfinispanRegionFactory startRegionFactory(JdbcServices jdbcServices,
|
||||
Configuration cfg)
|
||||
throws ClassNotFoundException, InstantiationException, IllegalAccessException {
|
||||
|
||||
Settings settings = cfg.buildSettings( connectionProvider );
|
||||
Settings settings = cfg.buildSettings( jdbcServices );
|
||||
Properties properties = cfg.getProperties();
|
||||
|
||||
String factoryType = cfg.getProperty(Environment.CACHE_REGION_FACTORY);
|
||||
|
@ -89,11 +90,11 @@ public class CacheTestUtil {
|
|||
return regionFactory;
|
||||
}
|
||||
|
||||
public static InfinispanRegionFactory startRegionFactory(ConnectionProvider connectionProvider,
|
||||
public static InfinispanRegionFactory startRegionFactory(JdbcServices jdbcServices,
|
||||
Configuration cfg,
|
||||
CacheTestSupport testSupport)
|
||||
throws ClassNotFoundException, InstantiationException, IllegalAccessException {
|
||||
InfinispanRegionFactory factory = startRegionFactory(connectionProvider, cfg);
|
||||
InfinispanRegionFactory factory = startRegionFactory(jdbcServices, cfg);
|
||||
testSupport.registerFactory(factory);
|
||||
return factory;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue