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.SelectRequest; 34 import org.objectweb.cjdbc.common.util.Constants; 35 import org.objectweb.cjdbc.controller.backend.DatabaseBackend; 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 49 public class SelectRequestTask extends AbstractTask 50 { 51 private SelectRequest request; 52 private ControllerResultSet result; 53 54 61 public SelectRequestTask(int nbToComplete, int totalNb, SelectRequest request) 62 { 63 super(nbToComplete, totalNb); 64 this.request = request; 65 } 66 67 73 public void executeTask(BackendWorkerThread backendThread) 74 throws SQLException 75 { 76 DatabaseBackend backend = backendThread.getBackend(); 77 78 AbstractConnectionManager cm = backend.getConnectionManager(request 79 .getLogin()); 80 if (cm == null) 81 { 82 SQLException se = new SQLException ( 83 "No Connection Manager for Virtual Login:" + request.getLogin()); 84 try 85 { 86 notifyFailure(backendThread, 1, se); 87 } 88 catch (SQLException ignore) 89 { 90 91 } 92 throw se; 93 } 94 95 Trace logger = backendThread.getLogger(); 96 if (request.isAutoCommit()) 97 { Connection c = null; 99 try 100 { 101 c = cm.getConnection(); 102 } 103 catch (UnreachableBackendException e1) 104 { 105 SQLException se = new SQLException ("Backend " + backend.getName() 106 + " is no more reachable."); 107 try 108 { 109 notifyFailure(backendThread, 1, se); 110 } 111 catch (SQLException ignore) 112 { 113 } 114 backendThread.kill(); 117 logger.error("Disabling backend " + backend.getName() 118 + " because it is no more reachable."); 119 throw se; 120 } 121 122 if (c == null) 124 { 125 SQLException se = new SQLException ("No more connections"); 126 try 127 { if (!notifyFailure(backendThread, (long) request.getTimeout() * 1000, 129 se)) 130 return; 131 } 132 catch (SQLException ignore) 133 { 134 } 135 backendThread.kill(); 138 throw new SQLException ("Request '" 139 + request.getSQLShortForm(Constants.SQL_SHORT_FORM_LENGTH) 140 + "' failed on backend " + backend.getName() + " (" + se + ")"); 141 } 142 143 try 145 { 146 result = AbstractLoadBalancer.executeSelectRequestOnBackend(request, 147 backend, c, null); 148 } 149 catch (Exception e) 150 { 151 try 152 { if (!notifyFailure(backendThread, (long) request.getTimeout() * 1000, 154 e)) 155 return; 156 } 157 catch (SQLException ignore) 158 { 159 } 160 throw new SQLException ("Request '" 161 + request.getSQLShortForm(Constants.SQL_SHORT_FORM_LENGTH) 162 + "' failed on backend " + backend.getName() + " (" + e + ")"); 163 } 164 finally 165 { 166 cm.releaseConnection(c); 167 } 168 } 169 else 170 { 171 Connection c; 172 long tid = request.getTransactionId(); 173 Long lTid = new Long (tid); 174 175 try 176 { 177 c = backend.getConnectionForTransactionAndLazyBeginIfNeeded(lTid, cm, 178 request.getTransactionIsolation()); 179 } 180 catch (UnreachableBackendException ube) 181 { 182 SQLException se = new SQLException ("Backend " + backend.getName() 183 + " is no more reachable."); 184 try 185 { 186 notifyFailure(backendThread, 1, se); 187 } 188 catch (SQLException ignore) 189 { 190 } 191 backendThread.kill(); 194 logger.error("Disabling backend " + backend.getName() 195 + " because it is no more reachable."); 196 throw se; 197 } 198 catch (NoTransactionStartWhenDisablingException e) 199 { 200 logger 201 .error("Disabling backend " 202 + backend.getName() 203 + " has been assigned a select request but it cannot start a new transaction for it."); 204 notifyFailure(backendThread, (long) request.getTimeout() * 1000, e); 205 return; 206 } 207 catch (SQLException e1) 208 { 209 SQLException se = new SQLException ( 210 "Unable to get connection for transaction " + tid); 211 try 212 { if (!notifyFailure(backendThread, (long) request.getTimeout() * 1000, 214 se)) 215 return; 216 } 217 catch (SQLException ignore) 218 { 219 } 220 backendThread.kill(); 223 String msg = "Request '" 224 + request.getSQLShortForm(backend.getSQLShortFormLength()) 225 + "' failed on backend " + backend.getName() + " but " 226 + getSuccess() + " succeeded (" + se + ")"; 227 logger.error(msg); 228 throw new SQLException (msg); 229 } 230 231 if (c == null) 233 { SQLException se = new SQLException ( 235 "Unable to retrieve connection for transaction " + tid); 236 try 237 { if (!notifyFailure(backendThread, (long) request.getTimeout() * 1000, 239 se)) 240 return; 241 } 242 catch (SQLException ignore) 243 { 244 } 245 backendThread.kill(); 248 String msg = "Request '" 249 + request.getSQLShortForm(backend.getSQLShortFormLength()) 250 + "' failed on backend " + backend.getName() + " but " 251 + getSuccess() + " succeeded (" + se + ")"; 252 logger.error(msg); 253 throw new SQLException (msg); 254 } 255 256 try 258 { 259 result = AbstractLoadBalancer.executeSelectRequestOnBackend(request, 260 backend, c, null); 261 } 262 catch (Exception e) 263 { 264 try 265 { if (!notifyFailure(backendThread, (long) request.getTimeout() * 1000, 267 e)) 268 return; 269 } 270 catch (SQLException ignore) 271 { 272 } 273 throw new SQLException ("Request '" 274 + request.getSQLShortForm(Constants.SQL_SHORT_FORM_LENGTH) 275 + "' failed on backend " + backend.getName() + " (" + e + ")"); 276 } 277 } 278 notifySuccess(); 279 } 280 281 286 public ControllerResultSet getResult() 287 { 288 return result; 289 } 290 291 294 public String toString() 295 { 296 return "SelectRequestTask (" + request.getSQL() + ")"; 297 } 298 } | Popular Tags |