1 24 25 package org.objectweb.cjdbc.controller.loadbalancer.tasks; 26 27 import java.sql.Connection ; 28 import java.sql.SQLException ; 29 30 import org.objectweb.cjdbc.common.exceptions.NoTransactionStartWhenDisablingException; 31 import org.objectweb.cjdbc.common.exceptions.UnreachableBackendException; 32 import org.objectweb.cjdbc.common.log.Trace; 33 import org.objectweb.cjdbc.common.sql.AbstractWriteRequest; 34 import org.objectweb.cjdbc.controller.backend.DatabaseBackend; 35 import org.objectweb.cjdbc.controller.connection.AbstractConnectionManager; 36 import org.objectweb.cjdbc.controller.loadbalancer.AbstractLoadBalancer; 37 import org.objectweb.cjdbc.controller.loadbalancer.BackendWorkerThread; 38 39 47 public class WriteRequestTask extends AbstractTask 48 { 49 private AbstractWriteRequest request; 50 private int result; 51 52 59 public WriteRequestTask(int nbToComplete, int totalNb, 60 AbstractWriteRequest request) 61 { 62 super(nbToComplete, totalNb); 63 this.request = request; 64 } 65 66 72 public void executeTask(BackendWorkerThread backendThread) 73 throws SQLException 74 { 75 DatabaseBackend backend = backendThread.getBackend(); 76 77 AbstractConnectionManager cm = backend.getConnectionManager(request 78 .getLogin()); 79 if (cm == null) 80 { 81 SQLException se = new SQLException ( 82 "No Connection Manager for Virtual Login:" + request.getLogin()); 83 try 84 { 85 notifyFailure(backendThread, 1, se); 86 } 87 catch (SQLException ignore) 88 { 89 } 90 throw se; 91 } 92 93 Trace logger = backendThread.getLogger(); 94 if (request.isAutoCommit()) 95 { 96 if (backend.isDisabling()) 97 { 98 notifyCompletion(); 102 return; 103 } 104 105 Connection c = null; 107 try 108 { 109 c = cm.getConnection(); 110 } 111 catch (UnreachableBackendException e1) 112 { 113 SQLException se = new SQLException ("Backend " + backend.getName() 114 + " is no more reachable."); 115 try 116 { 117 notifyFailure(backendThread, 1, se); 118 } 119 catch (SQLException ignore) 120 { 121 } 122 backendThread.kill(); 125 logger.error("Disabling backend " + backend.getName() 126 + " because it is no more reachable."); 127 throw se; 128 } 129 130 if (c == null) 132 { 133 SQLException se = new SQLException ("No more connections"); 134 try 135 { if (!notifyFailure(backendThread, (long) request.getTimeout() * 1000, 137 se)) 138 { 139 return; 140 } 141 } 142 catch (SQLException ignore) 143 { 144 } 145 backendThread.kill(); 148 String msg = "Request '" 149 + request.getSQLShortForm(backend.getSQLShortFormLength()) 150 + "' failed on backend " + backend.getName() + " but " 151 + getSuccess() + " succeeded (" + se + ")"; 152 logger.error(msg); 153 throw new SQLException (msg); 154 } 155 156 try 158 { 159 result = AbstractLoadBalancer.executeUpdateRequestOnBackend(request, 160 backend, c); 161 162 backend.updateDatabaseBackendSchema(request); 163 } 164 catch (Exception e) 165 { 166 try 167 { if (!notifyFailure(backendThread, (long) request.getTimeout() * 1000, 169 e)) 170 return; 171 } 172 catch (SQLException ignore) 173 { 174 } 175 backendThread.kill(); 178 String msg = "Request '" 179 + request.getSQLShortForm(backend.getSQLShortFormLength()) 180 + "' failed on backend " + backend.getName() + " but " 181 + getSuccess() + " succeeded (" + e + ")"; 182 183 if (logger.isDebugEnabled()) 184 logger.debug(msg, e); 185 else 186 logger.error(msg); 187 throw new SQLException (msg); 188 } 189 finally 190 { 191 cm.releaseConnection(c); 192 } 193 } 194 else 195 { Connection c; 197 long tid = request.getTransactionId(); 198 Long lTid = new Long (tid); 199 200 try 201 { 202 c = backend.getConnectionForTransactionAndLazyBeginIfNeeded(lTid, cm, 203 request.getTransactionIsolation()); 204 } 205 catch (UnreachableBackendException ube) 206 { 207 SQLException se = new SQLException ("Backend " + backend.getName() 208 + " is no more reachable."); 209 try 210 { 211 notifyFailure(backendThread, 1, se); 212 } 213 catch (SQLException ignore) 214 { 215 } 216 backendThread.kill(); 219 logger.error("Disabling backend " + backend.getName() 220 + " because it is no more reachable."); 221 throw se; 222 } 223 catch (NoTransactionStartWhenDisablingException e) 224 { 225 notifyCompletion(); 229 return; 230 } 231 catch (SQLException e1) 232 { 233 SQLException se = new SQLException ( 234 "Unable to get connection for transaction " + tid); 235 try 236 { if (!notifyFailure(backendThread, (long) request.getTimeout() * 1000, 238 se)) 239 return; 240 } 241 catch (SQLException ignore) 242 { 243 } 244 backendThread.kill(); 247 String msg = "Request '" 248 + request.getSQLShortForm(backend.getSQLShortFormLength()) 249 + "' failed on backend " + backend.getName() + " but " 250 + getSuccess() + " succeeded (" + se + ")"; 251 logger.error(msg); 252 throw new SQLException (msg); 253 } 254 255 if (c == null) 257 { SQLException se = new SQLException ( 259 "Unable to retrieve connection for transaction " + tid); 260 try 261 { if (!notifyFailure(backendThread, (long) request.getTimeout() * 1000, 263 se)) 264 return; 265 } 266 catch (SQLException ignore) 267 { 268 } 269 backendThread.kill(); 272 String msg = "Request '" 273 + request.getSQLShortForm(backend.getSQLShortFormLength()) 274 + "' failed on backend " + backend.getName() + " but " 275 + getSuccess() + " succeeded (" + se + ")"; 276 logger.error(msg); 277 throw new SQLException (msg); 278 } 279 280 try 282 { 283 result = AbstractLoadBalancer.executeUpdateRequestOnBackend(request, 284 backend, c); 285 286 backend.updateDatabaseBackendSchema(request); 287 } 288 catch (Exception e) 289 { 290 try 291 { if (!notifyFailure(backendThread, (long) request.getTimeout() * 1000, 293 e)) 294 return; 295 } 296 catch (SQLException ignore) 297 { 298 } 299 backendThread.kill(); 302 String msg = "Request '" 303 + request.getSQLShortForm(backend.getSQLShortFormLength()) 304 + "' failed on backend " + backend.getName() + " but " 305 + getSuccess() + " succeeded (" + e + ")"; 306 if (logger.isDebugEnabled()) 307 logger.debug(msg, e); 308 else 309 logger.error(msg); 310 throw new SQLException (msg); 311 } 312 } 313 notifySuccess(); 314 } 315 316 321 public int getResult() 322 { 323 return result; 324 } 325 326 329 public String toString() 330 { 331 if (request.isAutoCommit()) 332 return "WriteAutocommit Task (" + request.getSQL() + ")"; 333 else 334 return "Write Task from transaction:" + request.getTransactionId() + "(" 335 + request.getSQL() + ")"; 336 } 337 338 } | Popular Tags |