KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2004-2006 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 package org.quartz.impl.jdbcjobstore;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.SQLException JavaDoc;
22
23 /**
24  * Provide thread/resource locking in order to protect
25  * resources from being altered by multiple threads at the same time using
26  * a db row update.
27  *
28  * <p>
29  * <b>Note:</b> This Semaphore implementation is useful for databases that do
30  * not support row locking via "SELECT FOR UPDATE" type syntax, for example
31  * Microsoft SQLServer (MSSQL).
32  * </p>
33  */

34 public class UpdateLockRowSemaphore extends DBSemaphore {
35
36     /*
37      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
38      *
39      * Constants.
40      *
41      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42      */

43
44     public static final String JavaDoc UPDATE_FOR_LOCK =
45         "UPDATE " + TABLE_PREFIX_SUBST + TABLE_LOCKS +
46         " SET " + COL_LOCK_NAME + " = " + COL_LOCK_NAME +
47         " WHERE " + COL_LOCK_NAME + " = ? ";
48
49
50     /*
51      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52      *
53      * Constructors.
54      *
55      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
56      */

57
58     public UpdateLockRowSemaphore() {
59         super(DEFAULT_TABLE_PREFIX, null, UPDATE_FOR_LOCK);
60     }
61
62     /*
63      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
64      *
65      * Interface.
66      *
67      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68      */

69
70     /**
71      * Execute the SQL select for update that will lock the proper database row.
72      */

73     protected void executeSQL(Connection JavaDoc conn, String JavaDoc lockName, String JavaDoc expandedSQL) throws LockException {
74         PreparedStatement JavaDoc ps = null;
75
76         try {
77             ps = conn.prepareStatement(expandedSQL);
78             ps.setString(1, lockName);
79
80             if (getLog().isDebugEnabled()) {
81                 getLog().debug(
82                     "Lock '" + lockName + "' is being obtained: " +
83                     Thread.currentThread().getName());
84             }
85             
86             int numUpdate = ps.executeUpdate();
87             
88             if (numUpdate < 1) {
89                 throw new SQLException JavaDoc(Util.rtp(
90                     "No row exists in table " + TABLE_PREFIX_SUBST + TABLE_LOCKS +
91                     " for lock named: " + lockName, getTablePrefix()));
92             }
93         } catch (SQLException JavaDoc sqle) {
94             //Exception src =
95
// (Exception)getThreadLocksObtainer().get(lockName);
96
//if(src != null)
97
// src.printStackTrace();
98
//else
99
// System.err.println("--- ***************** NO OBTAINER!");
100

101             if(getLog().isDebugEnabled()) {
102                 getLog().debug(
103                     "Lock '" + lockName + "' was not obtained by: " +
104                     Thread.currentThread().getName());
105             }
106             
107             throw new LockException(
108                 "Failure obtaining db row lock: " + sqle.getMessage(), sqle);
109         } finally {
110             if (ps != null) {
111                 try {
112                     ps.close();
113                 } catch (Exception JavaDoc ignore) {
114                 }
115             }
116         }
117     }
118     
119     protected String JavaDoc getUpdateLockRowSQL() {
120         return getSQL();
121     }
122
123     public void setUpdateLockRowSQL(String JavaDoc updateLockRowSQL) {
124         setSQL(updateLockRowSQL);
125     }
126 }
127
Popular Tags