1 21 22 package org.apache.derby.impl.drda; 23 24 import java.sql.Connection ; 25 import java.sql.Driver ; 26 import java.sql.PreparedStatement ; 27 import java.sql.Statement ; 28 import java.sql.ResultSet ; 29 import java.sql.SQLException ; 30 import java.util.Hashtable ; 31 import java.util.Enumeration ; 32 import java.util.Properties ; 33 34 import org.apache.derby.iapi.jdbc.EngineConnection; 35 import org.apache.derby.iapi.reference.Attribute; 36 import org.apache.derby.iapi.tools.i18n.LocalizedResource; 37 import org.apache.derby.iapi.services.sanity.SanityManager; 38 39 43 class Database 44 { 45 46 protected String dbName; protected String shortDbName; String attrString=""; protected int securityMechanism; protected String userId; protected String password; protected String decryptedUserId; protected String decryptedPassword; protected byte[] passwordSubstitute; protected boolean rdbAllowUpdates = true; protected int accessCount; protected byte[] secTokenIn; protected byte[] secTokenOut; protected byte[] crrtkn; protected String typDefNam; protected int byteOrder; protected int ccsidSBC; protected int ccsidDBC; protected int ccsidMBC; protected String ccsidSBCEncoding; protected String ccsidDBCEncoding; protected String ccsidMBCEncoding; protected boolean RDBUPDRM_sent = false; protected boolean sendTRGDFTRT = false; 74 77 private EngineConnection conn; 78 DRDAStatement defaultStatement; private DRDAStatement currentStatement; private Hashtable stmtTable; 83 boolean forXA = false; 84 85 91 Database (String dbName) 92 { 93 if (dbName != null) 94 { 95 int attrOffset = dbName.indexOf(';'); 96 if (attrOffset != -1) 97 { 98 this.attrString = dbName.substring(attrOffset,dbName.length()); 99 this.shortDbName = dbName.substring(0,attrOffset); 100 } 101 else 102 this.shortDbName = dbName; 103 } 104 105 this.dbName = dbName; 106 this.stmtTable = new Hashtable (); 107 initializeDefaultStatement(); 108 } 109 110 111 private void initializeDefaultStatement() 112 { 113 this.defaultStatement = new DRDAStatement(this); 114 } 115 116 122 final void setConnection(EngineConnection conn) 123 throws SQLException 124 { 125 this.conn = conn; 126 if(conn != null) 127 defaultStatement.setStatement(conn); 128 } 129 134 final EngineConnection getConnection() 135 { 136 return conn; 137 } 138 144 protected DRDAStatement getCurrentStatement() 145 { 146 return currentStatement; 147 } 148 153 protected DRDAStatement getDefaultStatement() 154 { 155 currentStatement = defaultStatement; 156 return defaultStatement; 157 } 158 159 166 protected DRDAStatement getDefaultStatement(Pkgnamcsn pkgnamcsn) 167 { 168 currentStatement = defaultStatement; 169 currentStatement.setPkgnamcsn(pkgnamcsn); 170 return currentStatement; 171 } 172 173 186 protected DRDAStatement newDRDAStatement(Pkgnamcsn pkgnamcsn) 187 throws SQLException 188 { 189 DRDAStatement stmt = getDRDAStatement(pkgnamcsn); 190 if (stmt != null) { 191 stmt.close(); 192 stmt.reset(); 193 } 194 else 195 { 196 stmt = new DRDAStatement(this); 197 stmt.setPkgnamcsn(pkgnamcsn); 198 storeStatement(stmt); 199 } 200 return stmt; 201 } 202 203 209 protected DRDAStatement getDRDAStatement(Pkgnamcsn pkgnamcsn) { 210 DRDAStatement newStmt = 211 (DRDAStatement) stmtTable.get(pkgnamcsn.getStatementKey()); 212 if (newStmt != null) { 213 currentStatement = newStmt; 214 currentStatement.setCurrentDrdaResultSet(pkgnamcsn); 215 } 216 return newStmt; 217 } 218 219 224 void makeConnection(Properties p) throws SQLException 225 { 226 p.put(Attribute.USERNAME_ATTR, userId); 227 228 if (password != null) 230 p.put(Attribute.PASSWORD_ATTR, password); 231 232 EngineConnection conn = (EngineConnection) 235 NetworkServerControlImpl.getDriver().connect(Attribute.PROTOCOL 236 + shortDbName + attrString, p); 237 if (conn != null) { 238 conn.setAutoCommit(false); 239 } 240 setConnection(conn); 241 } 242 243 252 void makeDummyConnection() 253 { 254 try { 255 EngineConnection conn = (EngineConnection) 258 NetworkServerControlImpl.getDriver().connect(Attribute.PROTOCOL 259 + shortDbName + attrString, new Properties ()); 260 261 if (conn != null) { 263 conn.close(); 264 } 265 } catch (SQLException se) {} } 267 268 String appendAttrString(Properties p) 270 { 271 if (p == null) 272 return null; 273 274 Enumeration pKeys = p.propertyNames(); 275 while (pKeys.hasMoreElements()) 276 { 277 String key = (String ) pKeys.nextElement(); 278 attrString +=";" + key + "=" + p.getProperty(key); 279 } 280 281 return attrString; 282 } 283 284 288 protected void storeStatement(DRDAStatement stmt) throws SQLException 289 { 290 stmtTable.put(stmt.getPkgnamcsn().getStatementKey(), stmt); 291 } 292 293 protected void removeStatement(DRDAStatement stmt) throws SQLException 294 { 295 stmtTable.remove(stmt.getPkgnamcsn().getStatementKey()); 296 stmt.close(); 297 } 298 299 304 305 protected void setCurrentStatement(DRDAStatement stmt) 306 { 307 currentStatement = stmt; 308 } 309 310 311 protected void commit() throws SQLException 312 { 313 314 if (conn != null) 315 conn.commit(); 316 } 317 318 protected void rollback() throws SQLException 319 { 320 321 if (conn != null) 322 conn.rollback(); 323 } 324 328 protected void close() throws SQLException 329 { 330 331 try { 332 if (stmtTable != null) 333 { 334 for (Enumeration e = stmtTable.elements() ; e.hasMoreElements() ;) 335 { 336 ((DRDAStatement) e.nextElement()).close(); 337 } 338 339 } 340 if (defaultStatement != null) 341 defaultStatement.close(); 342 if ((conn != null) && !conn.isClosed()) 343 { 344 if (! forXA) 345 { 346 conn.rollback(); 347 } 348 conn.close(); 349 } 350 } 351 finally { 352 conn = null; 353 currentStatement = null; 354 defaultStatement = null; 355 stmtTable=null; 356 } 357 } 358 359 final void setDrdaID(String drdaID) 360 { 361 if (conn != null) 362 conn.setDrdaID(drdaID); 363 } 364 365 374 final void setPrepareIsolation(int level) throws SQLException 375 { 376 conn.setPrepareIsolation(level); 377 } 378 379 final int getPrepareIsolation() throws SQLException 380 { 381 return conn.getPrepareIsolation(); 382 } 383 384 protected String buildRuntimeInfo(String indent, LocalizedResource localLangUtil) 385 { 386 387 String s = indent + 388 localLangUtil.getTextMessage("DRDA_RuntimeInfoDatabase.I") + 389 dbName + "\n" + 390 localLangUtil.getTextMessage("DRDA_RuntimeInfoUser.I") + 391 userId + "\n" + 392 localLangUtil.getTextMessage("DRDA_RuntimeInfoNumStatements.I") + 393 stmtTable.size() + "\n"; 394 s += localLangUtil.getTextMessage("DRDA_RuntimeInfoPreparedStatementHeader.I"); 395 for (Enumeration e = stmtTable.elements() ; e.hasMoreElements() ;) 396 { 397 s += ((DRDAStatement) e.nextElement()).toDebugString(indent 398 +"\t") +"\n"; 399 } 400 return s; 401 } 402 403 413 public void reset() 414 { 415 decryptedUserId = null; 419 decryptedPassword = null; 420 passwordSubstitute = null; 421 secTokenIn = null; 422 secTokenOut = null; 423 userId = null; 424 password = null; 425 securityMechanism = 0; 426 } 427 } 428 | Popular Tags |