KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > impl > jdbcjobstore > JobStoreCMT


1 /*
2  * Copyright 2004-2005 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  *
16  */

17
18 /*
19  * Previously Copyright (c) 2001-2004 James House
20  */

21 package org.quartz.impl.jdbcjobstore;
22
23 import java.sql.Connection JavaDoc;
24 import java.sql.SQLException JavaDoc;
25
26 import org.quartz.JobPersistenceException;
27 import org.quartz.SchedulerConfigException;
28 import org.quartz.spi.ClassLoadHelper;
29 import org.quartz.spi.SchedulerSignaler;
30 import org.quartz.utils.DBConnectionManager;
31
32 /**
33  * <p>
34  * <code>JobStoreCMT</code> is meant to be used in an application-server
35  * environment that provides container-managed-transactions. No commit /
36  * rollback will be1 handled by this class.
37  * </p>
38  *
39  * <p>
40  * If you need commit / rollback, use <code>{@link
41  * org.quartz.impl.jdbcjobstore.JobStoreTX}</code>
42  * instead.
43  * </p>
44  *
45  * @author <a HREF="mailto:jeff@binaryfeed.org">Jeffrey Wescott</a>
46  * @author James House
47  * @author Srinivas Venkatarangaiah
48  *
49  */

50 public class JobStoreCMT extends JobStoreSupport {
51
52     /*
53      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
54      *
55      * Data members.
56      *
57      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
58      */

59
60     protected String JavaDoc nonManagedTxDsName;
61
62     // Great name huh?
63
protected boolean dontSetNonManagedTXConnectionAutoCommitFalse = false;
64
65     
66     protected boolean setTxIsolationLevelReadCommitted = false;
67     
68     /*
69      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70      *
71      * Interface.
72      *
73      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74      */

75
76     /**
77      * <p>
78      * Set the name of the <code>DataSource</code> that should be used for
79      * performing database functions.
80      * </p>
81      */

82     public void setNonManagedTXDataSource(String JavaDoc nonManagedTxDsName) {
83         this.nonManagedTxDsName = nonManagedTxDsName;
84     }
85
86     /**
87      * <p>
88      * Get the name of the <code>DataSource</code> that should be used for
89      * performing database functions.
90      * </p>
91      */

92     public String JavaDoc getNonManagedTXDataSource() {
93         return nonManagedTxDsName;
94     }
95
96     public boolean isDontSetNonManagedTXConnectionAutoCommitFalse() {
97         return dontSetNonManagedTXConnectionAutoCommitFalse;
98     }
99
100     /**
101      * Don't call set autocommit(false) on connections obtained from the
102      * DataSource. This can be helpfull in a few situations, such as if you
103      * have a driver that complains if it is called when it is already off.
104      *
105      * @param b
106      */

107     public void setDontSetNonManagedTXConnectionAutoCommitFalse(boolean b) {
108         dontSetNonManagedTXConnectionAutoCommitFalse = b;
109     }
110
111
112     public boolean isTxIsolationLevelReadCommitted() {
113         return setTxIsolationLevelReadCommitted;
114     }
115
116     /**
117      * Set the transaction isolation level of DB connections to sequential.
118      *
119      * @param b
120      */

121     public void setTxIsolationLevelReadCommitted(boolean b) {
122         setTxIsolationLevelReadCommitted = b;
123     }
124     
125
126     public void initialize(ClassLoadHelper loadHelper,
127             SchedulerSignaler signaler) throws SchedulerConfigException {
128
129         if (nonManagedTxDsName == null) {
130             throw new SchedulerConfigException(
131                 "Non-ManagedTX DataSource name not set! " +
132                 "If your 'org.quartz.jobStore.dataSource' is XA, then set " +
133                 "'org.quartz.jobStore.nonManagedTXDataSource' to a non-XA "+
134                 "datasource (for the same DB). " +
135                 "Otherwise, you can set them to be the same.");
136         }
137
138         if (getLockHandler() == null) {
139             // If the user hasn't specified an explicit lock handler,
140
// then we *must* use DB locks with CMT...
141
setUseDBLocks(true);
142         }
143
144         super.initialize(loadHelper, signaler);
145
146         getLog().info("JobStoreCMT initialized.");
147     }
148     
149     public void shutdown() {
150
151         super.shutdown();
152         
153         try {
154             DBConnectionManager.getInstance().shutdown(getNonManagedTXDataSource());
155         } catch (SQLException JavaDoc sqle) {
156             getLog().warn("Database connection shutdown unsuccessful.", sqle);
157         }
158     }
159
160     protected Connection JavaDoc getNonManagedTXConnection()
161         throws JobPersistenceException {
162         Connection JavaDoc conn = null;
163         try {
164             conn = DBConnectionManager.getInstance().getConnection(
165                     getNonManagedTXDataSource());
166         } catch (SQLException JavaDoc sqle) {
167             throw new JobPersistenceException(
168                 "Failed to obtain DB connection from data source '"
169                         + getNonManagedTXDataSource() + "': "
170                         + sqle.toString(), sqle);
171         } catch (Throwable JavaDoc e) {
172             throw new JobPersistenceException(
173                 "Failed to obtain DB connection from data source '"
174                         + getNonManagedTXDataSource() + "': "
175                         + e.toString(), e,
176                 JobPersistenceException.ERR_PERSISTENCE_CRITICAL_FAILURE);
177         }
178
179         if (conn == null) {
180             throw new JobPersistenceException(
181                 "Could not get connection from DataSource '"
182                         + getNonManagedTXDataSource() + "'");
183         }
184
185         // Protect connection attributes we might change.
186
conn = getAttributeRestoringConnection(conn);
187         
188         // Set any connection connection attributes we are to override.
189
try {
190             if (!isDontSetNonManagedTXConnectionAutoCommitFalse()) {
191                 conn.setAutoCommit(false);
192             }
193             
194             if (isTxIsolationLevelReadCommitted()) {
195                 conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
196             }
197         } catch (SQLException JavaDoc sqle) {
198             getLog().warn("Failed to override connection auto commit/transaction isolation.", sqle);
199         } catch (Throwable JavaDoc e) {
200             try { conn.close(); } catch(Throwable JavaDoc tt) {}
201             
202             throw new JobPersistenceException(
203                 "Failure setting up connection.", e);
204         }
205         
206         return conn;
207     }
208     
209     /**
210      * Execute the given callback having optionally aquired the given lock.
211      * Because CMT assumes that the connection is already part of a managed
212      * transaction, it does not attempt to commit or rollback the
213      * enclosing transaction.
214      *
215      * @param lockName The name of the lock to aquire, for example
216      * "TRIGGER_ACCESS". If null, then no lock is aquired, but the
217      * txCallback is still executed in a transaction.
218      *
219      * @see JobStoreSupport#executeInNonManagedTXLock(String, TransactionCallback)
220      * @see JobStoreTX#executeInLock(String, TransactionCallback)
221      * @see JobStoreSupport#getNonManagedTXConnection()
222      * @see JobStoreSupport#getConnection()
223      */

224     protected Object JavaDoc executeInLock(
225             String JavaDoc lockName,
226             TransactionCallback txCallback) throws JobPersistenceException {
227         boolean transOwner = false;
228         Connection JavaDoc conn = null;
229         try {
230             if (lockName != null) {
231                 // If we aren't using db locks, then delay getting DB connection
232
// until after aquiring the lock since it isn't needed.
233
if (getLockHandler().requiresConnection()) {
234                     conn = getConnection();
235                 }
236                 
237                 transOwner = getLockHandler().obtainLock(conn, lockName);
238             }
239
240             if (conn == null) {
241                 conn = getConnection();
242             }
243
244             return txCallback.execute(conn);
245         } finally {
246             try {
247                 releaseLock(conn, LOCK_TRIGGER_ACCESS, transOwner);
248             } finally {
249                 cleanupConnection(conn);
250             }
251         }
252     }
253 }
254
255 // EOF
256
Popular Tags