KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.objectweb.cjdbc.controller.virtualdatabase.ControllerResultSet;
35
36 /**
37  * Execute a write request between several controllers.
38  *
39  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
40  * @version 1.0
41  */

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

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

58   public void scheduleRequest(DistributedRequestManager drm)
59       throws SQLException JavaDoc
60   {
61     numberOfEnabledBackends = drm.getLoadBalancer()
62         .getNumberOfEnabledBackends();
63     if (numberOfEnabledBackends == 0)
64     { // Even if we do not execute this query, we have to log the begin if any
65
drm.lazyTransactionStart(request);
66     }
67     else
68       drm.scheduleExecWriteRequest((AbstractWriteRequest) request);
69   }
70
71   /**
72    * @see org.objectweb.cjdbc.controller.virtualdatabase.protocol.DistributedRequest#executeScheduledRequest(org.objectweb.cjdbc.controller.requestmanager.distributed.DistributedRequestManager)
73    */

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