1 16 17 package org.springframework.scheduling.quartz; 18 19 import java.sql.Connection ; 20 import java.sql.SQLException ; 21 22 import javax.sql.DataSource ; 23 24 import org.quartz.SchedulerConfigException; 25 import org.quartz.impl.jdbcjobstore.JobStoreCMT; 26 import org.quartz.spi.ClassLoadHelper; 27 import org.quartz.spi.SchedulerSignaler; 28 import org.quartz.utils.ConnectionProvider; 29 import org.quartz.utils.DBConnectionManager; 30 31 import org.springframework.jdbc.datasource.DataSourceUtils; 32 33 59 public class LocalDataSourceJobStore extends JobStoreCMT { 60 61 67 public static final String TX_DATA_SOURCE_PREFIX = "springTxDataSource."; 68 69 75 public static final String NON_TX_DATA_SOURCE_PREFIX = "springNonTxDataSource."; 76 77 78 private DataSource dataSource; 79 80 81 public void initialize(ClassLoadHelper loadHelper, SchedulerSignaler signaler) 82 throws SchedulerConfigException { 83 84 this.dataSource = SchedulerFactoryBean.getConfigTimeDataSource(); 86 if (this.dataSource == null) { 87 throw new SchedulerConfigException( 88 "No local DataSource found for configuration - " + 89 "'dataSource' property must be set on SchedulerFactoryBean"); 90 } 91 92 setDataSource(TX_DATA_SOURCE_PREFIX + getInstanceName()); 94 setDontSetAutoCommitFalse(true); 95 96 DBConnectionManager.getInstance().addConnectionProvider( 98 TX_DATA_SOURCE_PREFIX + getInstanceName(), 99 new ConnectionProvider() { 100 public Connection getConnection() throws SQLException { 101 return DataSourceUtils.doGetConnection(dataSource); 103 } 104 public void shutdown() { 105 } 107 } 108 ); 109 110 DataSource nonTxDataSource = SchedulerFactoryBean.getConfigTimeNonTransactionalDataSource(); 113 final DataSource nonTxDataSourceToUse = 114 (nonTxDataSource != null ? nonTxDataSource : this.dataSource); 115 116 setNonManagedTXDataSource(NON_TX_DATA_SOURCE_PREFIX + getInstanceName()); 118 119 DBConnectionManager.getInstance().addConnectionProvider( 121 NON_TX_DATA_SOURCE_PREFIX + getInstanceName(), 122 new ConnectionProvider() { 123 public Connection getConnection() throws SQLException { 124 return nonTxDataSourceToUse.getConnection(); 126 } 127 public void shutdown() { 128 } 130 } 131 ); 132 133 super.initialize(loadHelper, signaler); 134 } 135 136 protected void closeConnection(Connection con) { 137 DataSourceUtils.releaseConnection(con, this.dataSource); 139 } 140 141 } 142 | Popular Tags |