1 24 25 package org.objectweb.cjdbc.controller.virtualdatabase.protocol; 26 27 import java.sql.SQLException ; 28 29 import org.objectweb.cjdbc.common.exceptions.NoMoreBackendException; 30 import org.objectweb.cjdbc.common.i18n.Translate; 31 import org.objectweb.cjdbc.common.sql.AbstractRequest; 32 import org.objectweb.cjdbc.common.sql.UnknownRequest; 33 import org.objectweb.cjdbc.controller.loadbalancer.AllBackendsFailedException; 34 import org.objectweb.cjdbc.controller.requestmanager.TransactionMarkerMetaData; 35 import org.objectweb.cjdbc.controller.requestmanager.distributed.DistributedRequestManager; 36 37 44 public class RollbackToSavepoint extends DistributedTransactionMarker 45 { 46 private static final long serialVersionUID = -8670132997537808225L; 47 48 private String savepointName; 49 private TransactionMarkerMetaData tm; 50 private Long tid; 51 private int numberOfEnabledBackends; 52 53 59 public RollbackToSavepoint(long transactionId, String savepointName) 60 { 61 super(transactionId); 62 this.savepointName = savepointName; 63 } 64 65 68 public void scheduleCommand(DistributedRequestManager drm) 69 throws SQLException 70 { 71 try 72 { 73 tid = new Long (transactionId); 76 tm = drm.getTransactionMarker(tid); 77 78 numberOfEnabledBackends = drm.getLoadBalancer() 79 .getNumberOfEnabledBackends(); 80 if (numberOfEnabledBackends == 0) 81 return; 83 if (!drm.hasSavepoint(tid, savepointName)) 85 throw new SQLException (Translate.get("transaction.savepoint.not.found", 86 new String []{savepointName, String.valueOf(transactionId)})); 87 88 drm.getScheduler().rollback(tm, savepointName); 90 } 91 catch (SQLException e) 92 { 93 drm 94 .getLogger() 95 .warn( 96 Translate 97 .get("virtualdatabase.distributed.rollbacksavepoint.sqlexception"), 98 e); 99 throw e; 100 } 101 catch (RuntimeException re) 102 { 103 drm.getLogger().warn( 104 Translate 105 .get("virtualdatabase.distributed.rollbacksavepoint.exception"), 106 re); 107 throw new SQLException (re.getMessage()); 108 } 109 } 110 111 114 public Object executeCommand(DistributedRequestManager drm) 115 throws SQLException 116 { 117 try 118 { 119 if (numberOfEnabledBackends == 0) 120 throw new NoMoreBackendException( 121 "No backend enabled on this controller"); 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().rollback(tm, savepointName); 130 131 if (drm.getRecoveryLog() != null) 133 { 134 drm.getRecoveryLog().logRollback(tm, savepointName); 135 } 136 } 137 catch (NoMoreBackendException e) 138 { 139 if (drm.getRecoveryLog() != null) 142 { 143 if (drm.getLogger().isDebugEnabled()) 144 drm.getLogger().debug( 145 Translate.get( 146 "virtualdatabase.distributed.rollbacksavepoint.logging.only", 147 new String []{savepointName, String.valueOf(transactionId)})); 148 long logId = drm.getRecoveryLog().logRollback(tm, savepointName); 149 e.setRecoveryLogId(logId); 150 e.setLogin(tm.getLogin()); 151 } 152 throw e; 153 } 154 catch (SQLException e) 155 { 156 drm 157 .getLogger() 158 .warn( 159 Translate 160 .get("virtualdatabase.distributed.rollbacksavepoint.sqlexception"), 161 e); 162 return e; 163 } 164 catch (RuntimeException re) 165 { 166 drm.getLogger().warn( 167 Translate 168 .get("virtualdatabase.distributed.rollbacksavepoint.exception"), 169 re); 170 throw new SQLException (re.getMessage()); 171 } 172 catch (AllBackendsFailedException e) 173 { 174 AbstractRequest request = new UnknownRequest("rollback " + savepointName, 175 false, 0, "\n"); 176 request.setTransactionId(transactionId); 177 drm.addFailedOnAllBackends(request); 178 if (drm.getLogger().isDebugEnabled()) 179 drm 180 .getLogger() 181 .debug( 182 Translate 183 .get( 184 "virtualdatabase.distributed.rollbacksavepoint.all.backends.locally.failed", 185 new String []{savepointName, 186 String.valueOf(transactionId)})); 187 return e; 188 } 189 finally 190 { 191 if (numberOfEnabledBackends != 0) 192 { 193 drm.getScheduler().savepointCompleted(transactionId); 195 196 drm.removeSavepoints(tid, savepointName); 198 } 199 } 200 return Boolean.TRUE; 201 } 202 203 208 public String getSavepointName() 209 { 210 return savepointName; 211 } 212 213 216 public boolean equals(Object obj) 217 { 218 if (super.equals(obj)) 219 return savepointName.equals(((RollbackToSavepoint) obj) 220 .getSavepointName()); 221 else 222 return false; 223 } 224 225 228 public String toString() 229 { 230 return "Rollback transaction " + transactionId + " to savepoint " 231 + savepointName; 232 } 233 } 234 | Popular Tags |