Java Code Examples for org.springframework.jdbc.support.JdbcUtils#extractDatabaseMetaData()

The following examples show how to use org.springframework.jdbc.support.JdbcUtils#extractDatabaseMetaData() . 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
Source File: MolgenisVersionService.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 2
Source File: ExecutionQueueRepositoryImpl.java    From score with 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 3
Source File: BusinessObjectMetaDataDaoJdbc.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
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 4
Source File: SkipperFlywayConfigurationCustomizer.java    From spring-cloud-skipper with Apache License 2.0 5 votes vote down vote up
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 5
Source File: JdbcHttpSessionConfiguration.java    From spring-session with Apache License 2.0 5 votes vote down vote up
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 6
Source File: DataFlowFlywayConfigurationCustomizer.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
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
Source File: CallMetaDataProviderFactory.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * 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 8
Source File: TableMetaDataProviderFactory.java    From effectivejava with Apache License 2.0 4 votes vote down vote up
/**
 * 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 9
Source File: CallMetaDataProviderFactory.java    From effectivejava with Apache License 2.0 4 votes vote down vote up
/**
 * 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 {
					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 retreiving database metadata", ex);
	}

}
 
Example 10
Source File: TableMetaDataProviderFactory.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * 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 11
Source File: CallMetaDataProviderFactory.java    From spring-analysis-note with 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 12
Source File: TableMetaDataProviderFactory.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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 13
Source File: CallMetaDataProviderFactory.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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 14
Source File: LocalDataSourceJobStore.java    From java-technology-stack with 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 15
Source File: TableMetaDataProviderFactory.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * 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 16
Source File: CallMetaDataProviderFactory.java    From java-technology-stack with 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 17
Source File: LocalDataSourceJobStore.java    From spring-analysis-note with 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 18
Source File: TableMetaDataProviderFactory.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * 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);
	}
}