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.i18n.Translate; 33 import org.objectweb.cjdbc.common.log.Trace; 34 import org.objectweb.cjdbc.common.sql.AbstractWriteRequest; 35 import org.objectweb.cjdbc.controller.backend.DatabaseBackend; 36 import org.objectweb.cjdbc.controller.cache.metadata.MetadataCache; 37 import org.objectweb.cjdbc.controller.connection.AbstractConnectionManager; 38 import org.objectweb.cjdbc.controller.loadbalancer.AbstractLoadBalancer; 39 import org.objectweb.cjdbc.controller.loadbalancer.BackendWorkerThread; 40 import org.objectweb.cjdbc.controller.virtualdatabase.ControllerResultSet; 41 42 50 public class WriteRequestWithKeysTask extends AbstractTask 51 { 52 private AbstractWriteRequest request; 53 private ControllerResultSet result; 54 private MetadataCache metadataCache; 55 56 64 public WriteRequestWithKeysTask(int nbToComplete, int totalNb, 65 AbstractWriteRequest request, MetadataCache metadataCache) 66 { 67 super(nbToComplete, totalNb); 68 this.request = request; 69 this.metadataCache = metadataCache; 70 } 71 72 78 public void executeTask(BackendWorkerThread backendThread) 79 throws SQLException 80 { 81 DatabaseBackend backend = backendThread.getBackend(); 82 83 if (!backend.getDriverCompliance().supportGetGeneratedKeys()) 84 throw new SQLException (Translate.get( 85 "loadbalancer.backend.autogeneratedkeys.unsupported", backend 86 .getName())); 87 88 AbstractConnectionManager cm = backend.getConnectionManager(request 89 .getLogin()); 90 if (cm == null) 91 { 92 SQLException se = new SQLException ( 93 "No Connection Manager for Virtual Login:" + request.getLogin()); 94 try 95 { 96 notifyFailure(backendThread, 1, se); 97 } 98 catch (SQLException ignore) 99 { 100 101 } 102 throw se; 103 } 104 105 Trace logger = backendThread.getLogger(); 106 if (request.isAutoCommit()) 107 { 108 if (backend.isDisabling()) 109 { 110 notifyCompletion(); 113 return; 114 } 115 116 Connection c = null; 118 try 119 { 120 c = cm.getConnection(); 121 } 122 catch (UnreachableBackendException e1) 123 { 124 SQLException se = new SQLException ("Backend " + backend.getName() 125 + " is no more reachable."); 126 try 127 { 128 notifyFailure(backendThread, 1, se); 129 } 130 catch (SQLException ignore) 131 { 132 } 133 backendThread.kill(); 136 logger.error("Disabling backend " + backend.getName() 137 + " because it is no more reachable."); 138 throw se; 139 } 140 141 if (c == null) 143 { 144 SQLException se = new SQLException ("No more connections"); 145 try 146 { if (!notifyFailure(backendThread, (long) request.getTimeout() * 1000, 148 se)) 149 return; 150 } 151 catch (SQLException ignore) 152 { 153 } 154 backendThread.kill(); 157 String msg = "Request '" 158 + request.getSQLShortForm(backend.getSQLShortFormLength()) 159 + "' failed on backend " + backend.getName() + " but " 160 + getSuccess() + " succeeded (" + se + ")"; 161 logger.error(msg); 162 throw new SQLException (msg); 163 } 164 165 try 167 { 168 result = AbstractLoadBalancer.executeUpdateRequestOnBackendWithKeys( 169 request, backend, c, metadataCache); 170 171 backend.updateDatabaseBackendSchema(request); 172 } 173 catch (Exception e) 174 { 175 try 176 { if (!notifyFailure(backendThread, (long) request.getTimeout() * 1000, 178 e)) 179 return; 180 } 181 catch (SQLException ignore) 182 { 183 } 184 backendThread.kill(); 187 String msg = "Request '" 188 + request.getSQLShortForm(backend.getSQLShortFormLength()) 189 + "' failed on backend " + backend.getName() + " but " 190 + getSuccess() + " succeeded (" + e + ")"; 191 logger.error(msg); 192 throw new SQLException (msg); 193 } 194 finally 195 { 196 cm.releaseConnection(c); 197 } 198 } 199 else 200 { Connection c; 202 long tid = request.getTransactionId(); 203 Long lTid = new Long (tid); 204 205 try 206 { 207 c = backend.getConnectionForTransactionAndLazyBeginIfNeeded(lTid, cm, 208 request.getTransactionIsolation()); 209 } 210 catch (UnreachableBackendException ube) 211 { 212 SQLException se = new SQLException ("Backend " + backend.getName() 213 + " is no more reachable."); 214 try 215 { 216 notifyFailure(backendThread, 1, se); 217 } 218 catch (SQLException ignore) 219 { 220 } 221 backendThread.kill(); 224 logger.error("Disabling backend " + backend.getName() 225 + " because it is no more reachable."); 226 throw se; 227 } 228 catch (NoTransactionStartWhenDisablingException e) 229 { 230 notifyCompletion(); 234 return; 235 } 236 catch (SQLException e1) 237 { 238 SQLException se = new SQLException ( 239 "Unable to get connection for transaction " + tid); 240 try 241 { if (!notifyFailure(backendThread, (long) request.getTimeout() * 1000, 243 se)) 244 return; 245 } 246 catch (SQLException ignore) 247 { 248 } 249 backendThread.kill(); 252 String msg = "Request '" 253 + request.getSQLShortForm(backend.getSQLShortFormLength()) 254 + "' failed on backend " + backend.getName() + " but " 255 + getSuccess() + " succeeded (" + se + ")"; 256 logger.error(msg); 257 throw new SQLException (msg); 258 } 259 260 if (c == null) 262 { SQLException se = new SQLException ( 264 "Unable to retrieve connection for transaction " + tid); 265 try 266 { if (!notifyFailure(backendThread, (long) request.getTimeout() * 1000, 268 se)) 269 return; 270 } 271 catch (SQLException ignore) 272 { 273 } 274 backendThread.kill(); 277 String msg = "Request '" 278 + request.getSQLShortForm(backend.getSQLShortFormLength()) 279 + "' failed on backend " + backend.getName() + " but " 280 + getSuccess() + " succeeded (" + se + ")"; 281 logger.error(msg); 282 throw new SQLException (msg); 283 } 284 285 try 287 { 288 result = AbstractLoadBalancer.executeUpdateRequestOnBackendWithKeys( 289 request, backend, c, metadataCache); 290 291 backend.updateDatabaseBackendSchema(request); 292 } 293 catch (Exception e) 294 { 295 try 296 { if (!notifyFailure(backendThread, (long) request.getTimeout() * 1000, 298 e)) 299 return; 300 } 301 catch (SQLException ignore) 302 { 303 } 304 backendThread.kill(); 307 String msg = "Request '" 308 + request.getSQLShortForm(backend.getSQLShortFormLength()) 309 + "' failed on backend " + backend.getName() + " but " 310 + getSuccess() + " succeeded (" + e + ")"; 311 logger.error(msg); 312 throw new SQLException (msg); 313 } 314 } 315 notifySuccess(); 316 } 317 318 323 public ControllerResultSet getResult() 324 { 325 return result; 326 } 327 328 331 public String toString() 332 { 333 if (request.isAutoCommit()) 334 return "WriteWithKeys Autocommit Task (" + request.getSQL() + ")"; 335 else 336 return "WriteWithKeys Task from transaction:" 337 + request.getTransactionId() + "(" + request.getSQL() + ")"; 338 } 339 340 } | Popular Tags |