KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > virtualdatabase > protocol > ExecWriteRequest


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.controller.virtualdatabase.protocol;
26
27 import java.sql.SQLException JavaDoc;
28
29 import org.objectweb.cjdbc.common.exceptions.NoMoreBackendException;
30 import org.objectweb.cjdbc.common.i18n.Translate;
31 import org.objectweb.cjdbc.common.sql.AbstractWriteRequest;
32 import org.objectweb.cjdbc.controller.loadbalancer.AllBackendsFailedException;
33 import org.objectweb.cjdbc.controller.requestmanager.distributed.DistributedRequestManager;
34
35 /**
36  * Execute a write request between several controllers.
37  *
38  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
39  * @version 1.0
40  */

41 public class ExecWriteRequest extends DistributedRequest
42 {
43   private static final long serialVersionUID = -4849018153223926739L;
44   private int numberOfEnabledBackends;
45
46   /**
47    * @param request write request to execute
48    */

49   public ExecWriteRequest(AbstractWriteRequest request)
50   {
51     super(request);
52   }
53
54   /**
55    * @see org.objectweb.cjdbc.controller.virtualdatabase.protocol.DistributedRequest#scheduleRequest(org.objectweb.cjdbc.controller.requestmanager.distributed.DistributedRequestManager)
56    */

57   public void scheduleRequest(DistributedRequestManager drm)
58       throws SQLException JavaDoc
59   {
60     // This call will trigger a lazyTransactionStart as well
61
drm.scheduleExecWriteRequest((AbstractWriteRequest) request);
62   }
63
64   /**
65    * @see org.objectweb.cjdbc.controller.virtualdatabase.protocol.DistributedRequest#executeScheduledRequest(org.objectweb.cjdbc.controller.requestmanager.distributed.DistributedRequestManager)
66    */

67   public Object JavaDoc executeScheduledRequest(DistributedRequestManager drm)
68       throws SQLException JavaDoc
69   {
70     numberOfEnabledBackends = drm.getLoadBalancer()
71         .getNumberOfEnabledBackends();
72
73     try
74     {
75       int execWriteRequestResult = 0;
76       if (numberOfEnabledBackends == 0)
77         throw new NoMoreBackendException(
78             "No backend enabled on this controller");
79       execWriteRequestResult = drm
80           .loadBalanceExecWriteRequest((AbstractWriteRequest) request);
81       drm.updateAndNotifyExecWriteRequest((AbstractWriteRequest) request);
82       return new Integer JavaDoc(execWriteRequestResult);
83     }
84     catch (NoMoreBackendException e)
85     {
86       if (drm.getLogger().isDebugEnabled())
87         drm.getLogger().debug(
88             Translate.get("virtualdatabase.distributed.write.logging.only",
89                 request.getSQLShortForm(drm.getVirtualDatabase()
90                     .getSQLShortFormLength())));
91
92       // Log the query in any case for later recovery (if the request really
93
// failed, it will be unloged later)
94
if (drm.getRecoveryLog() != null)
95       {
96         if (numberOfEnabledBackends == 0)
97         { // Wait to be sure that we log in the proper order
98
if (drm.getLoadBalancer().waitForTotalOrder(request, false))
99             drm.getLoadBalancer().removeHeadFromAndNotifyTotalOrderQueue();
100         }
101         long logId = drm.getRecoveryLog().logRequest(
102             (AbstractWriteRequest) request);
103         e.setRecoveryLogId(logId);
104       }
105       // Notify scheduler of completion
106
drm.getScheduler().writeCompleted((AbstractWriteRequest) request);
107       throw e;
108     }
109     catch (AllBackendsFailedException e)
110     {
111       // Add to failed list, the scheduler will be notified when the response
112
// will be received from the other controllers.
113
drm.getScheduler().writeCompleted((AbstractWriteRequest) request);
114       drm.addFailedOnAllBackends(request);
115       if (drm.getLogger().isDebugEnabled())
116         drm
117             .getLogger()
118             .debug(
119                 Translate
120                     .get(
121                         "virtualdatabase.distributed.write.all.backends.locally.failed",
122                         request.getSQLShortForm(drm.getVirtualDatabase()
123                             .getSQLShortFormLength())));
124       return e;
125     }
126     catch (SQLException JavaDoc e)
127     {
128       // Something bad more likely happened during the notification. Let's
129
// notify the scheduler (possibly again) to be safer.
130
drm.getScheduler().writeCompleted((AbstractWriteRequest) request);
131       drm.getLogger().warn(
132           Translate.get("virtualdatabase.distributed.write.sqlexception", e
133               .getMessage()), e);
134       throw e;
135     }
136     catch (RuntimeException JavaDoc re)
137     {
138       // Something bad more likely happened during the notification. Let's
139
// notify the scheduler (possibly again) to be safer.
140
drm.getScheduler().writeCompleted((AbstractWriteRequest) request);
141       drm.getLogger().warn(
142           Translate.get("virtualdatabase.distributed.write.exception", re
143               .getMessage()), re);
144       throw new SQLException JavaDoc(re.getMessage());
145     }
146   }
147
148 }
Popular Tags