示例#1
0
 public void run() {
   while (true) {
     if (shuttingDown.get()) break;
     cleanupExpiredConnections(System.currentTimeMillis());
     if (shuttingDown.get()) break;
     try {
       Thread.sleep(5000L);
     } catch (InterruptedException e) {
       break;
     }
   }
 }
示例#2
0
 static void doSomething(DataSource ds) throws SQLException {
   Connection con = null;
   Statement stmt = null;
   try {
     con = ds.getConnection();
     stmt = con.createStatement();
     int i =
         stmt.executeUpdate(
             "INSERT INTO TRSS_TABLE VALUES ('" + System.currentTimeMillis() + "')");
     if (i != 1) throw new SQLException("Insert failed somehow strange!");
   } finally {
     StatementUtils.attemptClose(stmt);
     ConnectionUtils.attemptClose(con);
   }
 }
  public synchronized void close() {
    resetPoolManager();
    is_closed = true;

    C3P0Registry.markClosed(this);

    if (Debug.DEBUG && Debug.TRACE == Debug.TRACE_MAX && logger.isLoggable(MLevel.FINEST)) {
      logger.log(
          MLevel.FINEST,
          this.getClass().getName()
              + '@'
              + Integer.toHexString(System.identityHashCode(this))
              + " has been closed. ",
          new Exception("DEBUG STACK TRACE for PoolBackedDataSource.close()."));
    }
  }
  // javax.sql.ConnectionPoolDataSource interface methods
  public PooledConnection getPooledConnection() throws SQLException {
    PooledConnection pc;
    if (cpDS_ != null) {
      if (out_ != null) out_.println(getTraceId() + "getPooledConnection()");

      pc = cpDS_.getPooledConnection();

      if (out_ != null)
        out_.println(
            getTraceId()
                + "getPooledConnection() returns PooledConnection ["
                + System.identityHashCode(pc)
                + "]");

      return new TPooledConnection(pc, out_);
    } else throw new SQLException("Trace Data Source" + traceDataSource_ + "Not found");
  }
  public String getTraceId() {

    // Build up jdbcTrace output entry
    setTraceId(
        org.trafodion.jdbc.t2.T2Driver.traceText
            + org.trafodion.jdbc.t2.T2Driver.dateFormat.format(new Date())
            + "]:["
            + Thread.currentThread()
            + "]:["
            + System.identityHashCode(this)
            + "]:"
            + getClass()
                .getName()
                .substring(
                    org.trafodion.jdbc.t2.T2Driver.REMOVE_PKG_NAME, getClass().getName().length())
            + ".");
    return traceId_;
  }
  TConnectionPoolDataSource(String traceDataSource, ConnectionPoolDataSource cpDS) {
    String className = null;

    traceDataSource_ = traceDataSource;
    cpDS_ = cpDS;
    if (cpDS_ != null) className = cpDS_.getClass().getName();

    // Build up jdbcTrace output entry
    setTraceId(
        org.trafodion.jdbc.t2.T2Driver.traceText
            + org.trafodion.jdbc.t2.T2Driver.dateFormat.format(new Date())
            + "]:["
            + Thread.currentThread()
            + "]:["
            + System.identityHashCode(cpDS_)
            + "]:"
            + className.substring(
                org.trafodion.jdbc.t2.T2Driver.REMOVE_PKG_NAME, className.length())
            + ".");
  }
  public PooledConnection getPooledConnection(String username, String password)
      throws SQLException {
    PooledConnection pc;
    if (cpDS_ != null) {
      if (out_ != null)
        out_.println(getTraceId() + "getPooledConnection(\"" + username + "\", \"****\")");

      pc = cpDS_.getPooledConnection(username, password);

      if (out_ != null)
        out_.println(
            getTraceId()
                + "getPooledConnection(\""
                + username
                + "\", \"****\") returns PooledConnection ["
                + System.identityHashCode(pc)
                + "]");

      return new TPooledConnection(pc, out_);
    } else throw new SQLException("Trace Data Source" + traceDataSource_ + "Not found");
  }
示例#8
0
  /**
   * Se conecta a un servicio JDBC usando java.naming. Los parametros de configuracion se manejan
   * para el contexto de la aplicacion, permitiendo un pool de conexiónes persistentes disponibles
   * para toda la aplicacion. Tomcat proporciona este servicio configurandolo en el archivo web.xml
   * o server.xml
   *
   * @param servicio Una cadena como "java:comp/env/servicio"
   */
  protected boolean conectar(String servicio) throws Exception {

    /*
     *Para conectarse con Tomcat
     *en el archivo de coniguracion se especifican
     *los parametros de conexión.
     */
    long t = System.currentTimeMillis();
    // Context es un objeto que encapsula el contexto de la aplicacion
    Context ctx = new InitialContext();
    // DataSource es el origen de datos,
    // un servicio JDBC proporcionado mediante java naming
    // El nombre del servicio deberia ser recibido como
    // argumento
    DataSource ds = (DataSource) ctx.lookup(servicio);

    // Ahora si obtiene la conexión
    this.conexión = ds.getConnection();

    return this.conexión != null;
  } // Fin conectar
示例#9
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();
    }
  }
示例#10
0
 private static void usage() {
   System.err.println("java " + JndiLookupTest.class.getName() + " \\");
   System.err.println("\t<dmds_name> <wcpds_name> <wpbds_name>");
   System.exit(-1);
 }