Examples of ObjectPool


Examples of org.apache.commons.pool.ObjectPool

        handler.sendClientMsg("-ERR Pool Closed");
      }
      return;
    } else if(cmd.equals("client-handler-pool-dump")) /*v1.4.6*/{
     
      ObjectPool objectPool = target.getClientHandlerPool();

      if(PoolHelper.isPoolOpen(objectPool)==true) {
        if(QSObjectPool.class.isInstance(objectPool)==false) {
          handler.sendClientMsg("-ERR System Error!");
        }
View Full Code Here

Examples of org.apache.commons.pool.ObjectPool

    // actual pool of connections.
    //
    // We'll use a GenericObjectPool instance, although
    // any ObjectPool implementation will suffice.
    //
    ObjectPool connectionPool = new GenericObjectPool(null);

    //
    // Next, we'll create a ConnectionFactory that the
    // pool will use to create Connections.
    // We'll use the DriverManagerConnectionFactory,
    // using the connect string passed in the command line
    // arguments.
    //
    //ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI,null);
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, "admin_user", "client00");

    //
    // Now we'll create the PoolableConnectionFactory, which wraps
    // the "real" Connections created by the ConnectionFactory with
    // the classes that implement the pooling functionality.
    //
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);

    //
    // Finally, we create the PoolingDriver itself...
    //
    PoolingDriver driver = new PoolingDriver();

    //
    // ...and register our pool with it.
    //
    driver.registerPool("example",connectionPool);

    //
    // Now we can just use the connect string "jdbc:apache:commons:dbcp:example"
    // to access our pool of Connections.
    //
    System.out.println("Setting up driver: Done.");

    //
    // Now, we can use JDBC as we normally would.
    // Using the connect string
    //  jdbc:apache:commons:dbcp:example
    // The general form being:
    //  jdbc:apache:commons:dbcp:<name-of-pool>
    //

    Connection conn = null;
    PreparedStatement pstmt = null;
    Statement stmt = null;
    ResultSet rset = null;

    // Check to see if connection is pooled
    long startTime = new Date().getTime();
    long currentTime;
    System.out.println("Creating connection.");
    try {
      conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:example");
      conn.setAutoCommit(false);
      System.out.println("Connection time=" + (new Date().getTime() - startTime));
      System.out.println("Creating statement.");

      // Clean table
      stmt = conn.createStatement();
      rset = stmt.executeQuery("delete from Test;");
     
      sql = "insert into APP.Test " +
        "set TestID=? " +
        ", TextField=?";
      pstmt = conn.prepareStatement(sql);
      System.out.println("Executing statement.");
    } catch(SQLException e) {
      e.printStackTrace();
      try { pstmt.close(); } catch(Exception e1) { }
      try { conn.close(); } catch(Exception e2) { }
    }
   
    for (int n=1; n <101; n++) {
      System.out.println("=======");
      System.out.println("n=" + n);

      try {
        pstmt.setInt(1, n);
        pstmt.setString(2, "This is field " + n);
        rset = pstmt.executeQuery();
      } catch(SQLException e) {
        e.printStackTrace();
        try { pstmt.close(); } catch(Exception e2) { }
        try { conn.close(); } catch(Exception e2) { }
      }
      // Reset time
      startTime = new Date().getTime();
    }
   
    try {
      conn.commit();
    } catch(SQLException e) {
      e.printStackTrace();
    }
   
    try { pstmt.close(); } catch(Exception e2) { }
    try { conn.close(); } catch(Exception e2) { }

    System.out.println("DBCP example: sql done");
   
    // Close connection pool
    try {
      connectionPool.close();
    } catch (Exception e) {
      System.out.println("Exception thrown closing connection pool:");
      System.out.println(e.getMessage());
    }
   
View Full Code Here

Examples of org.apache.commons.pool.ObjectPool

    // actual pool of connections.
    //
    // We'll use a GenericObjectPool instance, although
    // any ObjectPool implementation will suffice.
    //
    ObjectPool connectionPool = new GenericObjectPool(null);

    //
    // Next, we'll create a ConnectionFactory that the
    // pool will use to create Connections.
    // We'll use the DriverManagerConnectionFactory,
