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.log.Trace; 32 import org.objectweb.cjdbc.common.sql.AbstractRequest; 33 import org.objectweb.cjdbc.common.sql.UnknownRequest; 34 import org.objectweb.cjdbc.controller.loadbalancer.AllBackendsFailedException; 35 import org.objectweb.cjdbc.controller.requestmanager.TransactionMarkerMetaData; 36 import org.objectweb.cjdbc.controller.requestmanager.distributed.DistributedRequestManager; 37 38 44 public class Rollback extends DistributedTransactionMarker 45 { 46 private static final long serialVersionUID = -8954391235872189513L; 47 48 private transient TransactionMarkerMetaData tm = null; 49 private transient Long tid; 50 private transient int numberOfEnabledBackends; 51 private transient boolean transactionStartedOnThisController; 52 private String login; 56 57 63 public Rollback(String login, long transactionId) 64 { 65 super(transactionId); 66 this.login = login; 67 } 68 69 72 public void scheduleCommand(DistributedRequestManager drm) 73 throws SQLException 74 { 75 transactionStartedOnThisController = true; 76 tid = new Long (transactionId); 77 try 78 { 79 tm = drm.getTransactionMarker(tid); 80 } 81 catch (SQLException ignore) 82 { 83 transactionStartedOnThisController = false; 86 tm = new TransactionMarkerMetaData(transactionId, 0, login); 87 return; 88 } 89 90 try 91 { 92 drm.getScheduler().rollback(tm); 94 } 95 catch (SQLException e) 96 { 97 drm.getLogger() 98 .warn( 99 Translate 100 .get("virtualdatabase.distributed.rollback.sqlexception"), e); 101 throw e; 102 } 103 catch (RuntimeException re) 104 { 105 drm.getLogger().warn( 106 Translate.get("virtualdatabase.distributed.rollback.exception"), re); 107 throw new SQLException (re.getMessage()); 108 } 109 } 110 111 120 public Object executeCommand(DistributedRequestManager drm) 121 throws SQLException 122 { 123 Trace logger = drm.getLogger(); 124 numberOfEnabledBackends = drm.getLoadBalancer() 125 .getNumberOfEnabledBackends(); 126 try 127 { 128 if (numberOfEnabledBackends == 0) 129 throw new NoMoreBackendException( 130 "No backend enabled on this controller"); 131 132 if (logger.isDebugEnabled()) 133 logger.debug(Translate.get("transaction.rollback", "" + tid)); 134 135 drm.getLoadBalancer().rollback(tm); 137 138 if (drm.getResultCache() != null) 140 drm.getResultCache().rollback(tm.getTransactionId()); 141 142 if (drm.getRecoveryLog() != null) 144 drm.getRecoveryLog().logRollback(tm); 145 } 146 catch (NoMoreBackendException e) 147 { 148 if (drm.getRecoveryLog() != null) 151 { 152 if (logger.isDebugEnabled()) 153 logger.debug(Translate.get( 154 "virtualdatabase.distributed.rollback.logging.only", 155 transactionId)); 156 157 if (numberOfEnabledBackends == 0) 158 { Rollback totalOrderRollback = new Rollback(tm.getLogin(), 160 transactionId); 161 if (drm.getLoadBalancer() 162 .waitForTotalOrder(totalOrderRollback, false)) 163 drm.getLoadBalancer().removeHeadFromAndNotifyTotalOrderQueue(); 164 } 165 long logId = drm.getRecoveryLog().logRollback(tm); 166 e.setRecoveryLogId(logId); 167 e.setLogin(tm.getLogin()); 168 } 169 throw e; 170 } 171 catch (SQLException e) 172 { 173 logger.warn(Translate 174 .get("virtualdatabase.distributed.rollback.sqlexception"), e); 175 return e; 176 } 177 catch (RuntimeException re) 178 { 179 logger.warn(Translate 180 .get("virtualdatabase.distributed.rollback.exception"), re); 181 throw new SQLException (re.getMessage()); 182 } 183 catch (AllBackendsFailedException e) 184 { 185 AbstractRequest request = new UnknownRequest("rollback", false, 0, "\n"); 186 request.setTransactionId(transactionId); 187 drm.addFailedOnAllBackends(request); 188 if (logger.isDebugEnabled()) 189 logger.debug(Translate.get( 190 "virtualdatabase.distributed.commit.all.backends.locally.failed", 191 transactionId)); 192 return e; 193 } 194 finally 195 { 196 if (transactionStartedOnThisController) 197 { 198 drm.getScheduler().rollbackCompleted(transactionId); 200 drm.completeTransaction(tid); 201 } 202 } 203 return Boolean.TRUE; 204 } 205 206 209 public String toString() 210 { 211 return "Rollback transaction " + transactionId; 212 } 213 } | Popular Tags |