KickJava   Java API By Example, From Geeks To Geeks.

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


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

42 public class ExecReadStoredProcedure extends DistributedRequest
43 {
44   private static final long serialVersionUID = 8634424524848530342L;
45
46   private int numberOfEnabledBackends;
47
48   /**
49    * Creates a new <code>ExecReadStoredProcedure</code> object to execute a
50    * read stored procedure on multiple controllers.
51    *
52    * @param proc the stored procedure to execute
53    */

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

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

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