1 24 25 package org.objectweb.cjdbc.controller.scheduler.raidb1; 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 RAIDb1PessimisticTransactionLevelScheduler 51 extends AbstractScheduler 52 { 53 54 64 private long requestId; 65 66 TransactionExclusiveLock lock = new TransactionExclusiveLock(); 67 68 72 75 public RAIDb1PessimisticTransactionLevelScheduler() 76 { 77 super(RAIDbLevels.RAIDb1, ParsingGranularities.NO_PARSING); 78 } 79 80 84 90 public final void scheduleReadRequest(SelectRequest request) 91 throws SQLException 92 { 93 synchronized (this) 94 { 95 request.setId(requestId++); 96 } 97 } 98 99 102 public final void readCompletedNotify(SelectRequest request) 103 { 104 } 105 106 111 public void scheduleNonSuspendedWriteRequest(AbstractWriteRequest request) 112 throws SQLException 113 { 114 if (request.isCreate()) 115 { 116 synchronized (this) 117 { 118 request.setId(requestId++); 119 } 120 return; 121 } 122 123 if (lock.acquire(request)) 124 { 125 synchronized (this) 126 { 127 request.setId(requestId++); 128 } 129 if (logger.isDebugEnabled()) 130 logger.debug("Request " + request.getId() + " scheduled for write (" 131 + getPendingWrites() + " pending writes)"); 132 } 133 else 134 { 135 if (logger.isWarnEnabled()) 136 logger.warn("Request " + request.getId() + " timed out (" 137 + request.getTimeout() + " s)"); 138 throw new SQLException ("Timeout (" + request.getTimeout() 139 + ") for request: " 140 + request.getSQLShortForm(Constants.SQL_SHORT_FORM_LENGTH)); 141 } 142 } 143 144 147 public final synchronized void notifyWriteCompleted( 148 AbstractWriteRequest request) 149 { 150 if (request.isAutoCommit() && (!request.isCreate())) 153 releaseLock(request.getTransactionId()); 154 } 155 156 159 public final synchronized void scheduleNonSuspendedStoredProcedure( 160 StoredProcedure proc) throws SQLException , RollbackException 161 { 162 if (lock.acquire(proc)) 163 { 164 synchronized (this) 165 { 166 proc.setId(requestId++); 167 } 168 if (logger.isDebugEnabled()) 169 logger.debug("Stored procedure " + proc.getId() 170 + " scheduled for write (" + getPendingWrites() 171 + " pending writes)"); 172 } 173 else 174 { 175 if (logger.isWarnEnabled()) 176 logger.warn("Stored procedure " + proc.getId() + " timed out (" 177 + proc.getTimeout() + " s)"); 178 throw new SQLException ("Timeout (" + proc.getTimeout() 179 + ") for request: " 180 + proc.getSQLShortForm(Constants.SQL_SHORT_FORM_LENGTH)); 181 } 182 } 183 184 187 public final void notifyStoredProcedureCompleted(StoredProcedure proc) 188 { 189 if (proc.isAutoCommit() && (!proc.isCreate())) 192 releaseLock(proc.getTransactionId()); 193 } 194 195 199 202 protected final void commitTransaction(long transactionId) 203 { 204 if (lock.isLocked()) 205 releaseLock(transactionId); 206 } 208 209 212 protected final void rollbackTransaction(long transactionId) 213 { 214 if (lock.isLocked()) 215 releaseLock(transactionId); 216 } 218 219 223 protected final void rollbackTransaction(long transactionId, 224 String savepointName) 225 { 226 } 227 228 232 protected final void setSavepointTransaction(long transactionId, String name) 233 { 234 } 235 236 240 protected final void releaseSavepointTransaction(long transactionId, 241 String name) 242 { 243 } 244 245 250 private void releaseLock(long transactionId) 251 { 252 if (lock.isLocked()) 254 { 255 if (lock.getLocker() == transactionId) 256 lock.release(); 257 258 else if (logger.isDebugEnabled()) 262 logger.debug("Transaction " + transactionId 263 + " wants to release the lock held by transaction " 264 + lock.getLocker()); 265 } 266 else if (logger.isDebugEnabled()) 267 logger.warn("Transaction " + transactionId 268 + " tries to release a lock that has not been acquired."); 269 } 270 271 277 public String getXmlImpl() 278 { 279 return "<" + DatabasesXmlTags.ELT_RAIDb1Scheduler + " " 280 + DatabasesXmlTags.ATT_level + "=\"" 281 + DatabasesXmlTags.VAL_pessimisticTransaction + "\"/>"; 282 } 283 } 284 | Popular Tags |