00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 package org.objectweb.cjdbc.controller.scheduler.singledb;
00026
00027 import java.sql.SQLException;
00028
00029 import org.objectweb.cjdbc.common.sql.AbstractWriteRequest;
00030 import org.objectweb.cjdbc.common.sql.ParsingGranularities;
00031 import org.objectweb.cjdbc.common.sql.SelectRequest;
00032 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
00033 import org.objectweb.cjdbc.controller.requestmanager.RAIDbLevels;
00034 import org.objectweb.cjdbc.controller.scheduler.AbstractScheduler;
00035 import org.objectweb.cjdbc.controller.scheduler.schema.TransactionExclusiveLock;
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 public class SingleDBPessimisticTransactionLevelScheduler
00046 extends
00047 AbstractScheduler
00048 {
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 private long requestId;
00061 TransactionExclusiveLock lock = new TransactionExclusiveLock();
00062
00063
00064
00065
00066
00067
00068
00069
00070 public SingleDBPessimisticTransactionLevelScheduler()
00071 {
00072 super(RAIDbLevels.SingleDB, ParsingGranularities.NO_PARSING);
00073 }
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 public final synchronized void scheduleReadRequest(SelectRequest request)
00086 throws SQLException
00087 {
00088 request.setId(requestId++);
00089 }
00090
00091
00092
00093
00094 public final void readCompletedNotify(SelectRequest request)
00095 {
00096 }
00097
00098
00099
00100
00101
00102
00103
00104 public void scheduleNonSuspendedWriteRequest(AbstractWriteRequest request)
00105 throws SQLException
00106 {
00107 if (lock.acquire(request))
00108 {
00109 synchronized (this)
00110 {
00111 request.setId(requestId++);
00112 }
00113 if (logger.isDebugEnabled())
00114 logger.debug("Request " + request.getId() + " scheduled for write ("
00115 + getPendingWrites() + " pending writes)");
00116 }
00117 else
00118 {
00119 if (logger.isWarnEnabled())
00120 logger.warn("Request " + request.getId() + " timed out ("
00121 + request.getTimeout() + " s)");
00122 throw new SQLException("Timeout (" + request.getTimeout()
00123 + ") for request: " + request.getId());
00124 }
00125 }
00126
00127
00128
00129
00130 public final synchronized void notifyWriteCompleted(
00131 AbstractWriteRequest request)
00132 {
00133
00134
00135 if (request.isAutoCommit())
00136 releaseLock(request.getTransactionId());
00137 }
00138
00139
00140
00141
00142
00143
00144
00145
00146 protected final void commitTransaction(long transactionId)
00147 {
00148 releaseLock(transactionId);
00149 }
00150
00151
00152
00153
00154 protected final void rollbackTransaction(long transactionId)
00155 {
00156 releaseLock(transactionId);
00157 }
00158
00159
00160
00161
00162
00163
00164 private void releaseLock(long transactionId)
00165 {
00166
00167 if (lock.isLocked())
00168 {
00169 if (lock.getLocker() == transactionId)
00170 lock.release();
00171
00172
00173
00174
00175
00176 else if (logger.isDebugEnabled())
00177 logger.debug("Transaction " + transactionId
00178 + " wants to release the lock held by transaction "
00179 + lock.getLocker());
00180 }
00181 else if (logger.isDebugEnabled())
00182 logger.warn("Transaction " + transactionId
00183 + " tries to release a lock that has not been acquired.");
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193 public String getXmlImpl()
00194 {
00195 return "<" + DatabasesXmlTags.ELT_SingleDBScheduler + " "
00196 + DatabasesXmlTags.ATT_level + "=\""
00197 + DatabasesXmlTags.VAL_pessimisticTransaction + "\"/>";
00198 }
00199
00200 }