1 24 25 package org.objectweb.cjdbc.controller.scheduler.raidb0; 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 RAIDb0PessimisticTransactionLevelScheduler 51 extends AbstractScheduler 52 { 53 54 64 private long requestId; 65 private TransactionExclusiveLock lock = new TransactionExclusiveLock(); 66 67 71 74 public RAIDb0PessimisticTransactionLevelScheduler() 75 { 76 super(RAIDbLevels.RAIDb0, 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 112 public void scheduleNonSuspendedWriteRequest(AbstractWriteRequest request) 113 throws SQLException 114 { 115 if (request.isCreate()) 116 { 117 synchronized (this) 118 { 119 request.setId(requestId++); 120 } 121 return; 122 } 123 124 if (lock.acquire(request)) 125 { 126 synchronized (this) 127 { 128 request.setId(requestId++); 129 } 130 if (logger.isDebugEnabled()) 131 logger.debug("Request " + request.getId() + " scheduled for write (" 132 + getPendingWrites() + " pending writes)"); 133 } 134 else 135 { 136 if (logger.isWarnEnabled()) 137 logger.warn("Request " + request.getId() + " timed out (" 138 + request.getTimeout() + " s)"); 139 throw new SQLException ("Timeout (" + request.getTimeout() 140 + ") for request: " 141 + request.getSQLShortForm(Constants.SQL_SHORT_FORM_LENGTH)); 142 } 143 } 144 145 148 public final synchronized void notifyWriteCompleted( 149 AbstractWriteRequest request) 150 { 151 if (request.isAutoCommit() && (!request.isCreate())) 154 releaseLock(request.getTransactionId()); 155 } 156 157 160 public final synchronized void scheduleNonSuspendedStoredProcedure( 161 StoredProcedure proc) throws SQLException , RollbackException 162 { 163 if (lock.acquire(proc)) 164 { 165 synchronized (this) 166 { 167 proc.setId(requestId++); 168 } 169 if (logger.isDebugEnabled()) 170 logger.debug("Stored procedure " + proc.getId() 171 + " scheduled for write (" + getPendingWrites() 172 + " pending writes)"); 173 } 174 else 175 { 176 if (logger.isWarnEnabled()) 177 logger.warn("Stored procedure " + proc.getId() + " timed out (" 178 + proc.getTimeout() + " s)"); 179 throw new SQLException ("Timeout (" + proc.getTimeout() 180 + ") for request: " 181 + proc.getSQLShortForm(Constants.SQL_SHORT_FORM_LENGTH)); 182 } 183 } 184 185 188 public final void notifyStoredProcedureCompleted(StoredProcedure proc) 189 { 190 if (proc.isAutoCommit() && (!proc.isCreate())) 193 releaseLock(proc.getTransactionId()); 194 } 195 196 200 203 protected final void commitTransaction(long transactionId) 204 { 205 releaseLock(transactionId); 206 } 207 208 211 protected final void rollbackTransaction(long transactionId) 212 { 213 releaseLock(transactionId); 214 } 215 216 220 protected final void rollbackTransaction(long transactionId, 221 String savepointName) 222 { 223 } 224 225 229 protected final void setSavepointTransaction(long transactionId, String name) 230 { 231 } 232 233 237 protected final void releaseSavepointTransaction(long transactionId, 238 String name) 239 { 240 } 241 242 247 private void releaseLock(long transactionId) 248 { 249 if (lock.isLocked()) 251 { 252 if (lock.getLocker() == transactionId) 253 lock.release(); 254 255 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 StringBuffer info = new StringBuffer (); 279 info.append("<" + DatabasesXmlTags.ELT_RAIDb0Scheduler + " " 280 + DatabasesXmlTags.ATT_level + "=\"" 281 + DatabasesXmlTags.VAL_pessimisticTransaction + "\"/>"); 282 return info.toString(); 283 } 284 } 285 | Popular Tags |