1 24 25 package org.objectweb.cjdbc.controller.loadbalancer.tasks; 26 27 import java.sql.Connection ; 28 import java.sql.SQLException ; 29 import java.sql.Savepoint ; 30 31 import org.objectweb.cjdbc.controller.backend.DatabaseBackend; 32 import org.objectweb.cjdbc.controller.connection.AbstractConnectionManager; 33 import org.objectweb.cjdbc.controller.loadbalancer.BackendWorkerThread; 34 35 42 public class RollbackToSavepointTask extends AbstractTask 43 { 44 45 private String login; 46 47 48 private long transactionId; 49 50 51 private long timeout; 52 53 54 private String savepointName; 55 56 66 public RollbackToSavepointTask(int nbToComplete, int totalNb, long timeout, 67 String login, long transactionId, String savepointName) 68 { 69 super(nbToComplete, totalNb); 70 this.login = login; 71 this.transactionId = transactionId; 72 this.timeout = timeout; 73 this.savepointName = savepointName; 74 } 75 76 79 public void executeTask(BackendWorkerThread backendThread) 80 throws SQLException 81 { 82 DatabaseBackend db = backendThread.getBackend(); 83 Long lTid = new Long (transactionId); 84 85 AbstractConnectionManager cm = db.getConnectionManager(login); 86 if (cm == null) 87 { 88 SQLException se = new SQLException ( 89 "No Connection Manager for Virtual Login:" + login); 90 try 91 { 92 notifyFailure(backendThread, 1, se); 93 } 94 catch (SQLException ignore) 95 { 96 97 } 98 throw se; 99 } 100 101 Connection c = cm.retrieveConnection(transactionId); 102 103 if (c == null) 105 { db.stopTransaction(lTid); 107 SQLException se = new SQLException ( 108 "Unable to retrieve connection for transaction " + transactionId); 109 try 110 { if (!notifyFailure(backendThread, timeout, se)) 112 return; 113 } 114 catch (SQLException ignore) 115 { 116 } 117 backendThread.kill(); 120 String msg = "Failed to rollback transaction " + transactionId 121 + " to savepoint " + savepointName + " on backend " + db.getName() 122 + " but " + getSuccess() + " succeeded (" + se + ")"; 123 backendThread.getLogger().error(msg); 124 throw new SQLException (msg); 125 } 126 127 boolean hasSavepoint = false; 129 try 130 { 131 hasSavepoint = db.hasSavepointForTransaction(lTid, savepointName); 132 if (hasSavepoint) 133 { 134 Savepoint savepoint = db.getSavepoint(lTid, savepointName); 135 c.rollback(savepoint); 136 } 137 else 138 { 139 c.rollback(); 140 c.setAutoCommit(true); 141 } 142 backendThread.getBackend().setSchemaIsDirty(true); 144 } 145 catch (Exception e) 146 { 147 try 148 { 149 if (!notifyFailure(backendThread, timeout, new SQLException (e 150 .getMessage()))) 151 return; 152 } 153 catch (SQLException ignore) 154 { 155 } 156 backendThread.kill(); 159 String msg = "Failed to rollback transaction " + transactionId 160 + " to savepoint " + savepointName + " on backend " + db.getName() 161 + " but " + getSuccess() + " succeeded (" + e + ")"; 162 backendThread.getLogger().error(msg); 163 throw new SQLException (msg); 164 } 165 finally 166 { 167 if (! hasSavepoint) 168 { 169 cm.releaseConnection(transactionId); 170 db.stopTransaction(lTid); 171 } 172 } 173 notifySuccess(); 174 } 175 176 179 public String toString() 180 { 181 return "RollbackToSavepointTask for transaction " + transactionId + " (" 182 + savepointName + ")"; 183 } 184 } 185 | Popular Tags |