KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jts > CosTransactions > LogDBHelper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28
29 package com.sun.jts.CosTransactions;
30
31 import java.util.Map JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.sql.*;
34 import javax.sql.*;
35 import javax.naming.*;
36 import java.lang.reflect.Method JavaDoc;
37
38 import java.util.logging.Logger JavaDoc;
39 import java.util.logging.Level JavaDoc;
40 import com.sun.logging.LogDomains;
41 import com.sun.jts.utils.LogFormatter;
42
43 /** The LogDBHelper class takes care of writing the transaction logs
44   * into database.
45   * @author Sun Micro Systems, Inc
46 */

47
48 class LogDBHelper {
49
50     String JavaDoc resName = "jdbc/TxnDS";
51     DataSource ds = null;
52     Method JavaDoc getNonTxConnectionMethod = null;
53     //DataSource ds_nontx = null;
54
static final String JavaDoc insertStatement =
55              System.getProperty("com.sun.jts.dblogging.insertquery",
56                  "insert into txn_log_table values ( ? , ? , ? )");
57     static final String JavaDoc deleteStatement =
58              System.getProperty("com.sun.jts.dblogging.deletequery",
59                  "delete from txn_log_table where localtid = ? and servername = ? ");
60     static final String JavaDoc selectStatement =
61              System.getProperty("com.sun.jts.dblogging.selectquery",
62                  "select * from txn_log_table where servername = ? ");
63     static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER);
64     static LogDBHelper _instance = new LogDBHelper();
65
66     static LogDBHelper getInstance() {
67         return _instance;
68     }
69
70     LogDBHelper() {
71         if (Configuration.getPropertyValue(Configuration.DB_LOG_RESOURCE) != null) {
72             resName = Configuration.getPropertyValue(Configuration.DB_LOG_RESOURCE);
73         }
74         try {
75             InitialContext ctx = new InitialContext();
76             // ds = (DataSource)ctx.lookup(resName + "__pm");
77
ds = (DataSource)ctx.lookup(resName);
78         Class JavaDoc cls = ds.getClass();
79         getNonTxConnectionMethod = cls.getMethod("getNonTxConnection", null);
80
81         //ds_nontx = (DataSource)ctx.lookup(resName + "__nontx");
82
} catch (Throwable JavaDoc t) {
83             _logger.log(Level.SEVERE,"jts.unconfigured_db_log_resource",resName);
84             _logger.log(Level.SEVERE,t.getLocalizedMessage(),t);
85         }
86     }
87
88
89     boolean addRecord(long localTID, byte[] data) {
90         if (ds != null) {
91             Connection conn = null;
92             PreparedStatement prepStmt1 = null;
93             try {
94                 conn = ds.getConnection();
95                 prepStmt1 = conn.prepareStatement(insertStatement);
96                 prepStmt1.setLong(1,localTID);
97                 prepStmt1.setString(2,Configuration.getServerName());
98                 prepStmt1.setBytes(3,data);
99                 prepStmt1 .executeUpdate();
100                 return true;
101             } catch (Throwable JavaDoc ex) {
102                 _logger.log(Level.SEVERE,"jts.exception_in_db_log_resource",ex);
103                 return false;
104             } finally {
105                 try {
106                 if (prepStmt1 != null)
107                     prepStmt1.close();
108                 if (conn != null)
109                     conn.close();
110                 } catch (Exception JavaDoc ex1) {
111                     _logger.log(Level.SEVERE,"jts.exception_in_db_log_resource",ex1);
112                 }
113             }
114         }
115         return false;
116     }
117
118     boolean deleteRecord(long localTID) {
119         if (ds != null) {
120             Connection conn = null;
121             PreparedStatement prepStmt1 = null;
122             try {
123          // To avoid compile time dependency to get NonTxConnection
124
conn = (Connection)(getNonTxConnectionMethod.invoke(ds, null));
125                 prepStmt1 = conn.prepareStatement(deleteStatement);
126                 prepStmt1.setLong(1,localTID);
127                 prepStmt1.setString(2,Configuration.getServerName());
128                 prepStmt1 .executeUpdate();
129                 return true;
130             } catch (Exception JavaDoc ex) {
131                 _logger.log(Level.SEVERE,"jts.exception_in_db_log_resource",ex);
132                 return false;
133             } finally {
134                 try {
135                 if (prepStmt1 != null)
136                     prepStmt1.close();
137                 if (conn != null)
138                     conn.close();
139                 } catch (Exception JavaDoc ex1) {
140                     _logger.log(Level.SEVERE,"jts.exception_in_db_log_resource",ex1);
141                 }
142             }
143         }
144         return false;
145     }
146
147     Map JavaDoc getGlobalTIDMap() {
148         Map JavaDoc gtidMap = new HashMap JavaDoc();
149         if (ds != null) {
150             Connection conn = null;
151             PreparedStatement prepStmt1 = null;
152             ResultSet rs = null;
153              Statement stmt = null;
154             try {
155                 conn = ds.getConnection();
156                 stmt = conn.createStatement();
157                 String JavaDoc selectStmt = " select * from txn_log_table where servername = '" + Configuration.getServerName() + "'";
158                 rs = stmt.executeQuery(selectStmt);
159                 while (rs.next()) {
160                     Long JavaDoc localTID = new Long JavaDoc(rs.getLong(1));
161                     byte[] gtridbytes = rs.getBytes(3);
162                     gtidMap.put(GlobalTID.fromTIDBytes(rs.getBytes(3)), localTID);
163                 }
164             } catch (Exception JavaDoc ex) {
165                 _logger.log(Level.SEVERE,"jts.exception_in_db_log_resource",ex);
166             } finally {
167                 try {
168                 if (rs != null)
169                     rs.close();
170                 if (stmt != null)
171                     stmt.close();
172                 if (prepStmt1 != null)
173                     prepStmt1.close();
174                 if (conn != null)
175                     conn.close();
176                 } catch (Exception JavaDoc ex1) {
177                     _logger.log(Level.SEVERE,"jts.exception_in_db_log_resource",ex1);
178                 }
179             }
180         }
181         return gtidMap;
182     }
183
184     
185 }
186
Popular Tags