1 24 25 package org.objectweb.cjdbc.controller.scheduler.singledb; 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 48 public class SingleDBPessimisticTransactionLevelScheduler 49 extends AbstractScheduler 50 { 51 52 62 private long requestId; 63 TransactionExclusiveLock lock = new TransactionExclusiveLock(); 64 65 69 72 public SingleDBPessimisticTransactionLevelScheduler() 73 { 74 super(RAIDbLevels.SingleDB, ParsingGranularities.NO_PARSING); 75 } 76 77 81 87 public final synchronized void scheduleReadRequest(SelectRequest request) 88 throws SQLException 89 { 90 request.setId(requestId++); 91 } 92 93 96 public final void readCompletedNotify(SelectRequest request) 97 { 98 } 99 100 106 public void scheduleNonSuspendedWriteRequest(AbstractWriteRequest request) 107 throws SQLException 108 { 109 if (lock.acquire(request)) 110 { 111 synchronized (this) 112 { 113 request.setId(requestId++); 114 } 115 if (logger.isDebugEnabled()) 116 logger.debug("Request " + request.getId() + " scheduled for write (" 117 + getPendingWrites() + " pending writes)"); 118 } 119 else 120 { 121 if (logger.isWarnEnabled()) 122 logger.warn("Request " + request.getId() + " timed out (" 123 + request.getTimeout() + " s)"); 124 throw new SQLException ("Timeout (" + request.getTimeout() 125 + ") for request: " + request.getId()); 126 } 127 } 128 129 132 public final synchronized void notifyWriteCompleted( 133 AbstractWriteRequest request) 134 { 135 if (request.isAutoCommit()) 138 releaseLock(request.getTransactionId()); 139 } 140 141 144 public final synchronized void scheduleNonSuspendedStoredProcedure( 145 StoredProcedure proc) throws SQLException , RollbackException 146 { 147 if (lock.acquire(proc)) 148 { 149 synchronized (this) 150 { 151 proc.setId(requestId++); 152 } 153 if (logger.isDebugEnabled()) 154 logger.debug("Stored procedure " + proc.getId() 155 + " scheduled for write (" + getPendingWrites() 156 + " pending writes)"); 157 } 158 else 159 { 160 if (logger.isWarnEnabled()) 161 logger.warn("Stored procedure " + proc.getId() + " timed out (" 162 + proc.getTimeout() + " s)"); 163 throw new SQLException ("Timeout (" + proc.getTimeout() 164 + ") for request: " 165 + proc.getSQLShortForm(Constants.SQL_SHORT_FORM_LENGTH)); 166 } 167 } 168 169 172 public final void notifyStoredProcedureCompleted(StoredProcedure proc) 173 { 174 if (proc.isAutoCommit() && (!proc.isCreate())) 177 releaseLock(proc.getTransactionId()); 178 } 179 180 184 187 protected final void commitTransaction(long transactionId) 188 { 189 releaseLock(transactionId); 190 } 191 192 195 protected final void rollbackTransaction(long transactionId) 196 { 197 releaseLock(transactionId); 198 } 199 200 204 protected final void rollbackTransaction(long transactionId, 205 String savepointName) 206 { 207 } 208 209 213 protected final void setSavepointTransaction(long transactionId, String name) 214 { 215 } 216 217 221 protected final void releaseSavepointTransaction(long transactionId, 222 String name) 223 { 224 } 225 226 231 private void releaseLock(long transactionId) 232 { 233 if (lock.isLocked()) 235 { 236 if (lock.getLocker() == transactionId) 237 lock.release(); 238 239 else if (logger.isDebugEnabled()) 244 logger.debug("Transaction " + transactionId 245 + " wants to release the lock held by transaction " 246 + lock.getLocker()); 247 } 248 else if (logger.isDebugEnabled()) 249 logger.warn("Transaction " + transactionId 250 + " tries to release a lock that has not been acquired."); 251 } 252 253 257 260 public String getXmlImpl() 261 { 262 return "<" + DatabasesXmlTags.ELT_SingleDBScheduler + " " 263 + DatabasesXmlTags.ATT_level + "=\"" 264 + DatabasesXmlTags.VAL_pessimisticTransaction + "\"/>"; 265 } 266 } 267 | Popular Tags |