1 23 24 package org.continuent.sequoia.controller.loadbalancer.tasks; 25 26 import java.sql.Connection ; 27 import java.sql.SQLException ; 28 import java.sql.Savepoint ; 29 30 import org.continuent.sequoia.common.i18n.Translate; 31 import org.continuent.sequoia.common.log.Trace; 32 import org.continuent.sequoia.controller.backend.DatabaseBackend; 33 import org.continuent.sequoia.controller.connection.AbstractConnectionManager; 34 import org.continuent.sequoia.controller.loadbalancer.BackendWorkerThread; 35 import org.continuent.sequoia.controller.requestmanager.TransactionMetaData; 36 import org.continuent.sequoia.controller.requests.AbstractRequest; 37 import org.continuent.sequoia.controller.requests.UnknownWriteRequest; 38 39 46 public class SavepointTask extends AbstractTask 47 { 48 49 private TransactionMetaData tm; 50 51 private String savepointName; 52 53 private Savepoint result; 54 55 static Trace endUserLogger = Trace 56 .getLogger("org.continuent.sequoia.enduser"); 57 58 67 public SavepointTask(int nbToComplete, int totalNb, TransactionMetaData tm, 68 String savepointName) throws NullPointerException 69 { 70 super(nbToComplete, totalNb, tm.isPersistentConnection(), tm 71 .getPersistentConnectionId()); 72 if (tm == null) 73 throw new NullPointerException ("Unexpected null metadata in BeginTask"); 74 this.tm = tm; 75 this.savepointName = savepointName; 76 } 77 78 81 public void executeTask(BackendWorkerThread backendThread) 82 throws SQLException 83 { 84 DatabaseBackend backend = backendThread.getBackend(); 85 Long lTid = new Long (tm.getTransactionId()); 86 87 AbstractConnectionManager cm = backend.getConnectionManager(tm.getLogin()); 88 if (cm == null) 89 { 90 SQLException se = new SQLException ( 91 "No Connection Manager for Virtual Login:" + tm.getLogin()); 92 try 93 { 94 notifyFailure(backendThread, -1, se); 95 } 96 catch (SQLException ignore) 97 { 98 99 } 100 throw se; 101 } 102 103 Savepoint savepoint = null; 104 try 105 { 106 AbstractRequest fakeRequest = new UnknownWriteRequest("savepoint " 107 + savepointName, false, 0, "\n"); 108 fakeRequest.setTransactionId(tm.getTransactionId()); 109 fakeRequest 112 .setTransactionIsolation(org.continuent.sequoia.driver.Connection.DEFAULT_TRANSACTION_ISOLATION_LEVEL); 113 Connection c = backend.getConnectionForTransactionAndLazyBeginIfNeeded( 114 fakeRequest, cm); 115 116 if (c == null) 118 { backend.stopTransaction(lTid); 120 SQLException se = new SQLException ( 121 "Unable to retrieve connection for transaction " 122 + tm.getTransactionId()); 123 124 try 125 { if (!notifyFailure(backendThread, tm.getTimeout(), se)) 127 return; 128 } 129 catch (SQLException ignore) 130 { 131 } 132 backendThread.getLoadBalancer().disableBackend(backend, true); 135 String msg = "Failed to set savepoint for transaction " 136 + tm.getTransactionId() + " on backend " + backend.getName() 137 + " but " + getSuccess() + " succeeded (" + se + ")"; 138 backendThread.getLogger().error(msg); 139 endUserLogger.error(Translate.get("loadbalancer.backend.disabling", 140 backend.getName())); 141 throw new SQLException (msg); 142 } 143 144 savepoint = c.setSavepoint(savepointName); 146 result = savepoint; 147 } 148 catch (Exception e) 149 { 150 try 151 { 152 if (!notifyFailure(backendThread, tm.getTimeout(), new SQLException (e 153 .getMessage()))) 154 return; 155 } 156 catch (SQLException ignore) 157 { 158 } 159 backendThread.getLoadBalancer().disableBackend(backend, true); 162 String msg = "Failed to set savepoint for transaction " 163 + tm.getTransactionId() + " on backend " + backend.getName() 164 + " but " + getSuccess() + " succeeded (" + e + ")"; 165 backendThread.getLogger().error(msg); 166 endUserLogger.error(Translate.get("loadbalancer.backend.disabling", 167 backend.getName())); 168 throw new SQLException (msg); 169 } 170 finally 171 { 172 if (savepoint != null) 173 backend.addSavepoint(lTid, savepoint); 174 } 175 176 notifySuccess(backendThread); 177 } 178 179 182 public AbstractRequest getRequest() 183 { 184 return null; 185 } 186 187 192 public Savepoint getResult() 193 { 194 return result; 195 } 196 197 200 public String getSavepointName() 201 { 202 return savepointName; 203 } 204 205 208 public long getTransactionId() 209 { 210 return tm.getTransactionId(); 211 } 212 213 216 public boolean isAutoCommit() 217 { 218 return false; 219 } 220 221 224 public boolean equals(Object other) 225 { 226 if ((other == null) || !(other instanceof SavepointTask)) 227 return false; 228 229 SavepointTask savepoint = (SavepointTask) other; 230 return (this.getTransactionId() == savepoint.getTransactionId()) 231 && (this.savepointName.equals(savepoint.getSavepointName())); 232 } 233 234 237 public int hashCode() 238 { 239 return (int) this.getTransactionId(); 240 } 241 242 245 public String toString() 246 { 247 return "SavepointTask for transaction " + tm.getTransactionId() + " (" 248 + savepointName + ")"; 249 } 250 251 } 252 | Popular Tags |