src/org/objectweb/cjdbc/controller/loadbalancer/tasks/WriteStoredProcedureTask.java

説明を見る。
00001 00025 package org.objectweb.cjdbc.controller.loadbalancer.tasks; 00026 00027 import java.sql.Connection; 00028 import java.sql.SQLException; 00029 00030 import org.objectweb.cjdbc.common.exceptions.UnreachableBackendException; 00031 import org.objectweb.cjdbc.common.sql.StoredProcedure; 00032 import org.objectweb.cjdbc.controller.backend.DatabaseBackend; 00033 import org.objectweb.cjdbc.controller.connection.AbstractConnectionManager; 00034 import org.objectweb.cjdbc.controller.loadbalancer.AbstractLoadBalancer; 00035 import org.objectweb.cjdbc.controller.loadbalancer.BackendWorkerThread; 00036 00043 public class WriteStoredProcedureTask extends AbstractTask 00044 { 00045 private StoredProcedure proc; 00046 private int result; 00047 00055 public WriteStoredProcedureTask(int nbToComplete, int totalNb, 00056 StoredProcedure proc) 00057 { 00058 super(nbToComplete, totalNb); 00059 this.proc = proc; 00060 } 00061 00068 public void execute(BackendWorkerThread backendThread) throws SQLException 00069 { 00070 DatabaseBackend backend = backendThread.getBackend(); 00071 00072 AbstractConnectionManager cm = backend 00073 .getConnectionManager(proc.getLogin()); 00074 if (cm == null) 00075 { 00076 SQLException se = new SQLException( 00077 "No Connection Manager for Virtual Login:" + proc.getLogin()); 00078 try 00079 { 00080 notifyFailure(backendThread, 1, se); 00081 } 00082 catch (SQLException ignore) 00083 { 00084 00085 } 00086 throw se; 00087 } 00088 00089 if (proc.isAutoCommit()) 00090 { 00091 if (backend.isDisabling()) 00092 { 00093 // Backend is disabling, we do not execute queries except the one in the 00094 // transaction we already started. Just notify the completion for the 00095 // others. 00096 notifyCompletion(); 00097 return; 00098 } 00099 00100 // Use a connection just for this request 00101 Connection c = null; 00102 try 00103 { 00104 c = cm.getConnection(); 00105 } 00106 catch (UnreachableBackendException e1) 00107 { 00108 SQLException se = new SQLException("Backend " + backend.getName() 00109 + " is no more reachable."); 00110 try 00111 { 00112 notifyFailure(backendThread, 1, se); 00113 } 00114 catch (SQLException ignore) 00115 { 00116 } 00117 backendThread.getLogger().error( 00118 "Disabling backend " + backend.getName() 00119 + " because it is no more reachable."); 00120 backend.disable(); 00121 throw se; 00122 } 00123 00124 // Sanity check 00125 if (c == null) 00126 { 00127 SQLException se = new SQLException("No more connections"); 00128 try 00129 { // All backends failed, just ignore 00130 if (!notifyFailure(backendThread, (long) proc.getTimeout() * 1000, se)) 00131 return; 00132 } 00133 catch (SQLException ignore) 00134 { 00135 } 00136 // Disable this backend (it is no more in sync) by killing the backend 00137 // thread 00138 backendThread.kill(); 00139 String msg = "Stored procedure '" 00140 + proc.getSQLShortForm(backend.getSQLShortFormLength()) 00141 + "' failed on backend " + backend.getName() + " but " 00142 + getSuccess() + " succeeded (" + se + ")"; 00143 backendThread.getLogger().error(msg); 00144 throw new SQLException(msg); 00145 } 00146 00147 // Execute Query 00148 try 00149 { 00150 result = AbstractLoadBalancer.executeWriteStoredProcedureOnBackend( 00151 proc, backend, c); 00152 00153 // Warning! No way to detect if schema has been modified unless 00154 // we ask the backend again using DatabaseMetaData.getTables(). 00155 } 00156 catch (Exception e) 00157 { 00158 try 00159 { // All backends failed, just ignore 00160 if (!notifyFailure(backendThread, (long) proc.getTimeout() * 1000, e)) 00161 return; 00162 } 00163 catch (SQLException ignore) 00164 { 00165 } 00166 // Disable this backend (it is no more in sync) by killing the backend 00167 // thread 00168 backendThread.kill(); 00169 String msg = "Stored procedure '" 00170 + proc.getSQLShortForm(backend.getSQLShortFormLength()) 00171 + "' failed on backend " + backend.getName() + " but " 00172 + getSuccess() + " succeeded (" + e + ")"; 00173 backendThread.getLogger().error(msg); 00174 throw new SQLException(msg); 00175 } 00176 finally 00177 { 00178 cm.releaseConnection(c); 00179 } 00180 } 00181 else 00182 { // Re-use the connection used by this transaction 00183 Connection c; 00184 long tid = proc.getTransactionId(); 00185 Long lTid = new Long(tid); 00186 00187 if (!backend.isStartedTransaction(lTid)) 00188 { 00189 if (backend.isDisabling()) 00190 { 00191 // Backend is disabling, we do not start new transactions, just notify 00192 // the completion for the others 00193 notifyCompletion(); 00194 return; 00195 } 00196 00197 // Transaction has not been started yet, this is a lazy begin 00198 try 00199 { 00200 c = cm.getConnection(); 00201 } 00202 catch (UnreachableBackendException e1) 00203 { 00204 SQLException se = new SQLException("Backend " + backend.getName() 00205 + " is no more reachable."); 00206 try 00207 { 00208 notifyFailure(backendThread, 1, se); 00209 } 00210 catch (SQLException ignore) 00211 { 00212 } 00213 backendThread.getLogger().error( 00214 "Disabling backend " + backend.getName() 00215 + " because it is no more reachable."); 00216 backend.disable(); 00217 throw se; 00218 } 00219 00220 // Sanity check 00221 if (c == null) 00222 { // Bad connection 00223 SQLException se = new SQLException( 00224 "Unable to get connection for transaction " + tid); 00225 try 00226 { // All backends failed, just ignore 00227 if (!notifyFailure(backendThread, (long) proc.getTimeout() * 1000, 00228 se)) 00229 return; 00230 } 00231 catch (SQLException ignore) 00232 { 00233 } 00234 // Disable this backend (it is no more in sync) by killing the 00235 // backend thread 00236 backendThread.kill(); 00237 String msg = "Stored procedure '" 00238 + proc.getSQLShortForm(backend.getSQLShortFormLength()) 00239 + "' failed on backend " + backend.getName() + " but " 00240 + getSuccess() + " succeeded (" + se + ")"; 00241 backendThread.getLogger().error(msg); 00242 throw new SQLException(msg); 00243 } 00244 00245 // begin transaction 00246 backend.startTransaction(lTid); 00247 c.setAutoCommit(false); 00248 } 00249 else 00250 { // Transaction has already been started, retrieve connection 00251 c = cm.retrieveConnection(tid); 00252 00253 // Sanity check 00254 if (c == null) 00255 { // Bad connection 00256 SQLException se = new SQLException( 00257 "Unable to retrieve connection for transaction " + tid); 00258 try 00259 { // All backends failed, just ignore 00260 if (!notifyFailure(backendThread, (long) proc.getTimeout() * 1000, 00261 se)) 00262 return; 00263 } 00264 catch (SQLException ignore) 00265 { 00266 } 00267 // Disable this backend (it is no more in sync) by killing the 00268 // backend thread 00269 backendThread.kill(); 00270 String msg = "Stored procedure '" 00271 + proc.getSQLShortForm(backend.getSQLShortFormLength()) 00272 + "' failed on backend " + backend.getName() + " but " 00273 + getSuccess() + " succeeded (" + se + ")"; 00274 backendThread.getLogger().error(msg); 00275 throw new SQLException(msg); 00276 } 00277 } 00278 00279 // Execute Query 00280 try 00281 { 00282 result = AbstractLoadBalancer.executeWriteStoredProcedureOnBackend( 00283 proc, backend, c); 00284 00285 // Warning! No way to detect if schema has been modified unless 00286 // we ask the backend again using DatabaseMetaData.getTables(). 00287 } 00288 catch (Exception e) 00289 { 00290 try 00291 { // All backends failed, just ignore 00292 if (!notifyFailure(backendThread, (long) proc.getTimeout() * 1000, e)) 00293 return; 00294 } 00295 catch (SQLException ignore) 00296 { 00297 } 00298 // Disable this backend (it is no more in sync) by killing the backend 00299 // thread 00300 backendThread.kill(); 00301 String msg = "Stored procedure '" 00302 + proc.getSQLShortForm(backend.getSQLShortFormLength()) 00303 + "' failed on backend " + backend.getName() + " but " 00304 + getSuccess() + " succeeded (" + e + ")"; 00305 backendThread.getLogger().error(msg); 00306 throw new SQLException(msg); 00307 } 00308 } 00309 notifySuccess(); 00310 } 00311 00317 public int getResult() 00318 { 00319 return result; 00320 } 00321 00325 public String toString() 00326 { 00327 if (proc.isAutoCommit()) 00328 return "Write autocommit StoredProcedureTask (" + proc.getSQL() + ")"; 00329 else 00330 return "Write StoredProcedureTask for transaction:" 00331 + proc.getTransactionId() + "(" + proc.getSQL() + ")"; 00332 } 00333 00334 }

CJDBCversion1.0.4に対してTue Oct 12 15:16:03 2004に生成されました。 doxygen 1.3.8