1 24 25 package org.objectweb.cjdbc.controller.scheduler.raidb2; 26 27 import java.sql.SQLException ; 28 29 import org.objectweb.cjdbc.common.exceptions.RollbackException; 30 import org.objectweb.cjdbc.common.sql.AbstractWriteRequest; 31 import org.objectweb.cjdbc.common.sql.ParsingGranularities; 32 import org.objectweb.cjdbc.common.sql.SelectRequest; 33 import org.objectweb.cjdbc.common.sql.StoredProcedure; 34 import org.objectweb.cjdbc.common.util.Constants; 35 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags; 36 import org.objectweb.cjdbc.controller.requestmanager.RAIDbLevels; 37 import org.objectweb.cjdbc.controller.scheduler.AbstractScheduler; 38 import org.objectweb.cjdbc.controller.scheduler.schema.TransactionExclusiveLock; 39 40 50 public class RAIDb2PessimisticTransactionLevelScheduler 51 extends AbstractScheduler 52 { 53 54 64 private long requestId; 65 private TransactionExclusiveLock lock = new TransactionExclusiveLock(); 66 67 71 74 public RAIDb2PessimisticTransactionLevelScheduler() 75 { 76 super(RAIDbLevels.RAIDb2, ParsingGranularities.NO_PARSING); 77 } 78 79 83 89 public final void scheduleReadRequest(SelectRequest request) 90 throws SQLException 91 { 92 synchronized (this) 93 { 94 request.setId(requestId++); 95 } 96 } 97 98 101 public final void readCompletedNotify(SelectRequest request) 102 { 103 } 104 105 110 public void scheduleNonSuspendedWriteRequest(AbstractWriteRequest request) 111 throws SQLException 112 { 113 if (request.isCreate()) 114 { 115 synchronized (this) 116 { 117 request.setId(requestId++); 118 } 119 return; 120 } 121 122 if (lock.acquire(request)) 123 { 124 synchronized (this) 125 { 126 request.setId(requestId++); 127 } 128 if (logger.isDebugEnabled()) 129 logger.debug("Request " + request.getId() + " scheduled for write (" 130 + getPendingWrites() + " pending writes)"); 131 } 132 else 133 { 134 if (logger.isWarnEnabled()) 135 logger.warn("Request " + request.getId() + " timed out (" 136 + request.getTimeout() + " s)"); 137 throw new SQLException ("Timeout (" + request.getTimeout() 138 + ") for request: " + request.getId()); 139 } 140 } 141 142 145 public final synchronized void notifyWriteCompleted( 146 AbstractWriteRequest request) 147 { 148 if (request.isAutoCommit() && (!request.isCreate())) 151 releaseLock(request.getTransactionId()); 152 } 153 154 157 public final synchronized void scheduleNonSuspendedStoredProcedure( 158 StoredProcedure proc) throws SQLException , RollbackException 159 { 160 if (lock.acquire(proc)) 161 { 162 synchronized (this) 163 { 164 proc.setId(requestId++); 165 } 166 if (logger.isDebugEnabled()) 167 logger.debug("Stored procedure " + proc.getId() 168 + " scheduled for write (" + getPendingWrites() 169 + " pending writes)"); 170 } 171 else 172 { 173 if (logger.isWarnEnabled()) 174 logger.warn("Stored procedure " + proc.getId() + " timed out (" 175 + proc.getTimeout() + " s)"); 176 throw new SQLException ("Timeout (" + proc.getTimeout() 177 + ") for request: " 178 + proc.getSQLShortForm(Constants.SQL_SHORT_FORM_LENGTH)); 179 } 180 } 181 182 185 public final void notifyStoredProcedureCompleted(StoredProcedure proc) 186 { 187 if (proc.isAutoCommit() && (!proc.isCreate())) 190 releaseLock(proc.getTransactionId()); 191 } 192 193 197 200 protected final void commitTransaction(long transactionId) 201 { 202 if (lock.isLocked()) 203 releaseLock(transactionId); 204 } 206 207 210 protected final void rollbackTransaction(long transactionId) 211 { 212 if (lock.isLocked()) 213 releaseLock(transactionId); 214 } 216 217 221 protected final void rollbackTransaction(long transactionId, 222 String savepointName) 223 { 224 } 225 226 230 protected final void setSavepointTransaction(long transactionId, String name) 231 { 232 } 233 234 238 protected final void releaseSavepointTransaction(long transactionId, 239 String name) 240 { 241 } 242 243 248 private void releaseLock(long transactionId) 249 { 250 if (lock.isLocked()) 252 { 253 if (lock.getLocker() == transactionId) 254 lock.release(); 255 256 else if (logger.isDebugEnabled()) 260 logger.debug("Transaction " + transactionId 261 + " wants to release the lock held by transaction " 262 + lock.getLocker()); 263 } 264 else if (logger.isDebugEnabled()) 265 logger.warn("Transaction " + transactionId 266 + " tries to release a lock that has not been acquired."); 267 } 268 269 273 276 public String getXmlImpl() 277 { 278 return "<" + DatabasesXmlTags.ELT_RAIDb2Scheduler + " " 279 + DatabasesXmlTags.ATT_level + "=\"" 280 + DatabasesXmlTags.VAL_pessimisticTransaction + "\"/>"; 281 } 282 } 283 | Popular Tags |