1 21 22 package org.continuent.sequoia.controller.virtualdatabase.protocol; 23 24 import java.io.Serializable ; 25 import java.sql.SQLException ; 26 import java.util.LinkedList ; 27 28 import org.continuent.hedera.common.Member; 29 import org.continuent.sequoia.common.exceptions.NoMoreBackendException; 30 import org.continuent.sequoia.common.exceptions.VirtualDatabaseStartingException; 31 import org.continuent.sequoia.controller.recoverylog.RecoveryLog; 32 import org.continuent.sequoia.controller.requestmanager.distributed.DistributedRequestManager; 33 import org.continuent.sequoia.controller.requests.UnknownWriteRequest; 34 import org.continuent.sequoia.controller.scheduler.AbstractScheduler; 35 import org.continuent.sequoia.controller.virtualdatabase.DistributedVirtualDatabase; 36 37 43 public class DistributedOpenPersistentConnection 44 extends DistributedVirtualDatabaseMessage 45 { 46 private static final long serialVersionUID = -693544521730643721L; 47 private String login; 48 private long persistentConnectionId; 49 50 56 public DistributedOpenPersistentConnection(String login, 57 long persistentConnectionId) 58 { 59 this.login = login; 60 this.persistentConnectionId = persistentConnectionId; 61 } 62 63 68 public final String getLogin() 69 { 70 return login; 71 } 72 73 78 public final long getPersistentConnectionId() 79 { 80 return persistentConnectionId; 81 } 82 83 87 public Object handleMessageSingleThreaded(DistributedVirtualDatabase dvdb, 88 Member sender) 89 { 90 if (!dvdb.isVirtualDatabaseStarted()) 91 return new VirtualDatabaseStartingException(); 92 93 LinkedList totalOrderQueue = dvdb.getTotalOrderQueue(); 94 synchronized (totalOrderQueue) 95 { 96 totalOrderQueue.addLast(this); 97 } 98 return this; 99 } 100 101 105 public Serializable handleMessageMultiThreaded( 106 DistributedVirtualDatabase dvdb, Member sender, 107 Object handleMessageSingleThreadedResult) 108 { 109 if (handleMessageSingleThreadedResult instanceof Exception ) 110 return (Serializable ) handleMessageSingleThreadedResult; 111 112 dvdb.getRequestManager().getLoadBalancer().waitForTotalOrder(this, true); 113 114 DistributedRequestManager drm = ((DistributedRequestManager) dvdb 115 .getRequestManager()); 116 AbstractScheduler scheduler = drm.getScheduler(); 117 RecoveryLog recoveryLog = drm.getRecoveryLog(); 118 long entryId = -1; 119 try 120 { 121 boolean success = false; 122 try 123 { 124 scheduler.scheduleOpenPersistentConnection(this); 125 126 entryId = recoveryLog.logOpenPersistentConnection(login, 127 persistentConnectionId); 128 129 drm.getLoadBalancer().openPersistentConnection(login, 130 persistentConnectionId); 131 success = true; 132 recoveryLog.logRequestCompletion(entryId, success, 0); 133 } 134 catch (NoMoreBackendException e) 135 { 136 throw e; 137 } 138 catch (SQLException e) 139 { 140 throw e; 141 } 142 finally 143 { 144 scheduler.openPersistentConnectionCompleted(persistentConnectionId, 145 success); 146 } 147 return Boolean.TRUE; 148 } 149 catch (SQLException e) 150 { 151 UnknownWriteRequest notifRequest = new UnknownWriteRequest("open " 152 + persistentConnectionId, false, 0, null); 153 notifRequest.setLogId(entryId); 154 notifRequest.setPersistentConnection(true); 155 notifRequest.setPersistentConnectionId(persistentConnectionId); 156 drm.addFailedOnAllBackends(notifRequest, false); 157 return e; 158 } 159 } 160 161 164 public boolean equals(Object obj) 165 { 166 if (obj instanceof DistributedOpenPersistentConnection) 167 { 168 DistributedOpenPersistentConnection other = (DistributedOpenPersistentConnection) obj; 169 return persistentConnectionId == other.persistentConnectionId; 170 } 171 return false; 172 } 173 174 177 public int hashCode() 178 { 179 return (int) persistentConnectionId; 180 } 181 } 182 | Popular Tags |