Search in sources :

Example 1 with SchedulerConfigException

use of org.quartz.SchedulerConfigException 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)

Aggregations

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 MetaDataAccessException (org.springframework.jdbc.support.MetaDataAccessException)1