KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > scheduling > quartz > LocalDataSourceJobStore


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.scheduling.quartz;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.SQLException JavaDoc;
21
22 import javax.sql.DataSource JavaDoc;
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 /**
34  * Subclass of Quartz' JobStoreCMT class that delegates to a Spring-managed
35  * DataSource instead of using a Quartz-managed connection pool. This JobStore
36  * will be used if SchedulerFactoryBean's "dataSource" property is set.
37  *
38  * <p>Supports both transactional and non-transactional DataSource access.
39  * With a non-XA DataSource and local Spring transactions, a single DataSource
40  * argument is sufficient. In case of an XA DataSource and global JTA transactions,
41  * SchedulerFactoryBean's "nonTransactionalDataSource" property should be set,
42  * passing in a non-XA DataSource that will not participate in global transactions.
43  *
44  * <p>Operations performed by this JobStore will properly participate in any
45  * kind of Spring-managed transaction, as it uses Spring's DataSourceUtils
46  * connection handling methods that are aware of a current transaction.
47  *
48  * <p>Note that all Quartz Scheduler operations that affect the persistent
49  * job store should usually be performed within active transaction, as they
50  * assume to get proper locks etc.
51  *
52  * @author Juergen Hoeller
53  * @since 1.1
54  * @see SchedulerFactoryBean#setDataSource
55  * @see SchedulerFactoryBean#setNonTransactionalDataSource
56  * @see org.springframework.jdbc.datasource.DataSourceUtils#doGetConnection
57  * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection
58  */

59 public class LocalDataSourceJobStore extends JobStoreCMT {
60
61     /**
62      * Name used for the transactional ConnectionProvider for Quartz.
63      * This provider will delegate to the local Spring-managed DataSource.
64      * @see org.quartz.utils.DBConnectionManager#addConnectionProvider
65      * @see SchedulerFactoryBean#setDataSource
66      */

67     public static final String JavaDoc TX_DATA_SOURCE_PREFIX = "springTxDataSource.";
68
69     /**
70      * Name used for the non-transactional ConnectionProvider for Quartz.
71      * This provider will delegate to the local Spring-managed DataSource.
72      * @see org.quartz.utils.DBConnectionManager#addConnectionProvider
73      * @see SchedulerFactoryBean#setDataSource
74      */

75     public static final String JavaDoc NON_TX_DATA_SOURCE_PREFIX = "springNonTxDataSource.";
76
77
78     private DataSource JavaDoc dataSource;
79
80
81     public void initialize(ClassLoadHelper loadHelper, SchedulerSignaler signaler)
82         throws SchedulerConfigException {
83
84         // Absolutely needs thread-bound DataSource to initialize.
85
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         // Configure transactional connection settings for Quartz.
93
setDataSource(TX_DATA_SOURCE_PREFIX + getInstanceName());
94         setDontSetAutoCommitFalse(true);
95
96         // Register transactional ConnectionProvider for Quartz.
97
DBConnectionManager.getInstance().addConnectionProvider(
98                 TX_DATA_SOURCE_PREFIX + getInstanceName(),
99                 new ConnectionProvider() {
100                     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
101                         // Return a transactional Connection, if any.
102
return DataSourceUtils.doGetConnection(dataSource);
103                     }
104                     public void shutdown() {
105                         // Do nothing - a Spring-managed DataSource has its own lifecycle.
106
}
107                 }
108         );
109
110         // Non-transactional DataSource is optional: fall back to default
111
// DataSource if not explicitly specified.
112
DataSource JavaDoc nonTxDataSource = SchedulerFactoryBean.getConfigTimeNonTransactionalDataSource();
113         final DataSource JavaDoc nonTxDataSourceToUse =
114                 (nonTxDataSource != null ? nonTxDataSource : this.dataSource);
115
116         // Configure non-transactional connection settings for Quartz.
117
setNonManagedTXDataSource(NON_TX_DATA_SOURCE_PREFIX + getInstanceName());
118
119         // Register non-transactional ConnectionProvider for Quartz.
120
DBConnectionManager.getInstance().addConnectionProvider(
121                 NON_TX_DATA_SOURCE_PREFIX + getInstanceName(),
122                 new ConnectionProvider() {
123                     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
124                         // Always return a non-transactional Connection.
125
return nonTxDataSourceToUse.getConnection();
126                     }
127                     public void shutdown() {
128                         // Do nothing - a Spring-managed DataSource has its own lifecycle.
129
}
130                 }
131         );
132
133         super.initialize(loadHelper, signaler);
134     }
135
136     protected void closeConnection(Connection JavaDoc con) {
137         // Will work for transactional and non-transactional connections.
138
DataSourceUtils.releaseConnection(con, this.dataSource);
139     }
140
141 }
142
Popular Tags