1 24 25 package org.continuent.sequoia.controller.virtualdatabase.protocol; 26 27 import java.io.Serializable ; 28 import java.sql.SQLException ; 29 import java.util.LinkedList ; 30 31 import org.continuent.sequoia.common.exceptions.NoMoreBackendException; 32 import org.continuent.sequoia.common.i18n.Translate; 33 import org.continuent.sequoia.controller.loadbalancer.AllBackendsFailedException; 34 import org.continuent.sequoia.controller.requestmanager.TransactionMetaData; 35 import org.continuent.sequoia.controller.requestmanager.distributed.DistributedRequestManager; 36 import org.continuent.sequoia.controller.requests.AbstractRequest; 37 import org.continuent.sequoia.controller.requests.UnknownWriteRequest; 38 39 48 public class DistributedRollbackToSavepoint 49 extends DistributedTransactionMarker 50 { 51 private static final long serialVersionUID = -8670132997537808225L; 52 53 private String savepointName; 54 55 61 public DistributedRollbackToSavepoint(long transactionId, String savepointName) 62 { 63 super(transactionId); 64 this.savepointName = savepointName; 65 } 66 67 70 public Object scheduleCommand(DistributedRequestManager drm) 71 throws SQLException 72 { 73 LinkedList totalOrderQueue = drm.getVirtualDatabase().getTotalOrderQueue(); 74 if (totalOrderQueue != null) 75 { 76 synchronized (totalOrderQueue) 77 { 78 totalOrderQueue.addLast(this); 79 } 80 } 81 return this; 82 } 83 84 87 public Serializable executeCommand(DistributedRequestManager drm) 88 throws SQLException 89 { 90 boolean hasBeenScheduled = false; 91 92 Long tid = new Long (transactionId); 95 TransactionMetaData tm; 96 try 97 { 98 tm = drm.getTransactionMetaData(tid); 99 } 100 catch (SQLException e) 101 { 102 drm.getLoadBalancer().removeObjectFromAndNotifyTotalOrderQueue(this); 104 throw e; 105 } 106 107 if (!drm.hasSavepoint(tid, savepointName)) 109 { 110 drm.getLoadBalancer().removeObjectFromAndNotifyTotalOrderQueue(this); 112 addRollbackFailureOnAllBackends(drm, hasBeenScheduled, tm); 113 throw new SQLException (Translate.get("transaction.savepoint.not.found", 114 new String []{savepointName, String.valueOf(transactionId)})); 115 } 116 117 try 118 { 119 drm.getScheduler().rollback(tm, savepointName, this); 121 hasBeenScheduled = true; 122 123 if (drm.getLogger().isDebugEnabled()) 124 drm.getLogger().debug( 125 Translate.get("transaction.rollbacksavepoint", new String []{ 126 savepointName, String.valueOf(transactionId)})); 127 128 drm.getLoadBalancer().rollbackToSavepoint(tm, savepointName); 130 131 drm.getRecoveryLog().logRequestCompletion(tm.getLogId(), true, 0); 133 134 drm.getScheduler().savepointCompleted(transactionId); 136 } 137 catch (NoMoreBackendException e) 138 { 139 if (drm.getLogger().isDebugEnabled()) 140 drm.getLogger().debug( 141 Translate.get( 142 "virtualdatabase.distributed.rollbacksavepoint.logging.only", 143 new String []{savepointName, String.valueOf(transactionId)})); 144 145 addRollbackFailureOnAllBackends(drm, hasBeenScheduled, tm); 146 throw e; 147 } 148 catch (SQLException e) 149 { 150 addRollbackFailureOnAllBackends(drm, hasBeenScheduled, tm); 151 drm 152 .getLogger() 153 .warn( 154 Translate 155 .get("virtualdatabase.distributed.rollbacksavepoint.sqlexception"), 156 e); 157 return e; 158 } 159 catch (RuntimeException re) 160 { 161 addRollbackFailureOnAllBackends(drm, hasBeenScheduled, tm); 162 drm.getLogger().warn( 163 Translate 164 .get("virtualdatabase.distributed.rollbacksavepoint.exception"), 165 re); 166 throw new SQLException (re.getMessage()); 167 } 168 catch (AllBackendsFailedException e) 169 { 170 addRollbackFailureOnAllBackends(drm, hasBeenScheduled, tm); 171 if (drm.getLogger().isDebugEnabled()) 172 drm 173 .getLogger() 174 .debug( 175 Translate 176 .get( 177 "virtualdatabase.distributed.rollbacksavepoint.all.backends.locally.failed", 178 new String []{savepointName, 179 String.valueOf(transactionId)})); 180 return e; 181 } 182 finally 183 { 184 drm.removeSavepoints(tid, savepointName); 186 } 187 return Boolean.TRUE; 188 } 189 190 private void addRollbackFailureOnAllBackends(DistributedRequestManager drm, 191 boolean hasBeenScheduled, TransactionMetaData tm) 192 { 193 AbstractRequest request = new UnknownWriteRequest("rollback " 194 + savepointName, false, 0, "\n"); 195 request.setTransactionId(transactionId); 196 request.setLogId(tm.getLogId()); 197 drm.addFailedOnAllBackends(request, hasBeenScheduled); 198 } 199 200 205 public String getSavepointName() 206 { 207 return savepointName; 208 } 209 210 213 public boolean equals(Object obj) 214 { 215 if (super.equals(obj)) 216 return savepointName.equals(((DistributedRollbackToSavepoint) obj) 217 .getSavepointName()); 218 else 219 return false; 220 } 221 222 225 public String toString() 226 { 227 return "Rollback transaction " + transactionId + " to savepoint " 228 + savepointName; 229 } 230 } 231 | Popular Tags |