KickJava   Java API By Example, From Geeks To Geeks.

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


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.i18n.Translate;
30 import org.continuent.sequoia.controller.recoverylog.LoggerThread;
31 import org.continuent.sequoia.controller.recoverylog.RecoveryLog;
32
33 /**
34  * This class defines a FindCommitEvent.
35  * <p>
36  * This event is used to retrieve a commit associated to a transaction ID.
37  * <br/>Once this event has been executed, the fact that the commit was found or
38  * not can be checked through the <code>wasFound()</code> method.
39  *
40  * @author <a HREF="mailto:damian.arregui@continuent.com">Damian Arregui</a>
41  * @version 1.0
42  */

43 public class FindCommitEvent implements LogEvent
44 {
45   private Connection JavaDoc connection;
46   private RecoveryLog recoveryLog;
47   private long transactionId;
48   private boolean found = false;
49   private String JavaDoc status = LogEntry.MISSING;
50   private SQLException JavaDoc catchedException = null;
51
52   /**
53    * Creates a new <code>FindCommitEvent</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 FindCommitEvent(Connection JavaDoc connection, RecoveryLog recoveryLog,
60       long requestId)
61   {
62     this.connection = connection;
63     this.recoveryLog = recoveryLog;
64     this.transactionId = 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.prepareStatement("SELECT exec_status FROM "
85           + recoveryLog.getLogTableName() + " WHERE ((transaction_id=?) AND ("
86           + recoveryLog.getLogTableSqlColumnName() + "='commit'))");
87       stmt.setLong(1, transactionId);
88       rs = stmt.executeQuery();
89
90       // Found?
91
found = rs.next();
92       if (found)
93         status = rs.getString(1);
94     }
95     catch (SQLException JavaDoc e)
96     {
97       catchedException = new SQLException JavaDoc(Translate.get(
98           "recovery.jdbc.commit.not.found.error", new Object JavaDoc[]{
99               new Long JavaDoc(transactionId), e.getMessage()}));
100     }
101     finally
102     {
103       try
104       {
105         if (rs != null)
106           rs.close();
107       }
108       catch (Exception JavaDoc ignore)
109       {
110       }
111       try
112       {
113         if (stmt != null)
114           stmt.close();
115       }
116       catch (Exception JavaDoc ignore)
117       {
118       }
119       synchronized (this)
120       {
121         notify();
122       }
123     }
124   }
125
126   /**
127    * Returns the catched exception if an error occured during the execution.
128    * Returns null if no error occured or the query did not execute yet.
129    *
130    * @return Returns the catchedException.
131    */

132   public final SQLException JavaDoc getCatchedException()
133   {
134     return catchedException;
135   }
136
137   /**
138    * Returns the status of the commit statement if found in the recovery log
139    *
140    * @return the status the commit statement as found in the recovery log, or an
141    * empty string if not found
142    */

143   public String JavaDoc getStatus()
144   {
145     return status;
146   }
147
148   /**
149    * Checks if the commit was found.
150    *
151    * @return true if the commit has been found, false otherwise
152    */

153   public boolean wasFound()
154   {
155     return found;
156   }
157
158   /**
159    * @see java.lang.Object#toString()
160    */

161   public String JavaDoc toString()
162   {
163     return "FindCommitEvent for request ID " + transactionId;
164   }
165 }
166
Popular Tags