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.UnreachableBackendException; 31 import org.objectweb.cjdbc.common.log.Trace; 32 import org.objectweb.cjdbc.common.sql.StoredProcedure; 33 import org.objectweb.cjdbc.controller.backend.DatabaseBackend; 34 import org.objectweb.cjdbc.controller.connection.AbstractConnectionManager; 35 import org.objectweb.cjdbc.controller.loadbalancer.AbstractLoadBalancer; 36 import org.objectweb.cjdbc.controller.loadbalancer.BackendWorkerThread; 37 38 44 public class WriteStoredProcedureTask extends AbstractTask 45 { 46 private StoredProcedure proc; 47 private int result; 48 49 56 public WriteStoredProcedureTask(int nbToComplete, int totalNb, 57 StoredProcedure proc) 58 { 59 super(nbToComplete, totalNb); 60 this.proc = proc; 61 } 62 63 69 public void executeTask(BackendWorkerThread backendThread) 70 throws SQLException 71 { 72 DatabaseBackend backend = backendThread.getBackend(); 73 74 AbstractConnectionManager cm = backend 75 .getConnectionManager(proc.getLogin()); 76 if (cm == null) 77 { 78 SQLException se = new SQLException ( 79 "No Connection Manager for Virtual Login:" + proc.getLogin()); 80 try 81 { 82 notifyFailure(backendThread, 1, se); 83 } 84 catch (SQLException ignore) 85 { 86 87 } 88 throw se; 89 } 90 91 Trace logger = backendThread.getLogger(); 92 if (proc.isAutoCommit()) 93 { 94 if (backend.isDisabling()) 95 { 96 notifyCompletion(); 100 return; 101 } 102 103 Connection c = null; 105 try 106 { 107 c = cm.getConnection(); 108 } 109 catch (UnreachableBackendException e1) 110 { 111 SQLException se = new SQLException ("Backend " + backend.getName() 112 + " is no more reachable."); 113 try 114 { 115 notifyFailure(backendThread, 1, se); 116 } 117 catch (SQLException ignore) 118 { 119 } 120 backendThread.kill(); 123 logger.error("Disabling backend " + backend.getName() 124 + " because it is no more reachable."); 125 throw se; 126 } 127 128 if (c == null) 130 { 131 SQLException se = new SQLException ("No more connections"); 132 try 133 { if (!notifyFailure(backendThread, (long) proc.getTimeout() * 1000, se)) 135 return; 136 } 137 catch (SQLException ignore) 138 { 139 } 140 backendThread.kill(); 143 String msg = "Stored procedure '" 144 + proc.getSQLShortForm(backend.getSQLShortFormLength()) 145 + "' failed on backend " + backend.getName() + " but " 146 + getSuccess() + " succeeded (" + se + ")"; 147 logger.error(msg); 148 throw new SQLException (msg); 149 } 150 151 try 153 { 154 result = AbstractLoadBalancer.executeWriteStoredProcedureOnBackend( 155 proc, backend, c); 156 157 backend.setSchemaIsDirty(true); 158 } 159 catch (Exception e) 160 { 161 try 162 { if (!notifyFailure(backendThread, (long) proc.getTimeout() * 1000, e)) 164 return; 165 } 166 catch (SQLException ignore) 167 { 168 } 169 backendThread.kill(); 172 String msg = "Stored procedure '" 173 + proc.getSQLShortForm(backend.getSQLShortFormLength()) 174 + "' failed on backend " + backend.getName() + " but " 175 + getSuccess() + " succeeded (" + e + ")"; 176 logger.error(msg); 177 throw new SQLException (msg); 178 } 179 finally 180 { 181 cm.releaseConnection(c); 182 } 183 } 184 else 185 { Connection c; 187 long tid = proc.getTransactionId(); 188 Long lTid = new Long (tid); 189 190 try 191 { 192 c = backend.getConnectionForTransactionAndLazyBeginIfNeeded(lTid, cm, 193 proc.getTransactionIsolation()); 194 } 195 catch (UnreachableBackendException ube) 196 { 197 SQLException se = new SQLException ("Backend " + backend.getName() 198 + " is no more reachable."); 199 try 200 { 201 notifyFailure(backendThread, 1, se); 202 } 203 catch (SQLException ignore) 204 { 205 } 206 backendThread.kill(); 209 logger.error("Disabling backend " + backend.getName() 210 + " because it is no more reachable."); 211 throw se; 212 } 213 catch (SQLException e1) 214 { 215 SQLException se = new SQLException ( 216 "Unable to get connection for transaction " + tid); 217 try 218 { if (!notifyFailure(backendThread, (long) proc.getTimeout() * 1000, se)) 220 return; 221 } 222 catch (SQLException ignore) 223 { 224 } 225 backendThread.kill(); 228 String msg = "Request '" 229 + proc.getSQLShortForm(backend.getSQLShortFormLength()) 230 + "' failed on backend " + backend.getName() + " but " 231 + getSuccess() + " succeeded (" + se + ")"; 232 logger.error(msg); 233 throw new SQLException (msg); 234 } 235 236 if (c == null) 238 { SQLException se = new SQLException ( 240 "Unable to retrieve connection for transaction " + tid); 241 try 242 { if (!notifyFailure(backendThread, (long) proc.getTimeout() * 1000, se)) 244 return; 245 } 246 catch (SQLException ignore) 247 { 248 } 249 backendThread.kill(); 252 String msg = "Request '" 253 + proc.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 try 262 { 263 result = AbstractLoadBalancer.executeWriteStoredProcedureOnBackend( 264 proc, backend, c); 265 266 backend.setSchemaIsDirty(true); 267 } 268 catch (Exception e) 269 { 270 try 271 { if (!notifyFailure(backendThread, (long) proc.getTimeout() * 1000, e)) 273 return; 274 } 275 catch (SQLException ignore) 276 { 277 } 278 backendThread.kill(); 281 String msg = "Stored procedure '" 282 + proc.getSQLShortForm(backend.getSQLShortFormLength()) 283 + "' failed on backend " + backend.getName() + " but " 284 + getSuccess() + " succeeded (" + e + ")"; 285 logger.error(msg); 286 throw new SQLException (msg); 287 } 288 } 289 notifySuccess(); 290 } 291 292 297 public int getResult() 298 { 299 return result; 300 } 301 302 305 public String toString() 306 { 307 if (proc.isAutoCommit()) 308 return "Write autocommit StoredProcedureTask (" + proc.getSQL() + ")"; 309 else 310 return "Write StoredProcedureTask for transaction:" 311 + proc.getTransactionId() + "(" + proc.getSQL() + ")"; 312 } 313 314 } | Popular Tags |