KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > virtualdatabase > protocol > DistributedOpenPersistentConnection


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2006 Continuent, Inc.
4  * Contact: sequoia@continuent.org
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Initial developer(s): Emmanuel Cecchet.
19  * Contributor(s): ______________________.
20  */

21
22 package org.continuent.sequoia.controller.virtualdatabase.protocol;
23
24 import java.io.Serializable JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.util.LinkedList JavaDoc;
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 /**
38  * This class defines a DistributedOpenPersistentConnection
39  *
40  * @author <a HREF="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet</a>
41  * @version 1.0
42  */

43 public class DistributedOpenPersistentConnection
44     extends DistributedVirtualDatabaseMessage
45 {
46   private static final long serialVersionUID = -693544521730643721L;
47   private String JavaDoc login;
48   private long persistentConnectionId;
49
50   /**
51    * Creates a new <code>DistributedOpenPersistentConnection</code> object
52    *
53    * @param login login to retrieve the connection manager
54    * @param persistentConnectionId persistent connection id
55    */

56   public DistributedOpenPersistentConnection(String JavaDoc login,
57       long persistentConnectionId)
58   {
59     this.login = login;
60     this.persistentConnectionId = persistentConnectionId;
61   }
62
63   /**
64    * Returns the login value.
65    *
66    * @return Returns the login.
67    */

68   public final String JavaDoc getLogin()
69   {
70     return login;
71   }
72
73   /**
74    * Returns the persistentConnectionId value.
75    *
76    * @return Returns the persistentConnectionId.
77    */

78   public final long getPersistentConnectionId()
79   {
80     return persistentConnectionId;
81   }
82
83   /**
84    * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedVirtualDatabaseMessage#handleMessageSingleThreaded(org.continuent.sequoia.controller.virtualdatabase.DistributedVirtualDatabase,
85    * org.continuent.hedera.common.Member)
86    */

87   public Object JavaDoc handleMessageSingleThreaded(DistributedVirtualDatabase dvdb,
88       Member sender)
89   {
90     if (!dvdb.isVirtualDatabaseStarted())
91       return new VirtualDatabaseStartingException();
92
93     LinkedList JavaDoc totalOrderQueue = dvdb.getTotalOrderQueue();
94     synchronized (totalOrderQueue)
95     {
96       totalOrderQueue.addLast(this);
97     }
98     return this;
99   }
100
101   /**
102    * @see org.continuent.sequoia.controller.virtualdatabase.protocol.DistributedVirtualDatabaseMessage#handleMessageMultiThreaded(org.continuent.sequoia.controller.virtualdatabase.DistributedVirtualDatabase,
103    * org.continuent.hedera.common.Member, java.lang.Object)
104    */

105   public Serializable JavaDoc handleMessageMultiThreaded(
106       DistributedVirtualDatabase dvdb, Member sender,
107       Object JavaDoc handleMessageSingleThreadedResult)
108   {
109     if (handleMessageSingleThreadedResult instanceof Exception JavaDoc)
110       return (Serializable JavaDoc) 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 JavaDoc e)
139       {
140         throw e;
141       }
142       finally
143       {
144         scheduler.openPersistentConnectionCompleted(persistentConnectionId,
145             success);
146       }
147       return Boolean.TRUE;
148     }
149     catch (SQLException JavaDoc 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   /**
162    * @see java.lang.Object#equals(java.lang.Object)
163    */

164   public boolean equals(Object JavaDoc 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   /**
175    * @see java.lang.Object#hashCode()
176    */

177   public int hashCode()
178   {
179     return (int) persistentConnectionId;
180   }
181 }
182
Popular Tags