1 23 24 package org.continuent.sequoia.controller.scheduler.singledb; 25 26 import java.sql.SQLException ; 27 28 import org.continuent.sequoia.common.exceptions.RollbackException; 29 import org.continuent.sequoia.common.xml.DatabasesXmlTags; 30 import org.continuent.sequoia.controller.core.ControllerConstants; 31 import org.continuent.sequoia.controller.requestmanager.RAIDbLevels; 32 import org.continuent.sequoia.controller.requests.AbstractWriteRequest; 33 import org.continuent.sequoia.controller.requests.ParsingGranularities; 34 import org.continuent.sequoia.controller.requests.SelectRequest; 35 import org.continuent.sequoia.controller.requests.StoredProcedure; 36 import org.continuent.sequoia.controller.scheduler.AbstractScheduler; 37 import org.continuent.sequoia.controller.scheduler.schema.TransactionExclusiveLock; 38 39 47 public class SingleDBPessimisticTransactionLevelScheduler 48 extends AbstractScheduler 49 { 50 51 61 TransactionExclusiveLock lock = new TransactionExclusiveLock(); 62 63 67 70 public SingleDBPessimisticTransactionLevelScheduler() 71 { 72 super(RAIDbLevels.SingleDB, ParsingGranularities.NO_PARSING); 73 } 74 75 79 85 public final void scheduleNonSuspendedReadRequest(SelectRequest request) 86 throws SQLException 87 { 88 } 89 90 93 public final void readCompletedNotify(SelectRequest request) 94 { 95 } 96 97 103 public void scheduleNonSuspendedWriteRequest(AbstractWriteRequest request) 104 throws SQLException 105 { 106 if (lock.acquire(request)) 107 { 108 if (logger.isDebugEnabled()) 109 logger.debug("Request " + request.getId() + " scheduled for write (" 110 + getPendingWrites() + " pending writes)"); 111 } 112 else 113 { 114 if (logger.isWarnEnabled()) 115 logger.warn("Request " + request.getId() + " timed out (" 116 + request.getTimeout() + " s)"); 117 throw new SQLException ("Timeout (" + request.getTimeout() 118 + ") for request: " + request.getId()); 119 } 120 } 121 122 125 public final void notifyWriteCompleted(AbstractWriteRequest request) 126 { 127 if (request.isAutoCommit()) 130 releaseLock(request.getTransactionId()); 131 } 132 133 136 public final void scheduleNonSuspendedStoredProcedure(StoredProcedure proc) 137 throws SQLException , RollbackException 138 { 139 if (lock.acquire(proc)) 140 { 141 if (logger.isDebugEnabled()) 142 logger.debug("Stored procedure " + proc.getId() 143 + " scheduled for write (" + getPendingWrites() 144 + " pending writes)"); 145 } 146 else 147 { 148 if (logger.isWarnEnabled()) 149 logger.warn("Stored procedure " + proc.getId() + " timed out (" 150 + proc.getTimeout() + " s)"); 151 throw new SQLException ("Timeout (" + proc.getTimeout() 152 + ") for request: " 153 + proc.getSqlShortForm(ControllerConstants.SQL_SHORT_FORM_LENGTH)); 154 } 155 } 156 157 160 public final void notifyStoredProcedureCompleted(StoredProcedure proc) 161 { 162 if (proc.isAutoCommit() && (!proc.isCreate())) 165 releaseLock(proc.getTransactionId()); 166 } 167 168 172 175 protected final void commitTransaction(long transactionId) 176 { 177 releaseLock(transactionId); 178 } 179 180 183 protected final void rollbackTransaction(long transactionId) 184 { 185 releaseLock(transactionId); 186 } 187 188 192 protected final void rollbackTransaction(long transactionId, 193 String savepointName) 194 { 195 } 196 197 201 protected final void setSavepointTransaction(long transactionId, String name) 202 { 203 } 204 205 209 protected final void releaseSavepointTransaction(long transactionId, 210 String name) 211 { 212 } 213 214 219 private void releaseLock(long transactionId) 220 { 221 if (lock.isLocked()) 223 { 224 if (lock.getLocker() == transactionId) 225 lock.release(); 226 227 else if (logger.isDebugEnabled()) 232 logger.debug("Transaction " + transactionId 233 + " wants to release the lock held by transaction " 234 + lock.getLocker()); 235 } 236 else if (logger.isDebugEnabled()) 237 logger.warn("Transaction " + transactionId 238 + " tries to release a lock that has not been acquired."); 239 } 240 241 245 248 public String getXmlImpl() 249 { 250 return "<" + DatabasesXmlTags.ELT_SingleDBScheduler + " " 251 + DatabasesXmlTags.ATT_level + "=\"" 252 + DatabasesXmlTags.VAL_pessimisticTransaction + "\"/>"; 253 } 254 } 255 | Popular Tags |