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.StoredProcedure; 34 import org.objectweb.cjdbc.controller.backend.DatabaseBackend; 35 import org.objectweb.cjdbc.controller.cache.metadata.MetadataCache; 36 import org.objectweb.cjdbc.controller.connection.AbstractConnectionManager; 37 import org.objectweb.cjdbc.controller.loadbalancer.AbstractLoadBalancer; 38 import org.objectweb.cjdbc.controller.loadbalancer.BackendWorkerThread; 39 import org.objectweb.cjdbc.controller.virtualdatabase.ControllerResultSet; 40 41 47 public class ReadStoredProcedureTask extends AbstractTask 48 { 49 private StoredProcedure proc; 50 private ControllerResultSet result; 51 private MetadataCache metadataCache; 52 53 61 public ReadStoredProcedureTask(int nbToComplete, int totalNb, 62 StoredProcedure proc, MetadataCache metadataCache) 63 { 64 super(nbToComplete, totalNb); 65 this.proc = proc; 66 this.metadataCache = metadataCache; 67 } 68 69 76 public void executeTask(BackendWorkerThread backendThread) 77 throws SQLException 78 { 79 DatabaseBackend backend = backendThread.getBackend(); 80 81 AbstractConnectionManager cm = backend 82 .getConnectionManager(proc.getLogin()); 83 if (cm == null) 84 { 85 SQLException se = new SQLException ( 86 "No Connection Manager for Virtual Login:" + proc.getLogin()); 87 try 88 { 89 notifyFailure(backendThread, 1, se); 90 } 91 catch (SQLException ignore) 92 { 93 94 } 95 throw se; 96 } 97 98 Trace logger = backendThread.getLogger(); 99 if (proc.isAutoCommit()) 100 { Connection c = null; 102 try 103 { 104 c = cm.getConnection(); 105 } 106 catch (UnreachableBackendException e1) 107 { 108 SQLException se = new SQLException ("Backend " + backend.getName() 109 + " is no more reachable."); 110 try 111 { 112 notifyFailure(backendThread, 1, se); 113 } 114 catch (SQLException ignore) 115 { 116 } 117 backendThread.kill(); 120 logger.error("Disabling backend " + backend.getName() 121 + " because it is no more reachable."); 122 throw se; 123 } 124 125 if (c == null) 127 { 128 SQLException se = new SQLException ("No more connections"); 129 try 130 { if (!notifyFailure(backendThread, (long) proc.getTimeout() * 1000, se)) 132 return; 133 } 134 catch (SQLException ignore) 135 { 136 } 137 backendThread.kill(); 140 String msg = "Stored procedure '" 141 + proc.getSQLShortForm(backend.getSQLShortFormLength()) 142 + "' failed on backend " + backend.getName() + " but " 143 + getSuccess() + " succeeded (" + se + ")"; 144 logger.error(msg); 145 throw new SQLException (msg); 146 } 147 148 try 150 { 151 result = AbstractLoadBalancer.executeReadStoredProcedureOnBackend(proc, 152 backend, c, metadataCache); 153 154 backend.setSchemaIsDirty(true); 155 } 156 catch (Exception e) 157 { 158 try 159 { if (!notifyFailure(backendThread, (long) proc.getTimeout() * 1000, e)) 161 return; 162 } 163 catch (SQLException ignore) 164 { 165 } 166 backendThread.kill(); 169 String msg = "Stored procedure '" 170 + proc.getSQLShortForm(backend.getSQLShortFormLength()) 171 + "' failed on backend " + backend.getName() + " but " 172 + getSuccess() + " succeeded (" + e + ")"; 173 logger.error(msg); 174 throw new SQLException (msg); 175 } 176 finally 177 { 178 cm.releaseConnection(c); 179 } 180 } 181 else 182 { Connection c; 184 long tid = proc.getTransactionId(); 185 Long lTid = new Long (tid); 186 187 try 188 { 189 c = backend.getConnectionForTransactionAndLazyBeginIfNeeded(lTid, cm, 190 proc.getTransactionIsolation()); 191 } 192 catch (UnreachableBackendException ube) 193 { 194 SQLException se = new SQLException ("Backend " + backend.getName() 195 + " is no more reachable."); 196 try 197 { 198 notifyFailure(backendThread, 1, se); 199 } 200 catch (SQLException ignore) 201 { 202 } 203 backendThread.kill(); 206 logger.error("Disabling backend " + backend.getName() 207 + " because it is no more reachable."); 208 throw se; 209 } 210 catch (NoTransactionStartWhenDisablingException e) 211 { 212 logger 213 .error("Disabling backend " 214 + backend.getName() 215 + " has been assigned a select request but it cannot start a new transaction for it."); 216 notifyFailure(backendThread, (long) proc.getTimeout() * 1000, e); 217 return; 218 } 219 catch (SQLException e1) 220 { 221 SQLException se = new SQLException ( 222 "Unable to get connection for transaction " + tid); 223 try 224 { if (!notifyFailure(backendThread, (long) proc.getTimeout() * 1000, se)) 226 return; 227 } 228 catch (SQLException ignore) 229 { 230 } 231 backendThread.kill(); 234 String msg = "Request '" 235 + proc.getSQLShortForm(backend.getSQLShortFormLength()) 236 + "' failed on backend " + backend.getName() + " but " 237 + getSuccess() + " succeeded (" + se + ")"; 238 logger.error(msg); 239 throw new SQLException (msg); 240 } 241 242 if (c == null) 244 { SQLException se = new SQLException ( 246 "Unable to retrieve connection for transaction " + tid); 247 try 248 { if (!notifyFailure(backendThread, (long) proc.getTimeout() * 1000, se)) 250 return; 251 } 252 catch (SQLException ignore) 253 { 254 } 255 backendThread.kill(); 258 String msg = "Request '" 259 + proc.getSQLShortForm(backend.getSQLShortFormLength()) 260 + "' failed on backend " + backend.getName() + " but " 261 + getSuccess() + " succeeded (" + se + ")"; 262 logger.error(msg); 263 throw new SQLException (msg); 264 } 265 266 try 268 { 269 result = AbstractLoadBalancer.executeReadStoredProcedureOnBackend(proc, 270 backend, c, metadataCache); 271 272 backend.setSchemaIsDirty(true); 273 } 274 catch (Exception e) 275 { 276 try 277 { if (!notifyFailure(backendThread, (long) proc.getTimeout() * 1000, e)) 279 return; 280 } 281 catch (SQLException ignore) 282 { 283 } 284 backendThread.kill(); 287 String msg = "Stored procedure '" 288 + proc.getSQLShortForm(backend.getSQLShortFormLength()) 289 + "' failed on backend " + backend.getName() + " but " 290 + getSuccess() + " succeeded (" + e + ")"; 291 logger.error(msg); 292 throw new SQLException (msg); 293 } 294 } 295 notifySuccess(); 296 } 297 298 303 public ControllerResultSet getResult() 304 { 305 return result; 306 } 307 308 311 public String toString() 312 { 313 return "ReadStoredProcedureTask (" + proc.getSQL() + ")"; 314 } 315 } | Popular Tags |