Search in sources :

Example 1 with MetaDataAccessException

use of org.springframework.jdbc.support.MetaDataAccessException in project spring-framework by spring-projects.

the class LocalDataSourceJobStore method initialize.

@Override
public void initialize(ClassLoadHelper loadHelper, SchedulerSignaler signaler) throws SchedulerConfigException {
    // Absolutely needs thread-bound DataSource to initialize.
    this.dataSource = SchedulerFactoryBean.getConfigTimeDataSource();
    if (this.dataSource == null) {
        throw new SchedulerConfigException("No local DataSource found for configuration - " + "'dataSource' property must be set on SchedulerFactoryBean");
    }
    // Configure transactional connection settings for Quartz.
    setDataSource(TX_DATA_SOURCE_PREFIX + getInstanceName());
    setDontSetAutoCommitFalse(true);
    // Register transactional ConnectionProvider for Quartz.
    DBConnectionManager.getInstance().addConnectionProvider(TX_DATA_SOURCE_PREFIX + getInstanceName(), new ConnectionProvider() {

        @Override
        public Connection getConnection() throws SQLException {
            // Return a transactional Connection, if any.
            return DataSourceUtils.doGetConnection(dataSource);
        }

        @Override
        public void shutdown() {
        // Do nothing - a Spring-managed DataSource has its own lifecycle.
        }

        /* Quartz 2.2 initialize method */
        public void initialize() {
        // Do nothing - a Spring-managed DataSource has its own lifecycle.
        }
    });
    // Non-transactional DataSource is optional: fall back to default
    // DataSource if not explicitly specified.
    DataSource nonTxDataSource = SchedulerFactoryBean.getConfigTimeNonTransactionalDataSource();
    final DataSource nonTxDataSourceToUse = (nonTxDataSource != null ? nonTxDataSource : this.dataSource);
    // Configure non-transactional connection settings for Quartz.
    setNonManagedTXDataSource(NON_TX_DATA_SOURCE_PREFIX + getInstanceName());
    // Register non-transactional ConnectionProvider for Quartz.
    DBConnectionManager.getInstance().addConnectionProvider(NON_TX_DATA_SOURCE_PREFIX + getInstanceName(), new ConnectionProvider() {

        @Override
        public Connection getConnection() throws SQLException {
            // Always return a non-transactional Connection.
            return nonTxDataSourceToUse.getConnection();
        }

        @Override
        public void shutdown() {
        // Do nothing - a Spring-managed DataSource has its own lifecycle.
        }

        /* Quartz 2.2 initialize method */
        public void initialize() {
        // Do nothing - a Spring-managed DataSource has its own lifecycle.
        }
    });
    // No, if HSQL is the platform, we really don't want to use locks...
    try {
        String productName = JdbcUtils.extractDatabaseMetaData(this.dataSource, "getDatabaseProductName").toString();
        productName = JdbcUtils.commonDatabaseName(productName);
        if (productName != null && productName.toLowerCase().contains("hsql")) {
            setUseDBLocks(false);
            setLockHandler(new SimpleSemaphore());
        }
    } catch (MetaDataAccessException ex) {
        logWarnIfNonZero(1, "Could not detect database type. Assuming locks can be taken.");
    }
    super.initialize(loadHelper, signaler);
}
Also used : MetaDataAccessException(org.springframework.jdbc.support.MetaDataAccessException) SimpleSemaphore(org.quartz.impl.jdbcjobstore.SimpleSemaphore) SQLException(java.sql.SQLException) Connection(java.sql.Connection) SchedulerConfigException(org.quartz.SchedulerConfigException) ConnectionProvider(org.quartz.utils.ConnectionProvider) DataSource(javax.sql.DataSource)

Example 2 with MetaDataAccessException

use of org.springframework.jdbc.support.MetaDataAccessException in project spring-framework by spring-projects.

the class CallMetaDataProviderFactory method createMetaDataProvider.

/**
	 * Create a CallMetaDataProvider based on the database metadata
	 * @param dataSource used to retrieve metadata
	 * @param context the class that holds configuration and metadata
	 * @return instance of the CallMetaDataProvider implementation to be used
	 */
