1 23 24 28 29 package com.sun.jts.CosTransactions; 30 31 import java.util.Map ; 32 import java.util.HashMap ; 33 import java.sql.*; 34 import javax.sql.*; 35 import javax.naming.*; 36 import java.lang.reflect.Method ; 37 38 import java.util.logging.Logger ; 39 import java.util.logging.Level ; 40 import com.sun.logging.LogDomains; 41 import com.sun.jts.utils.LogFormatter; 42 43 47 48 class LogDBHelper { 49 50 String resName = "jdbc/TxnDS"; 51 DataSource ds = null; 52 Method getNonTxConnectionMethod = null; 53 static final String insertStatement = 55 System.getProperty("com.sun.jts.dblogging.insertquery", 56 "insert into txn_log_table values ( ? , ? , ? )"); 57 static final String deleteStatement = 58 System.getProperty("com.sun.jts.dblogging.deletequery", 59 "delete from txn_log_table where localtid = ? and servername = ? "); 60 static final String selectStatement = 61 System.getProperty("com.sun.jts.dblogging.selectquery", 62 "select * from txn_log_table where servername = ? "); 63 static Logger _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); 78 Class cls = ds.getClass(); 79 getNonTxConnectionMethod = cls.getMethod("getNonTxConnection", null); 80 81 } catch (Throwable 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 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 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 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 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 ex1) { 140 _logger.log(Level.SEVERE,"jts.exception_in_db_log_resource",ex1); 141 } 142 } 143 } 144 return false; 145 } 146 147 Map getGlobalTIDMap() { 148 Map gtidMap = new HashMap (); 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 selectStmt = " select * from txn_log_table where servername = '" + Configuration.getServerName() + "'"; 158 rs = stmt.executeQuery(selectStmt); 159 while (rs.next()) { 160 Long localTID = new Long (rs.getLong(1)); 161 byte[] gtridbytes = rs.getBytes(3); 162 gtidMap.put(GlobalTID.fromTIDBytes(rs.getBytes(3)), localTID); 163 } 164 } catch (Exception 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 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 |