1 32 33 package com.knowgate.jdc; 34 35 import java.util.Map ; 36 import java.util.Date ; 37 38 import java.sql.SQLException ; 39 import java.sql.SQLWarning ; 40 import java.sql.DatabaseMetaData ; 41 import java.sql.Statement ; 42 import java.sql.PreparedStatement ; 43 import java.sql.CallableStatement ; 44 import java.sql.ResultSet ; 45 import java.sql.Connection ; 46 import java.sql.Savepoint ; 47 48 import com.knowgate.debug.DebugFile; 49 50 55 public final class JDCConnection implements Connection { 56 57 public static final short IdClass = 100; 58 59 public static final int DBMS_GENERIC = 0; 60 public static final int DBMS_MYSQL = 1; 61 public static final int DBMS_POSTGRESQL = 2; 62 public static final int DBMS_MSSQL = 3; 63 public static final int DBMS_ORACLE = 5; 64 65 private static final int DBMS_UNKNOWN = -1; 66 private static final int DBMS_SYBASE = 4; 67 private static final int DBMS_B2 = 6; 68 private static final int DBMS_INFORMIX = 7; 69 private static final int DBMS_CLOUDSCAPE = 8; 70 71 private JDCConnectionPool pool; 72 private Connection conn; 73 private boolean inuse; 74 private long timestamp; 75 private int dbms; 76 private String name; 77 private String schema; 78 79 private static final String DBMSNAME_MSSQL = "Microsoft SQL Server"; 80 private static final String DBMSNAME_POSTGRESQL = "PostgreSQL"; 81 private static final String DBMSNAME_ORACLE = "Oracle"; 82 private static final String DBMSNAME_MYSQL = "MySQL"; 83 84 public JDCConnection(Connection conn, JDCConnectionPool pool, String schemaname) { 85 this.dbms = DBMS_UNKNOWN; 86 this.conn=conn; 87 this.pool=pool; 88 this.inuse=false; 89 this.timestamp=0; 90 this.name = null; 91 this.schema=schemaname; 92 } 93 94 public JDCConnection(Connection conn, JDCConnectionPool pool) { 95 this.dbms = DBMS_UNKNOWN; 96 this.conn=conn; 97 this.pool=pool; 98 this.inuse=false; 99 this.timestamp=0; 100 this.name = null; 101 this.schema=null; 102 } 103 104 public boolean lease(String sConnectionName) { 105 if (inuse) { 106 return false; 107 } else { 108 inuse=true; 109 name = sConnectionName; 110 timestamp=System.currentTimeMillis(); 111 return true; 112 } 113 } 114 115 public boolean validate() { 116 boolean bValid; 117 118 if (DebugFile.trace) { 119 DebugFile.writeln("Begin JDCConnection.validate()"); 120 DebugFile.incIdent(); 121 } 122 try { 123 conn.getMetaData(); 124 bValid = true; 125 } catch (Exception e) { 126 DebugFile.writeln(new Date ().toString() + " " + e.getMessage()); 127 bValid = false; 128 } 129 130 if (DebugFile.trace) { 131 DebugFile.decIdent(); 132 DebugFile.writeln("End JDCConnection.validate()"); 133 } 134 135 return bValid; 136 } 137 138 public boolean inUse() { 139 return inuse; 140 } 141 142 public JDCConnectionPool getPool() { 143 return pool; 144 } 145 146 public long getLastUse() { 147 return timestamp; 148 } 149 150 public String getName() { 151 return name; 152 } 153 154 public int getDataBaseProduct() throws SQLException { 155 DatabaseMetaData mdat; 156 String prod; 157 158 if (DBMS_UNKNOWN==dbms) { 159 try { 160 mdat = conn.getMetaData(); 161 prod = mdat.getDatabaseProductName(); 162 163 if (prod.equals(DBMSNAME_MSSQL)) 164 dbms = DBMS_MSSQL; 165 else if (prod.equals(DBMSNAME_POSTGRESQL)) 166 dbms = DBMS_POSTGRESQL; 167 else if (prod.equals(DBMSNAME_ORACLE)) 168 dbms = DBMS_ORACLE; 169 else if (prod.equals(DBMSNAME_MYSQL)) 170 dbms = DBMS_MYSQL; 171 else 172 dbms = DBMS_GENERIC; 173 } 174 catch (NullPointerException npe) { 175 if (DebugFile.trace) DebugFile.writeln("NullPointerException at JDCConnection.getDataBaseProduct()"); 176 dbms = DBMS_GENERIC; 177 } 178 } 179 return dbms; 180 } 181 182 public String getSchemaName() throws SQLException { 183 String sname; 184 185 if (null==schema) { 186 DatabaseMetaData mdat = conn.getMetaData(); 187 ResultSet rset = mdat.getSchemas(); 188 189 if (rset.next()) 190 sname = rset.getString(1); 191 else 192 sname = null; 193 194 rset.close(); 195 } 196 else 197 sname = schema; 198 199 return sname; 200 } 201 202 public void setSchemaName(String sname) { 203 schema = sname; 204 } 205 206 public void close() throws SQLException { 207 if (DebugFile.trace) { 208 DebugFile.writeln("Begin JDCConnection.close()"); 209 DebugFile.incIdent(); 210 } 211 212 if (pool==null) { 213 inuse = false; 214 name = null; 215 conn.close(); 216 } 217 else { 218 pool.returnConnection(this); 219 } 220 221 if (DebugFile.trace) { 222 DebugFile.decIdent(); 223 DebugFile.writeln("End JDCConnection.close()"); 224 } 225 } 226 227 public void close(String sCaller) throws SQLException { 228 if (DebugFile.trace) { 229 DebugFile.writeln("Begin JDCConnection.close("+sCaller+")"); 230 DebugFile.incIdent(); 231 } 232 if (pool==null) { 233 inuse = false; 234 name = null; 235 conn.close(); 236 } 237 else { 238 pool.returnConnection(this, sCaller); 239 } 240 241 if (DebugFile.trace) { 242 DebugFile.decIdent(); 243 DebugFile.writeln("End JDCConnection.close("+sCaller+")"); 244 } 245 } 246 247 protected void expireLease() { 248 inuse=false; 249 name =null ; 250 } 251 252 protected Connection getConnection() { 253 return conn; 254 } 255 256 public Statement createStatement(int i, int j) throws SQLException { 257 return conn.createStatement(i,j); 258 } 259 260 public Statement createStatement(int i, int j, int k) throws SQLException { 261 return conn.createStatement(i,j,k); 262 } 263 264 public PreparedStatement prepareStatement(String sql) throws SQLException { 265 return conn.prepareStatement(sql); 266 } 267 268 public PreparedStatement prepareStatement(String sql, String [] params) throws SQLException { 269 return conn.prepareStatement(sql,params); 270 } 271 272 public PreparedStatement prepareStatement(String sql, int i) throws SQLException { 273 return conn.prepareStatement(sql,i); 274 } 275 276 public PreparedStatement prepareStatement(String sql, int i, int j) throws SQLException { 277 return conn.prepareStatement(sql,i,j); 278 } 279 280 public PreparedStatement prepareStatement(String sql, int i, int j, int k) throws SQLException { 281 return conn.prepareStatement(sql,i,j,k); 282 } 283 284 public PreparedStatement prepareStatement(String sql, int[] params) throws SQLException { 285 return conn.prepareStatement(sql,params); 286 } 287 288 public CallableStatement prepareCall(String sql) throws SQLException { 289 return conn.prepareCall(sql); 290 } 291 292 public CallableStatement prepareCall(String sql, int i, int j) throws SQLException { 293 return conn.prepareCall(sql, i , j); 294 } 295 296 public CallableStatement prepareCall(String sql, int i, int j, int k) throws SQLException { 297 return conn.prepareCall(sql, i , j, k); 298 } 299 300 public Statement createStatement() throws SQLException { 301 return conn.createStatement(); 302 } 303 304 public String nativeSQL(String sql) throws SQLException { 305 return conn.nativeSQL(sql); 306 } 307 308 public void setAutoCommit(boolean autoCommit) throws SQLException { 309 conn.setAutoCommit(autoCommit); 310 } 311 312 public boolean getAutoCommit() throws SQLException { 313 return conn.getAutoCommit(); 314 } 315 316 public int getHoldability() throws SQLException { 317 return conn.getHoldability(); 318 } 319 320 public void setHoldability(int h) throws SQLException { 321 conn.setHoldability(h); 322 } 323 324 public Savepoint setSavepoint() throws SQLException { 325 return conn.setSavepoint(); 326 } 327 328 public Savepoint setSavepoint(String s) throws SQLException { 329 return conn.setSavepoint(s); 330 } 331 332 public void commit() throws SQLException { 333 conn.commit(); 334 } 335 336 public void rollback() throws SQLException { 337 conn.rollback(); 338 } 339 340 public void rollback(Savepoint p) throws SQLException { 341 conn.rollback(p); 342 } 343 344 public boolean isClosed() throws SQLException { 345 return conn.isClosed(); 346 } 347 348 public DatabaseMetaData getMetaData() throws SQLException { 349 return conn.getMetaData(); 350 } 351 352 public void setReadOnly(boolean readOnly) throws SQLException { 353 conn.setReadOnly(readOnly); 354 } 355 356 public boolean isReadOnly() throws SQLException { 357 return conn.isReadOnly(); 358 } 359 360 public void setCatalog(String catalog) throws SQLException { 361 conn.setCatalog(catalog); 362 } 363 364 public String getCatalog() throws SQLException { 365 return conn.getCatalog(); 366 } 367 368 public void setTransactionIsolation(int level) throws SQLException { 369 conn.setTransactionIsolation(level); 370 } 371 372 public int getTransactionIsolation() throws SQLException { 373 return conn.getTransactionIsolation(); 374 } 375 376 public Map getTypeMap() throws SQLException { 377 return conn.getTypeMap(); 378 } 379 380 public void setTypeMap(Map typemap) throws SQLException { 381 conn.setTypeMap(typemap); 382 } 383 384 public SQLWarning getWarnings() throws SQLException { 385 return conn.getWarnings(); 386 } 387 388 public void clearWarnings() throws SQLException { 389 conn.clearWarnings(); 390 } 391 392 public void releaseSavepoint(Savepoint p) throws SQLException { 393 conn.releaseSavepoint(p); 394 } 395 396 420 421 public boolean exists(String sObjectName, String sObjectType) 422 throws SQLException , UnsupportedOperationException { 423 boolean bRetVal; 424 PreparedStatement oStmt; 425 ResultSet oRSet; 426 427 if (DebugFile.trace) { 428 DebugFile.writeln("Begin JDCConnection.exists([Connection], " + sObjectName + ", " + sObjectType + ")"); 429 DebugFile.incIdent(); 430 } 431 432 switch (this.getDataBaseProduct()) { 433 434 case JDCConnection.DBMS_MSSQL: 435 if (DebugFile.trace) 436 DebugFile.writeln ("Connection.prepareStatement(SELECT id FROM sysobjects WHERE name='" + sObjectName + "' AND xtype='" + sObjectType + "' OPTION (FAST 1))"); 437 438 oStmt = this.prepareStatement("SELECT id FROM sysobjects WHERE name=? AND xtype=? OPTION (FAST 1)", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 439 oStmt.setString(1, sObjectName); 440 oStmt.setString(2, sObjectType); 441 oRSet = oStmt.executeQuery(); 442 bRetVal = oRSet.next(); 443 oRSet.close(); 444 oStmt.close(); 445 break; 446 447 case JDCConnection.DBMS_POSTGRESQL: 448 if (DebugFile.trace) 449 DebugFile.writeln ("Conenction.prepareStatement(SELECT relname FROM pg_class WHERE relname='" + sObjectName + "')"); 450 451 oStmt = this.prepareStatement("SELECT tablename FROM pg_tables WHERE tablename=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 452 oStmt.setString(1, sObjectName); 453 oRSet = oStmt.executeQuery(); 454 bRetVal = oRSet.next(); 455 oRSet.close(); 456 oStmt.close(); 457 break; 458 459 case JDCConnection.DBMS_ORACLE: 460 if (DebugFile.trace) 461 DebugFile.writeln ("Conenction.prepareStatement(SELECT TABLE_NAME FROM USER_TABLES WHERE TABLE_NAME='" + sObjectName + "')"); 462 463 oStmt = this.prepareStatement("SELECT TABLE_NAME FROM USER_TABLES WHERE TABLE_NAME=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 464 oStmt.setString(1, sObjectName.toUpperCase()); 465 oRSet = oStmt.executeQuery(); 466 bRetVal = oRSet.next(); 467 oRSet.close(); 468 oStmt.close(); 469 break; 470 471 case JDCConnection.DBMS_MYSQL: 472 if (DebugFile.trace) 473 DebugFile.writeln ("Conenction.prepareStatement(SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_name='"+sObjectName+"')"); 474 475 oStmt = this.prepareStatement("SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_name=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 476 oStmt.setString(1, sObjectName); 477 oRSet = oStmt.executeQuery(); 478 bRetVal = oRSet.next(); 479 bRetVal = false; 480 oRSet.close(); 481 oStmt.close(); 482 break; 483 484 default: 485 throw new UnsupportedOperationException ("Unsupported DBMS"); 486 } 488 if (DebugFile.trace) { 489 DebugFile.decIdent(); 490 DebugFile.writeln("End JDCConnection.exists() : " + String.valueOf(bRetVal)); 491 } 492 493 return bRetVal; 494 } 496 503 public String pid() throws SQLException { 504 Statement oStmt; 505 ResultSet oRSet; 506 String sPId = null; 507 switch (getDataBaseProduct()) { 508 case DBMS_POSTGRESQL: 509 oStmt = createStatement(); 510 oRSet = oStmt.executeQuery("SELECT pg_backend_pid()"); 511 if (oRSet.next()) 512 sPId = String.valueOf(oRSet.getInt(1)); 513 oRSet.close(); 514 oStmt.close(); 515 break; 516 case DBMS_ORACLE: 517 oStmt = createStatement(); 518 oRSet = oStmt.executeQuery("SELECT SYS_CONTEXT('USERENV','SESIONID') FROM DUAL"); 519 if (oRSet.next()) 520 sPId = oRSet.getString(1); 521 oRSet.close(); 522 oStmt.close(); 523 break; 524 } 525 return sPId; 526 } } 528
| Popular Tags
|