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.controller.backend.DatabaseBackend; 34 import org.objectweb.cjdbc.controller.connection.AbstractConnectionManager; 35 import org.objectweb.cjdbc.controller.loadbalancer.BackendWorkerThread; 36 37 47 public class BeginTask extends AbstractTask 48 { 49 50 private String login; 51 52 53 private long transactionId; 54 55 56 private long timeout; 57 58 67 public BeginTask(int nbToComplete, int totalNb, long timeout, String login, 68 long transactionId) 69 { 70 super(nbToComplete, totalNb); 71 this.login = login; 72 this.transactionId = transactionId; 73 this.timeout = timeout; 74 } 75 76 82 public void executeTask(BackendWorkerThread backendThread) 83 throws SQLException 84 { 85 DatabaseBackend backend = backendThread.getBackend(); 86 if (backend.isDisabling()) 87 { 88 notifyCompletion(); 92 return; 93 } 94 95 try 96 { 97 AbstractConnectionManager cm = backend.getConnectionManager(login); 98 if (cm == null) 99 { 100 SQLException se = new SQLException ( 101 "No Connection Manager for Virtual Login:" + login); 102 try 103 { 104 notifyFailure(backendThread, 1, se); 105 } 106 catch (SQLException ignore) 107 { 108 109 } 110 throw se; 111 } 112 113 Connection c; 114 Long lTid = new Long (transactionId); 115 Trace logger = backendThread.getLogger(); 116 117 try 118 { 119 c = backend 120 .getConnectionForTransactionAndLazyBeginIfNeeded( 121 lTid, 122 cm, 123 org.objectweb.cjdbc.driver.Connection.DEFAULT_TRANSACTION_ISOLATION_LEVEL); 124 } 125 catch (UnreachableBackendException ube) 126 { 127 SQLException se = new SQLException ("Backend " + backend.getName() 128 + " is no more reachable."); 129 try 130 { 131 notifyFailure(backendThread, 1, se); 132 } 133 catch (SQLException ignore) 134 { 135 } 136 backendThread.kill(); 139 logger.error("Disabling backend " + backend.getName() 140 + " because it is no more reachable."); 141 throw se; 142 } 143 catch (NoTransactionStartWhenDisablingException e) 144 { 145 notifyCompletion(); 149 return; 150 } 151 catch (SQLException e1) 152 { 153 SQLException se = new SQLException ( 154 "Unable to get connection for transaction " + lTid); 155 try 156 { if (!notifyFailure(backendThread, timeout * 1000, se)) 158 return; 159 } 160 catch (SQLException ignore) 161 { 162 } 163 backendThread.kill(); 166 String msg = "Begin of transaction " + transactionId 167 + " failed on backend " + backend.getName() + " but " 168 + getSuccess() + " succeeded (" + se + ")"; 169 logger.error(msg); 170 throw new SQLException (msg); 171 } 172 173 if (c == null) 175 { SQLException se = new SQLException ( 177 "No more connection to start a new transaction."); 178 try 179 { if (!notifyFailure(backendThread, timeout, se)) 181 return; 182 } 183 catch (SQLException ignore) 184 { 185 } 186 } 187 else 188 { 189 notifyCompletion(); 190 } 191 } 192 catch (Exception e) 193 { 194 try 195 { 196 if (!notifyFailure(backendThread, timeout, new SQLException (e 197 .getMessage()))) 198 return; 199 } 200 catch (SQLException ignore) 201 { 202 } 203 String msg = "Failed to begin transaction " + transactionId 204 + " on backend " + backend.getName() + " (" + e + ")"; 205 backendThread.getLogger().error(msg); 206 throw new SQLException (msg); 207 } 208 } 209 210 213 public String toString() 214 { 215 return "BeginTask (" + transactionId + ")"; 216 } 217 } | Popular Tags |