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 Commit extends DistributedTransactionMarker 45 { 46 private static final long serialVersionUID = 1222810057093662283L; 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 Commit(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().commit(tm); 94 } 95 catch (SQLException e) 96 { 97 drm.getLogger().warn( 98 Translate.get("virtualdatabase.distributed.commit.sqlexception"), e); 99 throw e; 100 } 101 catch (RuntimeException re) 102 { 103 drm.getLogger().warn( 104 Translate.get("virtualdatabase.distributed.commit.exception"), re); 105 throw new SQLException (re.getMessage()); 106 } 107 } 108 109 118 public Object executeCommand(DistributedRequestManager drm) 119 throws SQLException 120 { 121 Trace logger = drm.getLogger(); 122 numberOfEnabledBackends = drm.getLoadBalancer() 123 .getNumberOfEnabledBackends(); 124 try 125 { 126 if (numberOfEnabledBackends == 0) 127 throw new NoMoreBackendException( 128 "No backend enabled on this controller"); 129 130 if (logger.isDebugEnabled()) 131 logger.debug(Translate.get("transaction.commit", "" + tid)); 132 133 drm.getLoadBalancer().commit(tm); 135 136 if (drm.getResultCache() != null) 138 drm.getResultCache().commit(tm.getTransactionId()); 139 140 if (drm.getRecoveryLog() != null) 142 drm.getRecoveryLog().logCommit(tm); 143 } 144 catch (NoMoreBackendException e) 145 { 146 if (drm.getRecoveryLog() != null) 149 { 150 if (logger.isDebugEnabled()) 151 logger 152 .debug(Translate.get( 153 "virtualdatabase.distributed.commit.logging.only", 154 transactionId)); 155 156 if (numberOfEnabledBackends == 0) 157 { Commit totalOrderCommit = new Commit(tm.getLogin(), transactionId); 159 if (drm.getLoadBalancer().waitForTotalOrder(totalOrderCommit, false)) 160 drm.getLoadBalancer().removeHeadFromAndNotifyTotalOrderQueue(); 161 } 162 long logId = drm.getRecoveryLog().logCommit(tm); 163 e.setRecoveryLogId(logId); 164 e.setLogin(tm.getLogin()); 165 } 166 throw e; 167 } 168 catch (SQLException e) 169 { 170 logger.warn(Translate 171 .get("virtualdatabase.distributed.commit.sqlexception"), e); 172 return e; 173 } 174 catch (RuntimeException re) 175 { 176 logger.warn( 177 Translate.get("virtualdatabase.distributed.commit.exception"), re); 178 throw new SQLException (re.getMessage()); 179 } 180 catch (AllBackendsFailedException e) 181 { 182 AbstractRequest request = new UnknownRequest("commit", false, 0, "\n"); 183 request.setTransactionId(transactionId); 184 drm.addFailedOnAllBackends(request); 185 if (logger.isDebugEnabled()) 186 logger.debug(Translate.get( 187 "virtualdatabase.distributed.commit.all.backends.locally.failed", 188 transactionId)); 189 return e; 190 } 191 finally 192 { 193 if (transactionStartedOnThisController) 194 { 195 drm.getScheduler().commitCompleted(transactionId); 197 drm.completeTransaction(tid); 198 } 199 } 200 return Boolean.TRUE; 201 } 202 203 206 public String toString() 207 { 208 return "Commit transaction " + transactionId; 209 } 210 } | Popular Tags |