Package com.icentris.util

Examples of com.icentris.util.CodeTimer


  }

  public static java.sql.Connection getConnection(String poolName)
    throws SQLException
  {
    CodeTimer timer = new CodeTimer();
    CodeTimerSegment segment = timer.start("ConnectionPool: getConnection");
    segment.setCanBeSubSegmentOnlyIfUnacceptable( true );
    ConnectionPool pool = (ConnectionPool) poolsByDb.get(poolName);
    if ( pool == null ) {
      throw new IllegalStateException("The ConnectionPool (" + poolName + ") has not been initialized yet");
    }
    //sendNotification( new Notification(NOTIF_OPENCONNECTION, NOTIF_OPENCONNECTION,
    //                  notificationSequence++, connection.toString()) );
    try {
      ConnectionWrapper connection = pool.prepareConnection();
      timer.stop("ConnectionPool: getConnection");
      // hopefully the common case, we're done!
      return connection;
    } catch (IndexOutOfBoundsException e) {}
    synchronized (pool) {
      boolean timeToCreate = false;
      do {
        // try again now that we're synchronized
        if ( pool.connections.size() > 0 ) {
          try {
            ConnectionWrapper connection = pool.prepareConnection();
            timer.stop("ConnectionPool: getConnection");
            return connection;
          } catch (IndexOutOfBoundsException e) {}
        } else if ( pool.connectionCount < pool.getMaxConnections() ) {
          timeToCreate = true;
        } else {
          // Too many connections?  Let's wait for some to free up.
               if (logger.isDebugEnabled()) {
                  logger
                           .debug("getConnection(String) - WARNING: [ConnectionPool] too many connections in use [" + pool.connectionsInUse.size() + "], waiting [" + Thread.currentThread() + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
               }
          try { pool.wait(); } catch (InterruptedException e) {}
          if ( pool.showDebugging() ) {
                  if (logger.isDebugEnabled()) {
                     logger
                              .debug("getConnection(String) - INFO: [ConnectionPool] freed up! [" + Thread.currentThread() + "] connections in use= [" + pool.connectionsInUse.size() + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                  }
          }
        }
      } while ( timeToCreate == false );
      // if we made it here, the pool is too small or there are no more
      // connections in the pool, time to initialize another
      pool.connectionCount++;
    }
    // Now we're unsynchronzied so others can get a lock on the pool to do their thing.
    // We should be safe to open a new connection because we already reserved it by
    // doing pool.connectionCount++.  But if we fail in getting the new connection, let's
    // make sure to pool.connectionCount-- so others can try.
    try {
      ConnectionWrapper connection = pool.openNewConnection();
      timer.stop("ConnectionPool: getConnection");
      return connection;
    } catch (SQLException e) {
      pool.connectionCount--;
      timer.stop("ConnectionPool: getConnection");
      throw e;
    }
  }
View Full Code Here


  public ResultSet executeQuery(String sql)
    throws SQLException
  {
    checkConnection("executeQuery");
    CodeTimer timer = null;
    try {
      if ( useCodeTimer == false ) {
        return new ResultSetWrapper( realStatement.executeQuery(sql), this );
      } else {
        timer = new CodeTimer();
        timer.setCallersToIgnore( callersToIgnore );
        Properties props = new Properties();
        props.setProperty("sqlQuery", sql);
        segment.setProperties( props );
        CodeTimerSegment subSegment = timer.start(callerDepth);
        subSegment.setLabel("SQL:executeQuery");
        subSegment.setSaveAggregateData( false );
        ResultSetWrapper results = new ResultSetWrapper( realStatement.executeQuery(sql), this );
        results.setCallersToIgnore( callersToIgnore );
        results.setUniqueString( "SQL:ResultSet.next() + getString():" + segment.getUniqueString().substring(4) );
        results.setUseCodeTimer( useCodeTimer );
        return results;
      }
    } catch (SQLException e) {
      setMostRecentError(e);
      // (I'm really sad I can't change the message without losing the stack
      // trace, but having the query helps figure out what went wrong)
      throw new SQLException("The statement:[" + sql + "] had the error: " + e.getMessage());
    } finally {
      if ( timer != null ) {
        timer.stop(callerDepth);
      }
    }
  }
View Full Code Here

  public ResultSet executeQuery()
    throws SQLException
  {
    checkConnection("executeQuery");
    String thisToString = this.toString();
    CodeTimer timer = null;
    try {
      if ( useCodeTimer() == false ) {
        return new ResultSetWrapper( realStatement.executeQuery(), this );
      } else {
        timer = new CodeTimer();
        timer.setCallersToIgnore( getCallersToIgnore() );
        Properties props = new Properties();
        // If the toString has a space, I'll assume it's the real sql.
        if ( thisToString.indexOf(" ") > -1 ) {
          props.setProperty("sqlQuery", thisToString);
        // Otherwise, I'll use my own information that's almost as good.
        } else {
          props.setProperty("originalSql", originalSql);
          props.setProperty("params", params.toString());
        }
        segment.setProperties( props );
        CodeTimerSegment subSegment = timer.start( getCallerDepth() );
        subSegment.setLabel("SQL:executeQuery()");
        subSegment.setSaveAggregateData( false );
        ResultSetWrapper results = new ResultSetWrapper( realStatement.executeQuery(), this );
        results.setUniqueString( "SQL:ResultSet.next() + getString():" + segment.getUniqueString().substring(4) );
        results.setUseCodeTimer( useCodeTimer() );
        return results;
      }
    } catch (SQLException e) {
      // If the toString doesn't have a space, I'll assume it's not the real sql.
      if ( thisToString.indexOf(" ") == -1 ) {
        thisToString = originalSql + ", params=" + params.toString();
      }
      // (I'm really sad I can't change the message without losing the stack
      // trace, but for now I don't see any other way)
      e = new SQLException("The statement:[" + thisToString + "] had the error: " + e.getMessage());
      setMostRecentError(e);
      throw e;
    } finally {
      if ( timer != null ) {
        timer.stop(getCallerDepth());
      }
    }
  }
View Full Code Here

TOP

Related Classes of com.icentris.util.CodeTimer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.