1 23 24 package org.continuent.sequoia.controller.loadbalancer.tasks; 25 26 import java.sql.Connection ; 27 import java.sql.SQLException ; 28 29 import org.continuent.sequoia.common.i18n.Translate; 30 import org.continuent.sequoia.common.log.Trace; 31 import org.continuent.sequoia.controller.backend.DatabaseBackend; 32 import org.continuent.sequoia.controller.connection.AbstractConnectionManager; 33 import org.continuent.sequoia.controller.connection.PooledConnection; 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 38 45 public class CommitTask extends AbstractTask 46 { 47 private TransactionMetaData tm; 49 50 static Trace endUserLogger = Trace 51 .getLogger("org.continuent.sequoia.enduser"); 52 53 61 public CommitTask(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 BeginTask"); 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 PooledConnection pc = cm.retrieveConnectionForTransaction(tm 99 .getTransactionId()); 100 101 if (pc == null) 103 { backend.stopTransaction(lTid); 105 SQLException se = new SQLException ( 106 "Unable to retrieve connection for transaction " 107 + tm.getTransactionId()); 108 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 commit 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.commit(); 134 c.setAutoCommit(true); 135 } 136 catch (Exception e) 137 { 138 try 139 { 140 if (!notifyFailure(backendThread, tm.getTimeout(), new SQLException (e 141 .getMessage()))) 142 return; 143 } 144 catch (SQLException ignore) 145 { 146 } 147 backendThread.getLoadBalancer().disableBackend(backend, true); 150 String msg = "Failed to commit transaction " + tm.getTransactionId() 151 + " on backend " + backend.getName() + " but " + getSuccess() 152 + " succeeded (" + e + ")"; 153 backendThread.getLogger().error(msg); 154 endUserLogger.error(Translate.get("loadbalancer.backend.disabling", 155 backend.getName())); 156 throw new SQLException (msg); 157 } 158 finally 159 { 160 cm.releaseConnectionForTransaction(tm.getTransactionId()); 161 backend.stopTransaction(lTid); 162 backend.getTaskQueues().releaseLocksAndCheckForPriorityInversion(tm); 163 } 164 notifySuccess(backendThread); 165 } 166 167 170 public AbstractRequest getRequest() 171 { 172 return null; 173 } 174 175 178 public long getTransactionId() 179 { 180 return tm.getTransactionId(); 181 } 182 183 186 public boolean isAutoCommit() 187 { 188 return false; 189 } 190 191 194 public boolean equals(Object other) 195 { 196 if ((other == null) || !(other instanceof CommitTask)) 197 return false; 198 199 CommitTask commit = (CommitTask) other; 200 return this.getTransactionId() == commit.getTransactionId(); 201 } 202 203 206 public int hashCode() 207 { 208 return (int) this.getTransactionId(); 209 } 210 211 214 public String toString() 215 { 216 return "CommitTask (" + tm.getTransactionId() + ")"; 217 } 218 } | Popular Tags |