Пример #1
0
  public static void main(String[] argv) {
    try {
      ComboPooledDataSource cpds = new ComboPooledDataSource();
      cpds.setJdbcUrl(argv[0]);
      cpds.setUser(argv[1]);
      cpds.setPassword(argv[2]);
      cpds.setMinPoolSize(5);
      cpds.setAcquireIncrement(5);
      cpds.setMaxPoolSize(20);

      System.err.println("Initial...");
      display(cpds);
      Thread.sleep(2000);

      HashSet hs = new HashSet();
      for (int i = 0; i < 20; ++i) {
        Connection c = cpds.getConnection();
        hs.add(c);
        System.err.println("Adding (" + (i + 1) + ") " + c);
        display(cpds);
        Thread.sleep(1000);

        // 			if (i == 9)
        // 			    {
        //  				//System.err.println("hardReset()ing");
        //  				//cpds.hardReset();
        // 				System.err.println("softReset()ing");
        // 				cpds.softReset();
        // 			    }
      }

      int count = 0;
      for (Iterator ii = hs.iterator(); ii.hasNext(); ) {
        Connection c = ((Connection) ii.next());
        System.err.println("Removing " + ++count);
        ii.remove();
        try {
          c.getMetaData().getTables(null, null, "PROBABLYNOT", new String[] {"TABLE"});
        } catch (Exception e) {
          System.err.println(e);
          System.err.println();
          continue;
        } finally {
          c.close();
        }
        Thread.sleep(2000);
        display(cpds);
      }

      System.err.println(
          "Closing data source, \"forcing\" garbage collection, and sleeping for 5 seconds...");
      cpds.close();
      System.gc();
      System.err.println("Main Thread: Sleeping for five seconds!");
      Thread.sleep(5000);
      // 		System.gc();
      // 		Thread.sleep(5000);
      System.err.println("Bye!");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Пример #2
0
  // TODO: factor out repetitive debugging code
  private synchronized void close(boolean known_invalid) throws SQLException {
    // System.err.println("Closing " + this);
    if (physicalConnection != null) {
      try {
        StringBuffer debugOnlyLog = null;
        if (Debug.DEBUG && known_invalid) {
          debugOnlyLog = new StringBuffer();
          debugOnlyLog.append("[ exceptions: ");
        }

        Exception exc = cleanupUncachedActiveStatements();
        if (Debug.DEBUG && exc != null) {
          if (known_invalid) debugOnlyLog.append(exc.toString() + ' ');
          else
            logger.log(
                MLevel.WARNING,
                "An exception occurred while cleaning up uncached active Statements.",
                exc);
          // exc.printStackTrace();
        }

        try {
          // we've got to use silentClose() rather than close() here,
          // 'cuz if there's still an exposedProxy (say a user forgot to
          // close his Connection) before we close, and we use regular (loud)
          // close, we will try to check this dead or dying PooledConnection
          // back into the pool. We only want to do this when close is called
          // on user proxies, and the underlying PooledConnection might still
          // be good. The PooledConnection itself should only be closed by the
          // pool.
          if (exposedProxy != null) exposedProxy.silentClose(known_invalid);
        } catch (Exception e) {
          if (Debug.DEBUG) {
            if (known_invalid) debugOnlyLog.append(e.toString() + ' ');
            else logger.log(MLevel.WARNING, "An exception occurred.", exc);
            // e.printStackTrace();
          }
          exc = e;
        }
        try {
          this.closeAll();
        } catch (Exception e) {
          if (Debug.DEBUG) {
            if (known_invalid) debugOnlyLog.append(e.toString() + ' ');
            else logger.log(MLevel.WARNING, "An exception occurred.", exc);
            // e.printStackTrace();
          }
          exc = e;
        }

        try {
          physicalConnection.close();
        } catch (Exception e) {
          if (Debug.DEBUG) {
            if (known_invalid) debugOnlyLog.append(e.toString() + ' ');
            else logger.log(MLevel.WARNING, "An exception occurred.", exc);
            e.printStackTrace();
          }
          exc = e;
        }

        if (exc != null) {
          if (known_invalid) {
            debugOnlyLog.append(" ]");
            if (Debug.DEBUG) {
              // 						System.err.print("[DEBUG]" + this + ": while closing a PooledConnection known
              // to be invalid, ");
              // 						System.err.println("  some exceptions occurred. This is probably not a
              // problem:");
              // 						System.err.println( debugOnlyLog.toString() );

              logger.fine(
                  this
                      + ": while closing a PooledConnection known to be invalid, "
                      + "  some exceptions occurred. This is probably not a problem: "
                      + debugOnlyLog.toString());
            }
          } else
            throw new SQLException(
                "At least one error occurred while attempting "
                    + "to close() the PooledConnection: "
                    + exc);
        }
        if (Debug.TRACE == Debug.TRACE_MAX)
          logger.fine("C3P0PooledConnection closed. [" + this + ']');
        // System.err.println("C3P0PooledConnection closed. [" + this + ']');
      } finally {
        physicalConnection = null;
      }
    }
  }