public static CallMetaDataProvider createMetaDataProvider(DataSource dataSource, final CallMetaDataContext context) {
    try {
        return (CallMetaDataProvider) JdbcUtils.extractDatabaseMetaData(dataSource, new DatabaseMetaDataCallback() {

            @Override
            public Object processMetaData(DatabaseMetaData databaseMetaData) throws SQLException, MetaDataAccessException {
                String databaseProductName = JdbcUtils.commonDatabaseName(databaseMetaData.getDatabaseProductName());
                boolean accessProcedureColumnMetaData = context.isAccessCallParameterMetaData();
                if (context.isFunction()) {
                    if (!supportedDatabaseProductsForFunctions.contains(databaseProductName)) {
                        if (logger.isWarnEnabled()) {
                            logger.warn(databaseProductName + " is not one of the databases fully supported for function calls " + "-- supported are: " + supportedDatabaseProductsForFunctions);
                        }
                        if (accessProcedureColumnMetaData) {
                            logger.warn("Metadata processing disabled - you must specify all parameters explicitly");
                            accessProcedureColumnMetaData = false;
                        }
                    }
                } else {
                    if (!supportedDatabaseProductsForProcedures.contains(databaseProductName)) {
                        if (logger.isWarnEnabled()) {
                            logger.warn(databaseProductName + " is not one of the databases fully supported for procedure calls " + "-- supported are: " + supportedDatabaseProductsForProcedures);
                        }
                        if (accessProcedureColumnMetaData) {
                            logger.warn("Metadata processing disabled - you must specify all parameters explicitly");
                            accessProcedureColumnMetaData = false;
                        }
                    }
                }
                CallMetaDataProvider provider;
                if ("Oracle".equals(databaseProductName)) {
                    provider = new OracleCallMetaDataProvider(databaseMetaData);
                } else if ("DB2".equals(databaseProductName)) {
                    provider = new Db2CallMetaDataProvider((databaseMetaData));
                } else if ("Apache Derby".equals(databaseProductName)) {
                    provider = new DerbyCallMetaDataProvider((databaseMetaData));
                } else if ("PostgreSQL".equals(databaseProductName)) {
                    provider = new PostgresCallMetaDataProvider((databaseMetaData));
                } else if ("Sybase".equals(databaseProductName)) {
                    provider = new SybaseCallMetaDataProvider((databaseMetaData));
                } else if ("Microsoft SQL Server".equals(databaseProductName)) {
                    provider = new SqlServerCallMetaDataProvider((databaseMetaData));
                } else if ("HDB".equals(databaseProductName)) {
                    provider = new HanaCallMetaDataProvider((databaseMetaData));
                } else {
                    provider = new GenericCallMetaDataProvider(databaseMetaData);
                }
                if (logger.isDebugEnabled()) {
                    logger.debug("Using " + provider.getClass().getName());
                }
                provider.initializeWithMetaData(databaseMetaData);
                if (accessProcedureColumnMetaData) {
                    provider.initializeWithProcedureColumnMetaData(databaseMetaData, context.getCatalogName(), context.getSchemaName(), context.getProcedureName());
                }
                return provider;
            }
        });
    } catch (MetaDataAccessException ex) {
        throw new DataAccessResourceFailureException("Error retrieving database metadata", ex);
    }
}
Also used : DatabaseMetaDataCallback(org.springframework.jdbc.support.DatabaseMetaDataCallback) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) DatabaseMetaData(java.sql.DatabaseMetaData) MetaDataAccessException(org.springframework.jdbc.support.MetaDataAccessException)

Example 3 with MetaDataAccessException

use of org.springframework.jdbc.support.MetaDataAccessException in project spring-framework by spring-projects.

the class TableMetaDataProviderFactory method createMetaDataProvider.

/**
	 * Create a TableMetaDataProvider based on the database metadata.
	 * @param dataSource used to retrieve metadata
	 * @param context the class that holds configuration and metadata
	 * @return instance of the TableMetaDataProvider implementation to be used
	 */
