1 17 package org.quartz.impl.jdbcjobstore; 18 19 import java.sql.Connection ; 20 import java.util.HashSet ; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 25 30 public abstract class DBSemaphore implements Semaphore, Constants, 31 StdJDBCConstants, TablePrefixAware { 32 33 private final Log log = LogFactory.getLog(getClass()); 34 35 42 43 ThreadLocal lockOwners = new ThreadLocal (); 44 45 private String sql; 46 47 private String tablePrefix; 48 49 private String expandedSQL; 50 51 58 59 public DBSemaphore(String tablePrefix, String sql, String defaultSQL) { 60 this.sql = defaultSQL; 61 this.tablePrefix = tablePrefix; 62 setSQL(sql); 63 } 64 65 72 73 protected Log getLog() { 74 return log; 75 } 76 77 private HashSet getThreadLocks() { 78 HashSet threadLocks = (HashSet ) lockOwners.get(); 79 if (threadLocks == null) { 80 threadLocks = new HashSet (); 81 lockOwners.set(threadLocks); 82 } 83 return threadLocks; 84 } 85 86 89 protected abstract void executeSQL( 90 Connection conn, String lockName, String expandedSQL) throws LockException; 91 92 98 public boolean obtainLock(Connection conn, String lockName) 99 throws LockException { 100 101 lockName = lockName.intern(); 102 103 Log log = getLog(); 104 105 if(log.isDebugEnabled()) { 106 log.debug( 107 "Lock '" + lockName + "' is desired by: " 108 + Thread.currentThread().getName()); 109 } 110 if (!isLockOwner(conn, lockName)) { 111 112 executeSQL(conn, lockName, expandedSQL); 113 114 if(log.isDebugEnabled()) { 115 log.debug( 116 "Lock '" + lockName + "' given to: " 117 + Thread.currentThread().getName()); 118 } 119 getThreadLocks().add(lockName); 120 } else if(log.isDebugEnabled()) { 123 log.debug( 124 "Lock '" + lockName + "' Is already owned by: " 125 + Thread.currentThread().getName()); 126 } 127 128 return true; 129 } 130 131 132 136 public void releaseLock(Connection conn, String lockName) { 137 138 lockName = lockName.intern(); 139 140 if (isLockOwner(conn, lockName)) { 141 if(getLog().isDebugEnabled()) { 142 getLog().debug( 143 "Lock '" + lockName + "' returned by: " 144 + Thread.currentThread().getName()); 145 } 146 getThreadLocks().remove(lockName); 147 } else if (getLog().isDebugEnabled()) { 149 getLog().warn( 150 "Lock '" + lockName + "' attempt to return by: " 151 + Thread.currentThread().getName() 152 + " -- but not owner!", 153 new Exception ("stack-trace of wrongful returner")); 154 } 155 } 156 157 161 public boolean isLockOwner(Connection conn, String lockName) { 162 lockName = lockName.intern(); 163 164 return getThreadLocks().contains(lockName); 165 } 166 167 170 public boolean requiresConnection() { 171 return true; 172 } 173 174 protected String getSQL() { 175 return sql; 176 } 177 178 protected void setSQL(String sql) { 179 if ((sql != null) && (sql.trim().length() != 0)) { 180 this.sql = sql; 181 } 182 183 setExpandedSQL(); 184 } 185 186 private void setExpandedSQL() { 187 if (getTablePrefix() != null) { 188 expandedSQL = Util.rtp(this.sql, getTablePrefix()); 189 } 190 } 191 192 protected String getTablePrefix() { 193 return tablePrefix; 194 } 195 196 public void setTablePrefix(String tablePrefix) { 197 this.tablePrefix = tablePrefix; 198 199 setExpandedSQL(); 200 } 201 } 202 | Popular Tags |