KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > recoverylog > events > GetUpdateCountEvent


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): Damian Arregui.
19  * Contributor(s): ______________________.
20  */

21
22 package org.continuent.sequoia.controller.recoverylog.events;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.PreparedStatement JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.SQLException JavaDoc;
28
29 import org.continuent.sequoia.common.exceptions.NoResultAvailableException;
30 import org.continuent.sequoia.common.i18n.Translate;
31 import org.continuent.sequoia.controller.recoverylog.LoggerThread;
32 import org.continuent.sequoia.controller.recoverylog.RecoveryLog;
33
34 /**
35  * This class defines a GetUpdateCountEvent.
36  * <p>
37  * This event is used to retrieve an update count associated to a request ID.
38  * <br/>Once this event has been executed, the update count is available through
39  * the <code>getUpdateCount)</code> method.
40  *
41  * @author <a HREF="mailto:damian.arregui@continuent.com">Damian Arregui</a>
42  * @version 1.0
43  */

44 public class GetUpdateCountEvent implements LogEvent
45 {
46   private Connection JavaDoc connection;
47   private RecoveryLog recoveryLog;
48   private long requestId;
49   private int updateCount = -1;
50   private Exception JavaDoc catchedException = null;
51
52   /**
53    * Creates a new <code>GetUpdateCountEvent</code> object
54    *
55    * @param connection the connection used to access the recovery log table
56    * @param recoveryLog the recovery log object
57    * @param requestId the ID of the request to be found
58    */

59   public GetUpdateCountEvent(Connection JavaDoc connection, RecoveryLog recoveryLog,
60       long requestId)
61   {
62     this.connection = connection;
63     this.recoveryLog = recoveryLog;
64     this.requestId = requestId;
65   }
66
67   /**
68    * @see org.continuent.sequoia.controller.recoverylog.events.LogEvent#belongToTransaction(long)
69    */

70   public boolean belongToTransaction(long tid)
71   {
72     return false;
73   }
74
75   /**
76    * @see org.continuent.sequoia.controller.recoverylog.events.LogEvent#execute(org.continuent.sequoia.controller.recoverylog.LoggerThread)
77    */

78   public void execute(LoggerThread loggerThread)
79   {
80     PreparedStatement JavaDoc stmt = null;
81     ResultSet JavaDoc rs = null;
82     try
83     {
84       stmt = connection
85           .prepareStatement("SELECT update_count, exec_status FROM "
86               + recoveryLog.getLogTableName() + " WHERE request_id=?");
87       stmt.setLong(1, requestId);
88       rs = stmt.executeQuery();
89       if (rs.next())
90       {
91         // Found!
92
updateCount = rs.getInt(1);
93         if (!LogEntry.SUCCESS.equals(rs.getString(2)))
94           throw new SQLException JavaDoc(Translate.get("recovery.jdbc.update.failed"));
95       }
96       else
97       {
98         // Not found
99
String JavaDoc msg = Translate.get("recovery.jdbc.updatecount.not.found",
100             requestId);
101         catchedException = new NoResultAvailableException(msg);
102       }
103     }
104     catch (SQLException JavaDoc e)
105     {
106       catchedException = new SQLException JavaDoc(Translate.get(
107           "recovery.jdbc.updatecount.not.found.error", new Object JavaDoc[]{
108               new Long JavaDoc(requestId), e.getMessage()}));
109     }
110     finally
111     {
112       try
113       {
114         if (rs != null)
115           rs.close();
116       }
117       catch (Exception JavaDoc ignore)
118       {
119       }
120       try
121       {
122         if (stmt != null)
123           stmt.close();
124       }
125       catch (Exception JavaDoc ignore)
126       {
127       }
128       synchronized (this)
129       {
130         notify();
131       }
132     }
133   }
134
135   /**
136    * Returns the catched exception if an error occured during the execution (can
137    * be SQLException or NoResultAvailableException). Returns null if no error
138    * occured or the query did not execute yet.
139    *
140    * @return Returns the catchedException.
141    */

142   public final Exception JavaDoc getCatchedException()
143   {
144     return catchedException;
145   }
146
147   /**
148    * Gets the update count corresponding to the request ID
149    *
150    * @return the update count that has been found
151    */

152   public int getUpdateCount()
153   {
154     return updateCount;
155   }
156
157   /**
158    * @see java.lang.Object#toString()
159    */

160   public String JavaDoc toString()
161   {
162     return "GetUpdateCountEvent for request ID " + requestId;
163   }
164 }
165
Popular Tags