public static TableMetaDataProvider createMetaDataProvider(DataSource dataSource, TableMetaDataContext context) {
    try {
        return (TableMetaDataProvider) JdbcUtils.extractDatabaseMetaData(dataSource, new DatabaseMetaDataCallback() {

            @Override
            public Object processMetaData(DatabaseMetaData databaseMetaData) throws SQLException {
                String databaseProductName = JdbcUtils.commonDatabaseName(databaseMetaData.getDatabaseProductName());
                boolean accessTableColumnMetaData = context.isAccessTableColumnMetaData();
                TableMetaDataProvider provider;
                if ("Oracle".equals(databaseProductName)) {
                    provider = new OracleTableMetaDataProvider(databaseMetaData, context.isOverrideIncludeSynonymsDefault());
                } else if ("HSQL Database Engine".equals(databaseProductName)) {
                    provider = new HsqlTableMetaDataProvider(databaseMetaData);
                } else if ("PostgreSQL".equals(databaseProductName)) {
                    provider = new PostgresTableMetaDataProvider(databaseMetaData);
                } else if ("Apache Derby".equals(databaseProductName)) {
                    provider = new DerbyTableMetaDataProvider(databaseMetaData);
                } else {
                    provider = new GenericTableMetaDataProvider(databaseMetaData);
                }
                if (logger.isDebugEnabled()) {
                    logger.debug("Using " + provider.getClass().getSimpleName());
                }
                provider.initializeWithMetaData(databaseMetaData);
                if (accessTableColumnMetaData) {
                    provider.initializeWithTableColumnMetaData(databaseMetaData, context.getCatalogName(), context.getSchemaName(), context.getTableName());
                }
                return provider;
            }
        });
    } catch (MetaDataAccessException ex) {
        throw new DataAccessResourceFailureException("Error retrieving database metadata", ex);
    }
}
Also used : MetaDataAccessException(org.springframework.jdbc.support.MetaDataAccessException) DatabaseMetaDataCallback(org.springframework.jdbc.support.DatabaseMetaDataCallback) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 4 with MetaDataAccessException

use of org.springframework.jdbc.support.MetaDataAccessException in project spring-boot by spring-projects.

the class DatabaseLookup method getDatabase.

/**
	 * Return the most suitable {@link Database} for the given {@link DataSource}.
	 * @param dataSource the source {@link DataSource}
	 * @return the most suitable {@link Database}
	 */
public static Database getDatabase(DataSource dataSource) {
    if (dataSource == null) {
        return Database.DEFAULT;
    }
    try {
        String url = (String) JdbcUtils.extractDatabaseMetaData(dataSource, "getURL");
        DatabaseDriver driver = DatabaseDriver.fromJdbcUrl(url);
        Database database = LOOKUP.get(driver);
        if (database != null) {
            return database;
        }
    } catch (MetaDataAccessException ex) {
        logger.warn("Unable to determine jdbc url from datasource", ex);
    }
    return Database.DEFAULT;
}
Also used : MetaDataAccessException(org.springframework.jdbc.support.MetaDataAccessException) Database(org.springframework.orm.jpa.vendor.Database) DatabaseDriver(org.springframework.boot.jdbc.DatabaseDriver)

Example 5 with MetaDataAccessException

use of org.springframework.jdbc.support.MetaDataAccessException in project spring-boot by spring-projects.

the class AbstractDatabaseInitializer method getDatabaseName.

protected String getDatabaseName() {
    try {
        String productName = JdbcUtils.commonDatabaseName(JdbcUtils.extractDatabaseMetaData(this.dataSource, "getDatabaseProductName").toString());
        DatabaseDriver databaseDriver = DatabaseDriver.fromProductName(productName);
        if (databaseDriver == DatabaseDriver.UNKNOWN) {
            throw new IllegalStateException("Unable to detect database type");
        }
        return databaseDriver.getId();
    } catch (MetaDataAccessException ex) {
        throw new IllegalStateException("Unable to detect database type", ex);
    }
}
Also used : MetaDataAccessException(org.springframework.jdbc.support.MetaDataAccessException) DatabaseDriver(org.springframework.boot.jdbc.DatabaseDriver)

Aggregations

MetaDataAccessException (org.springframework.jdbc.support.MetaDataAccessException)5 DatabaseMetaData (java.sql.DatabaseMetaData)2 DatabaseDriver (org.springframework.boot.jdbc.DatabaseDriver)2 DataAccessResourceFailureException (org.springframework.dao.DataAccessResourceFailureException)2 DatabaseMetaDataCallback (org.springframework.jdbc.support.DatabaseMetaDataCallback)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 DataSource (javax.sql.DataSource)1 SchedulerConfigException (org.quartz.SchedulerConfigException)1 SimpleSemaphore (org.quartz.impl.jdbcjobstore.SimpleSemaphore)1 ConnectionProvider (org.quartz.utils.ConnectionProvider)1 Database (org.springframework.orm.jpa.vendor.Database)1