KickJava   Java API By Example, From Geeks To Geeks.

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


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.StoredProcedure;
32 import org.objectweb.cjdbc.controller.loadbalancer.AllBackendsFailedException;
33 import org.objectweb.cjdbc.controller.requestmanager.distributed.DistributedRequestManager;
34
35 /**
36  * This execute a write stored procedure on multiple controllers.
37  *
38  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
39  * @version 1.0
40  */

41 public class ExecWriteStoredProcedure extends DistributedRequest
42 {
43   private static final long serialVersionUID = 3550127604843949629L;
44
45   private int numberOfEnabledBackends;
46
47   /**
48    * Execute a write stored procedure on multiple controllers.
49    *
50    * @param proc the stored procedure to execute
51    */

52   public ExecWriteStoredProcedure(StoredProcedure proc)
53   {
54     super(proc);
55   }
56
57   /**
58    * @see org.objectweb.cjdbc.controller.virtualdatabase.protocol.DistributedRequest#scheduleRequest(org.objectweb.cjdbc.controller.requestmanager.distributed.DistributedRequestManager)
59    */

60   public void scheduleRequest(DistributedRequestManager drm)
61       throws SQLException JavaDoc
62   {
63     // Even if we do not execute this query, we have to log the begin if any
64
drm.lazyTransactionStart(request);
65     drm.scheduleStoredProcedure((StoredProcedure) request);
66   }
67
68   /**
69    * @see org.objectweb.cjdbc.controller.virtualdatabase.protocol.DistributedRequest#executeScheduledRequest(org.objectweb.cjdbc.controller.requestmanager.distributed.DistributedRequestManager)
70    */

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