View Full Code Here

Examples of org.apache.commons.pool.ObjectPool

            {
                log.error("Unable to obtain a connection database via URI: "+connectURI, e);
                throw e;
            }
           
            ObjectPool connectionPool = new GenericObjectPool(null, maxActive, whenExhausted, maxWait);
           
            ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, user, password);
           
            dsConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);
           
View Full Code Here

Examples of org.apache.commons.pool.ObjectPool

    protected Object getNthObject(int n) {
        return String.valueOf(n);
    }

    public void testIdleCap() throws Exception {
        ObjectPool pool = makeEmptyPool(8);
        Object[] active = new Object[100];
        for(int i=0;i<100;i++) {
            active[i] = pool.borrowObject();
        }
        assertEquals(100,pool.getNumActive());
        assertEquals(0,pool.getNumIdle());
        for(int i=0;i<100;i++) {
            pool.returnObject(active[i]);
            assertEquals(99 - i,pool.getNumActive());
            assertEquals((i < 8 ? i+1 : 8),pool.getNumIdle());
        }
    }
View Full Code Here

Examples of org.apache.commons.pool.ObjectPool

            assertEquals((i < 8 ? i+1 : 8),pool.getNumIdle());
        }
    }

    public void testPoolWithNullFactory() throws Exception {
        ObjectPool pool = new StackObjectPool(10);
        for(int i=0;i<10;i++) {
            pool.returnObject(new Integer(i));
        }
        for(int j=0;j<3;j++) {
            Integer[] borrowed = new Integer[10];
            BitSet found = new BitSet();
            for(int i=0;i<10;i++) {
                borrowed[i] = (Integer)(pool.borrowObject());
                assertNotNull(borrowed);
                assertTrue(!found.get(borrowed[i].intValue()));
                found.set(borrowed[i].intValue());
            }
            for(int i=0;i<10;i++) {
                pool.returnObject(borrowed[i]);
            }
        }
        pool.invalidateObject(pool.borrowObject());
        pool.invalidateObject(pool.borrowObject());
        pool.clear();       
    }
View Full Code Here

Examples of org.apache.commons.pool.ObjectPool

        pool.invalidateObject(pool.borrowObject());
        pool.clear();       
    }
   
    public void testBorrowFromEmptyPoolWithNullFactory() throws Exception {
        ObjectPool pool = new StackObjectPool();
        try {
            pool.borrowObject();
            fail("Expected NoSuchElementException");
        } catch(NoSuchElementException e) {
            // expected
        }
    }
View Full Code Here

Examples of org.apache.commons.pool.ObjectPool

            // expected
        }
    }
   
    public void testSetFactory() throws Exception {
        ObjectPool pool = new StackObjectPool();
        try {
            pool.borrowObject();
            fail("Expected NoSuchElementException");
        } catch(NoSuchElementException e) {
            // expected
        }
        pool.setFactory(new SimpleFactory());
        Object obj = pool.borrowObject();
        assertNotNull(obj);
        pool.returnObject(obj);
    }
View Full Code Here

Examples of org.apache.commons.pool.ObjectPool

        assertNotNull(obj);
        pool.returnObject(obj);
    }

    public void testCantResetFactoryWithActiveObjects() throws Exception {
        ObjectPool pool = new StackObjectPool();
        pool.setFactory(new SimpleFactory());
        Object obj = pool.borrowObject();
        assertNotNull(obj);

        try {
            pool.setFactory(new SimpleFactory());
            fail("Expected IllegalStateException");
        } catch(IllegalStateException e) {
            // expected
        }       
    }
View Full Code Here

Examples of org.apache.commons.pool.ObjectPool

            // expected
        }       
    }
   
    public void testCanResetFactoryWithoutActiveObjects() throws Exception {
        ObjectPool pool = new StackObjectPool();
        {
            pool.setFactory(new SimpleFactory());
            Object obj = pool.borrowObject();       
            assertNotNull(obj);
            pool.returnObject(obj);
        }
        {
            pool.setFactory(new SimpleFactory());
            Object obj = pool.borrowObject();       
            assertNotNull(obj);
            pool.returnObject(obj);
        }
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.