1 5 package com.teamkonzept.db; 6 7 import java.sql.*; 8 import java.util.Enumeration ; 9 import java.util.Stack ; 10 import java.util.Properties ; 11 12 import com.teamkonzept.lib.*; 13 import org.apache.log4j.Category; 14 15 36 public class TKDBConnectionManager 37 { 38 private static final Category CAT = Category.getInstance(TKDBConnectionManager.class); 39 40 43 45 48 private int maxConnections = 0; 49 50 54 private String dbIdentifier = null; 55 56 59 private TKConnectData jdbcConnectData; 60 61 66 private TKHashtable tkConnectionHash = new TKHashtable(); 67 68 73 private TKHashtable queryClasses = new TKHashtable(); 74 75 78 private Stack tkConnectionStack = new Stack (); 79 80 83 private TKLimiter limiter = new TKLimiter(); 84 85 91 private TKVector connectionsToClose = new TKVector(); 92 93 96 101 public Connection getConnection() throws SQLException{ 102 return getTKDBConnection().getConnection(); 103 } 104 105 110 public TKConnectData getConnectionData(){ 111 return jdbcConnectData; 112 } 113 114 125 public TKDBConnection getTKDBConnection() throws SQLException 126 { 127 try 128 { 129 Thread currentThread = Thread.currentThread(); 130 TKDBConnection tkConn = (TKDBConnection) tkConnectionHash.get(currentThread); 131 132 if (CAT.isDebugEnabled()) { 134 CAT.debug("Current Thread: [" + Thread.currentThread() + "]"); CAT.debug("Limiter: (oid=" + limiter + 136 ", used=" + limiter.used + 137 ", pending=" + limiter.pending + 138 ", limit=" + limiter.limit + 139 ") !=? CACHE SIZE: " + tkConnectionHash.size()); Enumeration en = tkConnectionHash.keys(); 141 int cnt = 0; 142 while (en.hasMoreElements()) { 143 CAT.debug("|<" + cnt + "> " + en.nextElement() + " |"); cnt++; 145 } 146 } 147 if (tkConn == null) { 149 limiter.take(); 150 } 151 synchronized (this) { 152 if( tkConn == null || tkConn.getConnection().isClosed() ) { 153 if( !tkConnectionStack.empty() ) { 154 try { 155 tkConn = (TKDBConnection) tkConnectionStack.pop(); 156 tkConnectionHash.put( currentThread, tkConn); 157 } 158 catch( Throwable t) { 159 CAT.error(t); 160 } 161 } 162 else { 163 CAT.debug("stack empty"); 164 } 165 if( tkConn == null || tkConn.getConnection().isClosed() ) { 166 if (CAT.isDebugEnabled()) { 167 CAT.debug("open new connection now ..."); 168 CAT.debug("using connectData:" + jdbcConnectData); 169 } 170 Connection conn 171 = DriverManager.getConnection(jdbcConnectData.getConnectString(), 172 jdbcConnectData.getConnectProperties()); 173 tkConn = new TKDBConnection(conn, this); 174 tkConnectionHash.put( currentThread, tkConn ); 175 } 176 } 177 else { 178 CAT.debug("reuse connection"); 179 } 180 } 181 return tkConn; 182 } 183 catch (SQLException e) 184 { 185 CAT.error("Error during connection creation : ", e); 186 throw e; 187 } 188 } 189 190 199 public TKQuery newQuery(Class inQueryClass) throws SQLException 200 { 201 TKDBConnection tkConn = getTKDBConnection(); TKQuery query; 203 Class queryClass; 204 String queryId = TKLib.getClassName(inQueryClass); 205 String basePackage = TKLib.getPackageName (inQueryClass) ; 207 208 synchronized (this) { query = getInitializedPrepQuery(queryId, tkConn); if(query != null) { 211 return query; 212 } 213 queryClass = (Class ) queryClasses.get( queryId ); 214 if( queryClass == null ) { 215 if( dbIdentifier != null && dbIdentifier.length() > 0 ) { 216 try { 217 String classToLoad = ""; 218 if(basePackage==null) { 219 classToLoad = dbIdentifier + '.' + queryId; 220 }else{ 221 classToLoad = basePackage + '.' + dbIdentifier + '.' + queryId; 222 } 223 queryClass = Class.forName(classToLoad); 224 } 225 catch(ClassNotFoundException e) { 226 CAT.debug(e); 227 } 228 } 229 230 if( queryClass == null ) { 231 queryClass = inQueryClass; 232 } 233 queryClasses.put(queryId, queryClass); 234 } 235 } 236 237 try { 238 if (query == null) { 239 query = (TKQuery) queryClass.newInstance(); 240 } 241 query.initQuery(jdbcConnectData.getTypeConverter(), tkConn, queryId); 242 243 return query; 244 } 245 catch(Throwable t) { 246 CAT.error("closeConnection", t); 247 throw new InstantiationError (t.getMessage()); 248 } 249 } 250 251 257 258 264 public synchronized void setConnectionData( TKConnectData data ) 265 { 266 CAT.debug("jdbcConnectData set in setConnectionData: with: " + data); 267 jdbcConnectData = data; 268 } 269 270 274 public synchronized void prepareConnection( Properties prop ) 275 throws SQLException 276 { 277 jdbcConnectData = null; 278 279 dbIdentifier = prop.getProperty("database").toLowerCase(); 280 CAT.debug("got DB Identifier "+ dbIdentifier); 281 if( dbIdentifier.startsWith( "mssql" ) ) { 282 jdbcConnectData = new TKMSSQLConnectData( prop ); 283 } 284 else if( dbIdentifier.startsWith( "sybase" ) ) { 285 jdbcConnectData = new TKSybaseConnectData( prop ); 286 } 287 else if( dbIdentifier.startsWith( "oracle" ) ) { 288 jdbcConnectData = new TKOracleConnectData( prop ); 289 } 290 else if (dbIdentifier.startsWith( "postgresql" ) ) { 291 jdbcConnectData = new TKPostgreSQLConnectData( prop ); 292 } 293 if ( jdbcConnectData != null ) 294 jdbcConnectData.initTypeConverter( getConnection() ); 295 296 } 297 298 299 public synchronized void closeConnection() throws SQLException{ 300 Thread currentThread = Thread.currentThread(); 301 TKDBConnection tkConn = (TKDBConnection) tkConnectionHash.remove( currentThread ); 302 if (tkConn != null) { 303 limiter.free(); 304 closeConnection(tkConn); 305 } 306 else{ 307 CAT.warn(" closeConnection: no Connection found! "); 308 } 309 } 310 311 public synchronized void closeConnection(TKDBConnection tkConn) 312 throws SQLException 313 { 314 limiter.free(); 315 try{ 316 tkConn.closeNonsensitiveQueries(false); 317 } 318 finally{ Connection conn = tkConn.getConnection(); 320 if (!conn.isClosed()) { 321 conn.close(); 322 323 324 } 325 } 326 } 327 328 329 public synchronized void freeConnection() 330 throws SQLException 331 { 332 Thread currentThread = Thread.currentThread(); 333 334 TKDBConnection tkConn = (TKDBConnection) tkConnectionHash.remove( currentThread ); 335 if (tkConn != null) { 336 try{ 337 tkConn.closeNonsensitiveQueries(false); 338 } 339 catch(SQLException e){ 340 closeConnection(tkConn); 341 } 342 limiter.free(); 343 Connection conn = tkConn.getConnection(); 344 if( connectionsToClose.removeElement( conn ) ) { 345 conn.close(); 347 348 } 349 if (!conn.isClosed()) { 350 tkConnectionStack.push( tkConn ); 351 } 352 } 353 } 354 355 356 public synchronized void resetConnections() 357 { 358 maxConnections = 0; 359 dbIdentifier = null; 360 jdbcConnectData = null; 361 362 Enumeration e = tkConnectionHash.elements(); 364 while( e.hasMoreElements() ) { 365 connectionsToClose.addElement( ((TKDBConnection) e.nextElement()).getConnection() ); 366 } 367 368 queryClasses = new TKHashtable(); 370 371 while( !tkConnectionStack.empty() ) { 373 Connection conn = ((TKDBConnection)tkConnectionStack.pop()).getConnection(); 374 try { 375 conn.close(); 376 377 } 378 catch( SQLException sqle ) { 379 throw new TKSQLError(sqle.getMessage(), sqle); 380 } 381 } 382 } 383 384 public synchronized void limitConnections( int count ) 385 { 386 limiter.newLimit( count ); 387 } 388 389 390 391 public void beginTransaction() throws SQLException 392 { 393 getTKDBConnection().beginTransaction(); 394 } 395 396 public void commitTransaction() throws SQLException 397 { 398 399 getTKDBConnection().commitTransaction(); 400 } 401 402 403 404 public void rollbackTransaction() throws SQLException 405 { 406 getTKDBConnection().rollbackTransaction(); 407 } 408 409 410 public void closeNonsensitiveQueries() throws SQLException 411 { getTKDBConnection().closeNonsensitiveQueries();} 412 413 414 415 416 417 418 422 private TKPrepQuery getInitializedPrepQuery(Object queryID, TKDBConnection tkConn){ 423 TKHashtable queries = (TKHashtable) tkConn.getPrepQueries(); 424 TKPrepQuery query = (TKPrepQuery) queries.remove(queryID); 425 return query; 426 } 427 428 432 443 446 447 448 private static TKDBConnectionManager singleton = null; 449 public static TKDBConnectionManager getInstance() { 450 if (singleton == null) 451 singleton = new TKDBConnectionManager(); 452 return singleton; 453 } 454 455 private TKDBConnectionManager() { 456 } 457 } 458 459
| Popular Tags
|