Java Code Examples for org.springframework.jdbc.support.MetaDataAccessException

The following examples show how to use org.springframework.jdbc.support.MetaDataAccessException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * 获取当前使用数据库类型
 */
private static String getDatabaseName(DataSource dataSource) {
    try {
        String productName = JdbcUtils.
                commonDatabaseName(JdbcUtils.extractDatabaseMetaData(
                        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);
    }
}
 
Example 2
private DatabaseDriver getDatabaseDriver(DataSource dataSource) {
	// copied from boot's flyway auto-config to get matching db vendor id
	try {
		String url = JdbcUtils.extractDatabaseMetaData(dataSource, "getURL");
		return DatabaseDriver.fromJdbcUrl(url);
	}
	catch (MetaDataAccessException ex) {
		throw new IllegalStateException(ex);
	}
}
 
Example 3
private String getDatabaseType(DataSource dataSource) {
	try {
		return DatabaseType.fromMetaData(dataSource).toString().toLowerCase();
	}
	catch (MetaDataAccessException ex) {
		throw new IllegalStateException("Unable to detect database type", ex);
	}
}
 
Example 4
/**
 * Get a {@link PagingQueryProvider} instance using the provided properties
 * and appropriate for the given database type.
 *
 * @see FactoryBean#getObject()
 */
@Override
public PagingQueryProvider getObject() throws Exception {

	DatabaseType type;
	try {
		type = databaseType != null ? DatabaseType.valueOf(databaseType.toUpperCase()) : DatabaseType
				.fromMetaData(dataSource);
	}
	catch (MetaDataAccessException e) {
		throw new IllegalArgumentException(
				"Could not inspect meta data for database type.  You have to supply it explicitly.", e);
	}

	AbstractSqlPagingQueryProvider provider = providers.get(type);
	Assert.state(provider != null, "Should not happen: missing PagingQueryProvider for DatabaseType=" + type);

	provider.setFromClause(fromClause);
	provider.setWhereClause(whereClause);
	provider.setSortKeys(sortKeys);
	if (StringUtils.hasText(selectClause)) {
		provider.setSelectClause(selectClause);
	}
	provider.init(dataSource);

	return provider;

}
 
Example 5
/**
 * Get a {@link PagingQueryProvider} instance using the provided properties and
 * appropriate for the given database type.
 *
 * @see FactoryBean#getObject()
 */
@Override
public PagingQueryProvider getObject() throws Exception {

	DatabaseType type;
	try {
		type = databaseType != null ? DatabaseType.valueOf(databaseType.toUpperCase())
				: DatabaseType.fromMetaData(dataSource);
	}
	catch (MetaDataAccessException e) {
		throw new IllegalArgumentException(
				"Could not inspect meta data for database type.  You have to supply it explicitly.", e);
	}

	AbstractSqlPagingQueryProvider provider = providers.get(type);
	Assert.state(provider != null, "Should not happen: missing PagingQueryProvider for DatabaseType=" + type);

	provider.setFromClause(fromClause);
	provider.setWhereClause(whereClause);
	provider.setSortKeys(sortKeys);
	if (StringUtils.hasText(selectClause)) {
		provider.setSelectClause(selectClause);
	}
	provider.init(dataSource);

	return provider;

}
 
Example 6
private DatabaseDriver getDatabaseDriver(DataSource dataSource) {
	// copied from boot's flyway auto-config to get matching db vendor id
	try {
		String url = JdbcUtils.extractDatabaseMetaData(dataSource, "getURL");
		return DatabaseDriver.fromJdbcUrl(url);
	}
	catch (MetaDataAccessException ex) {
		throw new IllegalStateException(ex);
	}
}
 
Example 7
@Bean
DataflowTaskExecutionMetadataDao dataflowTaskExecutionMetadataDao(DataSource dataSource) {
	DataFieldMaxValueIncrementerFactory incrementerFactory = new DefaultDataFieldMaxValueIncrementerFactory(dataSource);
	String databaseType;
	try {
		databaseType = DatabaseType.fromMetaData(dataSource).name();
	}
	catch (MetaDataAccessException e) {
		throw new IllegalStateException(e);
	}
	return new JdbcDataflowTaskExecutionMetadataDao(dataSource, incrementerFactory.getIncrementer(databaseType,
			"task_execution_metadata_seq"));
}
 
Example 8
Source Project: spring-cloud-dataflow   Source File: TestDependencies.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public DataflowTaskExecutionMetadataDao dataflowTaskExecutionMetadataDao(DataSource dataSource, ApplicationContext context) {
	DataFieldMaxValueIncrementerFactory incrementerFactory = new DefaultDataFieldMaxValueIncrementerFactory(dataSource);
	String databaseType;
	try {
		databaseType = DatabaseType.fromMetaData(dataSource).name();
	}
	catch (MetaDataAccessException e) {
		throw new IllegalStateException(e);
	}
	return new JdbcDataflowTaskExecutionMetadataDao(dataSource, incrementerFactory.getIncrementer(databaseType,
			"task_execution_metadata_seq"));
}
 
Example 9
@Bean
public DataflowTaskExecutionMetadataDao dataflowTaskExecutionMetadataDao(DataSource dataSource, ApplicationContext context) {
	DataFieldMaxValueIncrementerFactory incrementerFactory = new DefaultDataFieldMaxValueIncrementerFactory(dataSource);
	String databaseType;
	try {
		databaseType = DatabaseType.fromMetaData(dataSource).name();
	}
	catch (MetaDataAccessException e) {
		throw new IllegalStateException(e);
	}
	return new JdbcDataflowTaskExecutionMetadataDao(dataSource, incrementerFactory.getIncrementer(databaseType,
			"task_execution_metadata_seq"));
}
 
Example 10
Source Project: spring-cloud-dataflow   Source File: JobDependencies.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public DataflowTaskExecutionMetadataDao dataflowTaskExecutionMetadataDao(DataSource dataSource) {
	DataFieldMaxValueIncrementerFactory incrementerFactory = new DefaultDataFieldMaxValueIncrementerFactory(dataSource);
	String databaseType;
	try {
		databaseType = DatabaseType.fromMetaData(dataSource).name();
	}
	catch (MetaDataAccessException e) {
		throw new IllegalStateException(e);
	}
	return new JdbcDataflowTaskExecutionMetadataDao(dataSource, incrementerFactory.getIncrementer(databaseType,
			"task_execution_metadata_seq"));
}
 
Example 11
/**
 * Get a {@link PagingQueryProvider} instance using the provided properties and
 * appropriate for the given database type.
 *
 * @see FactoryBean#getObject()
 */
@Override
public PagingQueryProvider getObject() throws Exception {

	DatabaseType type;
	try {
		type = this.databaseType != null
				? DatabaseType.valueOf(this.databaseType.toUpperCase())
				: DatabaseType.fromMetaData(this.dataSource);
	}
	catch (MetaDataAccessException e) {
		throw new IllegalArgumentException(
				"Could not inspect meta data for database type.  You have to supply it explicitly.",
				e);
	}

	AbstractSqlPagingQueryProvider provider = this.providers.get(type);
	Assert.state(provider != null,
			"Should not happen: missing PagingQueryProvider for DatabaseType="
					+ type);

	provider.setFromClause(this.fromClause);
	provider.setWhereClause(this.whereClause);
	provider.setSortKeys(this.sortKeys);
	if (StringUtils.hasText(this.selectClause)) {
		provider.setSelectClause(this.selectClause);
	}
	provider.init(this.dataSource);

	return provider;

}
 
Example 12
private String getDatabaseType(DataSource dataSource) {
	try {
		return JdbcUtils
				.commonDatabaseName(DatabaseType.fromMetaData(dataSource).toString())
				.toLowerCase();
	}
	catch (MetaDataAccessException ex) {
		throw new IllegalStateException("Unable to detect database type", ex);
	}
}
 
Example 13
private void buildTaskExecutionDao(DataSource dataSource) {
	DataFieldMaxValueIncrementerFactory incrementerFactory = new DefaultDataFieldMaxValueIncrementerFactory(
			dataSource);
	this.dao = new JdbcTaskExecutionDao(dataSource, this.tablePrefix);
	String databaseType;
	try {
		databaseType = DatabaseType.fromMetaData(dataSource).name();
	}
	catch (MetaDataAccessException e) {
		throw new IllegalStateException(e);
	}
	((JdbcTaskExecutionDao) this.dao).setTaskIncrementer(incrementerFactory
			.getIncrementer(databaseType, this.tablePrefix + "SEQ"));
}
 
Example 14
Source Project: spring-cloud-task   Source File: TestDBUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a incrementer for the DataSource.
 * @param dataSource the datasource that the incrementer will use to record current
 * id.
 * @return a DataFieldMaxValueIncrementer object.
 */
public static DataFieldMaxValueIncrementer getIncrementer(DataSource dataSource) {
	DataFieldMaxValueIncrementerFactory incrementerFactory = new DefaultDataFieldMaxValueIncrementerFactory(
			dataSource);
	String databaseType = null;
	try {
		databaseType = DatabaseType.fromMetaData(dataSource).name();
	}
	catch (MetaDataAccessException e) {
		throw new IllegalStateException(e);
	}
	return incrementerFactory.getIncrementer(databaseType, "TASK_SEQ");
}
 
Example 15
private static boolean requiresTemporaryLob(DataSource dataSource) {
	try {
		String productName = JdbcUtils.extractDatabaseMetaData(dataSource, "getDatabaseProductName");
		return "Oracle".equalsIgnoreCase(JdbcUtils.commonDatabaseName(productName));
	}
	catch (MetaDataAccessException ex) {
		return false;
	}
}
 
Example 16
public FieldMetaData getFieldMetaData(Class businessObjectClass, String propertyName) {
    try {
        return (FieldMetaData) JdbcUtils.extractDatabaseMetaData(getDataSource(), new FieldMetaDataImpl(businessObjectClass, propertyName));
    }
    catch (MetaDataAccessException e) {
        throw new RuntimeException(new StringBuffer("BusinessObjectMetaDataDaoJdbc unable to getFieldMetaData for businessObjectClass ").append(businessObjectClass).append(" propertyName ").append(propertyName).toString(), e);
    }
}
 
Example 17
Source Project: score   Source File: ExecutionQueueRepositoryImpl.java    License: Apache License 2.0 5 votes vote down vote up
private String getDatabaseProductName() {
    String dbms = "";
    try {
        dbms = (String) JdbcUtils.extractDatabaseMetaData(dataSource, "getDatabaseProductName");

        logger.info("Database product name: " + dbms);
    } catch (MetaDataAccessException e) {
        logger.warn("Database type could not be determined!", e);
    }

    return dbms;
}
 
Example 18
private boolean versionTableExist() {
  try {
    return (boolean)
        JdbcUtils.extractDatabaseMetaData(
            dataSource,
            dbmd -> {
              ResultSet tables = dbmd.getTables(null, null, "Version", new String[] {"TABLE"});
              return tables.first();
            });
  } catch (MetaDataAccessException e) {
    return false;
  }
}
 
Example 19
Source Project: spring-analysis-note   Source File: CallMetaDataProviderFactory.java    License: MIT License 4 votes vote down vote up
/**
 * Create a {@link CallMetaDataProvider} based on the database meta-data.
 * @param dataSource the JDBC DataSource to use for retrieving meta-data
 * @param context the class that holds configuration and meta-data
 * @return instance of the CallMetaDataProvider implementation to be used
 */
public static CallMetaDataProvider createMetaDataProvider(DataSource dataSource, final CallMetaDataContext context) {
	try {
		return (CallMetaDataProvider) JdbcUtils.extractDatabaseMetaData(dataSource, databaseMetaData -> {
			String databaseProductName = JdbcUtils.commonDatabaseName(databaseMetaData.getDatabaseProductName());
			boolean accessProcedureColumnMetaData = context.isAccessCallParameterMetaData();
			if (context.isFunction()) {
				if (!supportedDatabaseProductsForFunctions.contains(databaseProductName)) {
					if (logger.isInfoEnabled()) {
						logger.info(databaseProductName + " is not one of the databases fully supported for function calls " +
								"-- supported are: " + supportedDatabaseProductsForFunctions);
					}
					if (accessProcedureColumnMetaData) {
						logger.info("Metadata processing disabled - you must specify all parameters explicitly");
						accessProcedureColumnMetaData = false;
					}
				}
			}
			else {
				if (!supportedDatabaseProductsForProcedures.contains(databaseProductName)) {
					if (logger.isInfoEnabled()) {
						logger.info(databaseProductName + " is not one of the databases fully supported for procedure calls " +
								"-- supported are: " + supportedDatabaseProductsForProcedures);
					}
					if (accessProcedureColumnMetaData) {
						logger.info("Metadata processing disabled - you must specify all parameters explicitly");
						accessProcedureColumnMetaData = false;
					}
				}
			}

			CallMetaDataProvider provider;
			if ("Oracle".equals(databaseProductName)) {
				provider = new OracleCallMetaDataProvider(databaseMetaData);
			}
			else if ("PostgreSQL".equals(databaseProductName)) {
				provider = new PostgresCallMetaDataProvider((databaseMetaData));
			}
			else if ("Apache Derby".equals(databaseProductName)) {
				provider = new DerbyCallMetaDataProvider((databaseMetaData));
			}
			else if ("DB2".equals(databaseProductName)) {
				provider = new Db2CallMetaDataProvider((databaseMetaData));
			}
			else if ("HDB".equals(databaseProductName)) {
				provider = new HanaCallMetaDataProvider((databaseMetaData));
			}
			else if ("Microsoft SQL Server".equals(databaseProductName)) {
				provider = new SqlServerCallMetaDataProvider((databaseMetaData));
			}
			else if ("Sybase".equals(databaseProductName)) {
				provider = new SybaseCallMetaDataProvider((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 meta-data", ex);
	}
}
 
Example 20
/**
 * Create a {@link TableMetaDataProvider} based on the database meta-data.
 * @param dataSource used to retrieve meta-data
 * @param context the class that holds configuration and meta-data
 * @return instance of the TableMetaDataProvider implementation to be used
 */
public static TableMetaDataProvider createMetaDataProvider(DataSource dataSource, TableMetaDataContext context) {
	try {
		return (TableMetaDataProvider) JdbcUtils.extractDatabaseMetaData(dataSource, databaseMetaData -> {
			String databaseProductName =
					JdbcUtils.commonDatabaseName(databaseMetaData.getDatabaseProductName());
			boolean accessTableColumnMetaData = context.isAccessTableColumnMetaData();
			TableMetaDataProvider provider;

			if ("Oracle".equals(databaseProductName)) {
				provider = new OracleTableMetaDataProvider(
						databaseMetaData, context.isOverrideIncludeSynonymsDefault());
			}
			else if ("PostgreSQL".equals(databaseProductName)) {
				provider = new PostgresTableMetaDataProvider(databaseMetaData);
			}
			else if ("Apache Derby".equals(databaseProductName)) {
				provider = new DerbyTableMetaDataProvider(databaseMetaData);
			}
			else if ("HSQL Database Engine".equals(databaseProductName)) {
				provider = new HsqlTableMetaDataProvider(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 meta-data", ex);
	}
}
 
Example 21
Source Project: spring-analysis-note   Source File: LocalDataSourceJobStore.java    License: MIT License 4 votes vote down vote up
@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");
		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);

}
 
Example 22
/**
 * Create a {@link CallMetaDataProvider} based on the database meta-data.
 * @param dataSource the JDBC DataSource to use for retrieving meta-data
 * @param context the class that holds configuration and meta-data
 * @return instance of the CallMetaDataProvider implementation to be used
 */
public static CallMetaDataProvider createMetaDataProvider(DataSource dataSource, final CallMetaDataContext context) {
	try {
		return (CallMetaDataProvider) JdbcUtils.extractDatabaseMetaData(dataSource, databaseMetaData -> {
			String databaseProductName = JdbcUtils.commonDatabaseName(databaseMetaData.getDatabaseProductName());
			boolean accessProcedureColumnMetaData = context.isAccessCallParameterMetaData();
			if (context.isFunction()) {
				if (!supportedDatabaseProductsForFunctions.contains(databaseProductName)) {
					if (logger.isInfoEnabled()) {
						logger.info(databaseProductName + " is not one of the databases fully supported for function calls " +
								"-- supported are: " + supportedDatabaseProductsForFunctions);
					}
					if (accessProcedureColumnMetaData) {
						logger.info("Metadata processing disabled - you must specify all parameters explicitly");
						accessProcedureColumnMetaData = false;
					}
				}
			}
			else {
				if (!supportedDatabaseProductsForProcedures.contains(databaseProductName)) {
					if (logger.isInfoEnabled()) {
						logger.info(databaseProductName + " is not one of the databases fully supported for procedure calls " +
								"-- supported are: " + supportedDatabaseProductsForProcedures);
					}
					if (accessProcedureColumnMetaData) {
						logger.info("Metadata processing disabled - you must specify all parameters explicitly");
						accessProcedureColumnMetaData = false;
					}
				}
			}

			CallMetaDataProvider provider;
			if ("Oracle".equals(databaseProductName)) {
				provider = new OracleCallMetaDataProvider(databaseMetaData);
			}
			else if ("PostgreSQL".equals(databaseProductName)) {
				provider = new PostgresCallMetaDataProvider((databaseMetaData));
			}
			else if ("Apache Derby".equals(databaseProductName)) {
				provider = new DerbyCallMetaDataProvider((databaseMetaData));
			}
			else if ("DB2".equals(databaseProductName)) {
				provider = new Db2CallMetaDataProvider((databaseMetaData));
			}
			else if ("HDB".equals(databaseProductName)) {
				provider = new HanaCallMetaDataProvider((databaseMetaData));
			}
			else if ("Microsoft SQL Server".equals(databaseProductName)) {
				provider = new SqlServerCallMetaDataProvider((databaseMetaData));
			}
			else if ("Sybase".equals(databaseProductName)) {
				provider = new SybaseCallMetaDataProvider((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 meta-data", ex);
	}
}
 
Example 23
/**
 * Create a {@link TableMetaDataProvider} based on the database meta-data.
 * @param dataSource used to retrieve meta-data
 * @param context the class that holds configuration and meta-data
 * @return instance of the TableMetaDataProvider implementation to be used
 */
public static TableMetaDataProvider createMetaDataProvider(DataSource dataSource, TableMetaDataContext context) {
	try {
		return (TableMetaDataProvider) JdbcUtils.extractDatabaseMetaData(dataSource, databaseMetaData -> {
			String databaseProductName =
					JdbcUtils.commonDatabaseName(databaseMetaData.getDatabaseProductName());
			boolean accessTableColumnMetaData = context.isAccessTableColumnMetaData();
			TableMetaDataProvider provider;

			if ("Oracle".equals(databaseProductName)) {
				provider = new OracleTableMetaDataProvider(
						databaseMetaData, context.isOverrideIncludeSynonymsDefault());
			}
			else if ("PostgreSQL".equals(databaseProductName)) {
				provider = new PostgresTableMetaDataProvider(databaseMetaData);
			}
			else if ("Apache Derby".equals(databaseProductName)) {
				provider = new DerbyTableMetaDataProvider(databaseMetaData);
			}
			else if ("HSQL Database Engine".equals(databaseProductName)) {
				provider = new HsqlTableMetaDataProvider(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 meta-data", ex);
	}
}
 
Example 24
Source Project: java-technology-stack   Source File: LocalDataSourceJobStore.java    License: MIT License 4 votes vote down vote up
@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");
		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);

}
 
Example 25
Source Project: lams   Source File: LocalDataSourceJobStore.java    License: GNU General Public License v2.0 4 votes vote down vote up
@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);

}
 
Example 26
/**
 * 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
 */
static public 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);
	}
}
 
Example 27
/**
 * Create a TableMetaDataProvider based on the database metadata.
 * @param dataSource used to retrieve metadata
 * @param context the class that holds configuration and metadata
 * @param nativeJdbcExtractor the NativeJdbcExtractor to be used
 * @return instance of the TableMetaDataProvider implementation to be used
 */
public static TableMetaDataProvider createMetaDataProvider(DataSource dataSource,
			final TableMetaDataContext context, final NativeJdbcExtractor nativeJdbcExtractor) {
	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 (nativeJdbcExtractor != null) {
							provider.setNativeJdbcExtractor(nativeJdbcExtractor);
						}
						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);
	}
}
 
Example 28
/**
 * 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
 */
static public 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);
	}
}
 
Example 29
/**
 * Create a TableMetaDataProvider based on the database metedata
 * @param dataSource used to retrieve metedata
 * @param context the class that holds configuration and metedata
 * @param nativeJdbcExtractor the NativeJdbcExtractor to be used
 * @return instance of the TableMetaDataProvider implementation to be used
 */
public static TableMetaDataProvider createMetaDataProvider(DataSource dataSource,
			final TableMetaDataContext context, final NativeJdbcExtractor nativeJdbcExtractor) {
	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 (nativeJdbcExtractor != null) {
							provider.setNativeJdbcExtractor(nativeJdbcExtractor);
						}
						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);
	}
}
 
Example 30
@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);

}