KickJava   Java API By Example, From Geeks To Geeks.

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


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.PreparedStatement JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27
28 /**
29  * Internal database based lock handler for providing thread/resource locking
30  * in order to protect resources from being altered by multiple threads at the
31  * same time.
32  *
33  * @author jhouse
34  */

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

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

56
57     /**
58      * This constructor is for using the <code>StdRowLockSemaphore</code> as
59      * a bean.
60      */

61     public StdRowLockSemaphore() {
62         super(DEFAULT_TABLE_PREFIX, null, SELECT_FOR_LOCK);
63     }
64     
65     public StdRowLockSemaphore(String JavaDoc tablePrefix, String JavaDoc selectWithLockSQL) {
66         super(tablePrefix, selectWithLockSQL, SELECT_FOR_LOCK);
67     }
68
69     /*
70      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71      *
72      * Interface.
73      *
74      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
75      */

76
77     /**
78      * Execute the SQL select for update that will lock the proper database row.
79      */

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

106             if (getLog().isDebugEnabled()) {
107                 getLog().debug(
108                     "Lock '" + lockName + "' was not obtained by: " +
109                     Thread.currentThread().getName());
110             }
111             
112             throw new LockException("Failure obtaining db row lock: "
113                     + sqle.getMessage(), sqle);
114         } finally {
115             if (rs != null) {
116                 try {
117                     rs.close();
118                 } catch (Exception JavaDoc ignore) {
119                 }
120             }
121             if (ps != null) {
122                 try {
123                     ps.close();
124                 } catch (Exception JavaDoc ignore) {
125                 }
126             }
127         }
128     }
129
130     protected String JavaDoc getSelectWithLockSQL() {
131         return getSQL();
132     }
133
134     public void setSelectWithLockSQL(String JavaDoc selectWithLockSQL) {
135         setSQL(selectWithLockSQL);
136     }
137 }
138
Popular Tags