1 22 23 package org.continuent.sequoia.controller.loadbalancer.tasks; 24 25 import java.sql.Connection ; 26 import java.sql.SQLException ; 27 28 import org.continuent.sequoia.common.i18n.Translate; 29 import org.continuent.sequoia.common.log.Trace; 30 import org.continuent.sequoia.controller.backend.DatabaseBackend; 31 import org.continuent.sequoia.controller.connection.AbstractConnectionManager; 32 import org.continuent.sequoia.controller.connection.PooledConnection; 33 import org.continuent.sequoia.controller.loadbalancer.BackendWorkerThread; 34 import org.continuent.sequoia.controller.requestmanager.TransactionMetaData; 35 import org.continuent.sequoia.controller.requests.AbstractRequest; 36 import org.continuent.sequoia.controller.requests.UnknownWriteRequest; 37 38 45 public class RollbackTask extends AbstractTask 46 { 47 private TransactionMetaData tm; 49 50 static Trace endUserLogger = Trace 51 .getLogger("org.continuent.sequoia.enduser"); 52 53 61 public RollbackTask(int nbToComplete, int totalNb, TransactionMetaData tm) 62 throws NullPointerException 63 { 64 super(nbToComplete, totalNb, tm.isPersistentConnection(), tm 65 .getPersistentConnectionId()); 66 if (tm == null) 67 throw new NullPointerException ("Unexpected null metadata in RollbackTask"); 68 this.tm = tm; 69 } 70 71 77 public void executeTask(BackendWorkerThread backendThread) 78 throws SQLException 79 { 80 DatabaseBackend backend = backendThread.getBackend(); 81 Long lTid = new Long (tm.getTransactionId()); 82 83 AbstractConnectionManager cm = backend.getConnectionManager(tm.getLogin()); 84 if (cm == null) 85 { 86 SQLException se = new SQLException ( 87 "No Connection Manager for Virtual Login:" + tm.getLogin()); 88 try 89 { 90 notifyFailure(backendThread, -1, se); 91 } 92 catch (SQLException ignore) 93 { 94 95 } 96 throw se; 97 } 98 99 PooledConnection pc = cm.retrieveConnectionForTransaction(tm 100 .getTransactionId()); 101 102 if (pc == null) 104 { backend.stopTransaction(lTid); 106 SQLException se = new SQLException ( 107 "Unable to retrieve connection for transaction " 108 + tm.getTransactionId()); 109 try 110 { if (!notifyFailure(backendThread, tm.getTimeout(), se)) 112 return; 113 } 114 catch (SQLException ignore) 115 { 116 } 117 backendThread.getLoadBalancer().disableBackend(backend, true); 120 String msg = "Failed to rollback transaction " + tm.getTransactionId() 121 + " on backend " + backend.getName() + " but " + getSuccess() 122 + " succeeded (" + se + ")"; 123 backendThread.getLogger().error(msg); 124 endUserLogger.error(Translate.get("loadbalancer.backend.disabling", 125 backend.getName())); 126 throw new SQLException (msg); 127 } 128 129 try 131 { 132 Connection c = pc.getConnection(); 133 c.rollback(); 134 if (tm.altersDatabaseSchema()) 135 { UnknownWriteRequest fakeRequest = new UnknownWriteRequest("rollback " 137 + getTransactionId(), false, 0, null); 138 fakeRequest.setLogin(tm.getLogin()); 139 fakeRequest.setIsAutoCommit(false); 140 fakeRequest.setTransactionId(getTransactionId()); 141 fakeRequest.setPersistentConnection(isPersistentConnection()); 142 fakeRequest.setPersistentConnectionId(getPersistentConnectionId()); 143 backendThread.getBackend().setSchemaIsDirty(true, fakeRequest); 144 } 145 c.setAutoCommit(true); 146 } 147 catch (Exception e) 148 { 149 try 150 { 151 if (!notifyFailure(backendThread, tm.getTimeout(), new SQLException (e 152 .getMessage()))) 153 return; 154 } 155 catch (SQLException ignore) 156 { 157 } 158 backendThread.getLoadBalancer().disableBackend(backend, true); 161 String msg = "Failed to rollback transaction " + tm.getTransactionId() 162 + " on backend " + backend.getName() + " but " + getSuccess() 163 + " succeeded (" + e + ")"; 164 backendThread.getLogger().error(msg); 165 endUserLogger.error(Translate.get("loadbalancer.backend.disabling", 166 backend.getName())); 167 throw new SQLException (msg); 168 } 169 finally 170 { 171 cm.releaseConnectionForTransaction(tm.getTransactionId()); 172 backend.stopTransaction(lTid); 173 backend.getTaskQueues().releaseLocksAndCheckForPriorityInversion(tm); 174 } 175 notifySuccess(backendThread); 176 } 177 178 181 public AbstractRequest getRequest() 182 { 183 return null; 184 } 185 186 189 public long getTransactionId() 190 { 191 return tm.getTransactionId(); 192 } 193 194 197 public boolean isAutoCommit() 198 { 199 return false; 200 } 201 202 205 public boolean equals(Object other) 206 { 207 if ((other == null) || !(other instanceof RollbackTask)) 208 return false; 209 210 RollbackTask rollback = (RollbackTask) other; 211 return this.getTransactionId() == rollback.getTransactionId(); 212 } 213 214 219 public final TransactionMetaData getTransactionMetaData() 220 { 221 return tm; 222 } 223 224 227 public int hashCode() 228 { 229 return (int) this.getTransactionId(); 230 } 231 232 235 public String toString() 236 { 237 return "RollbackTask (" + tm.getTransactionId() + ")"; 238 } 239 } | Popular Tags |