Example usage for org.apache.commons.pool2 ObjectPool borrowObject

List of usage examples for org.apache.commons.pool2 ObjectPool borrowObject

Introduction

In this page you can find the example usage for org.apache.commons.pool2 ObjectPool borrowObject.

Prototype

T borrowObject() throws Exception, NoSuchElementException, IllegalStateException;

Source Link

Document

Obtains an instance from this pool.

Usage

From source file:co.edu.unbosque.swii.PoolTest.java

@Test
public void quePasaCuandoSeRetornaUnaconexionContransaccionIniciada() throws Exception {
    FabricaConexiones fc = new FabricaConexiones("aretico.com", 5432, "software_2", "grupo7_5", pwd);
    ObjectPool<Connection> pool = new GenericObjectPool<Connection>(fc);
    Connection c = pool.borrowObject();
    c.close();//from w  w w . j  ava2  s .c om
    pool.returnObject(c);
}

From source file:co.edu.unbosque.swii.PoolTest.java

@Test(threadPoolSize = 5, invocationCount = 5)
public void midaTiemposParaInsertar1000RegistrosConSingleton()
        throws ClassNotFoundException, SQLException, Exception {
    long tiempo = System.currentTimeMillis();
    String SQL;//from  ww w.ja v a  2 s .c  o  m
    for (int x = 0; x < 1000; x++) {
        FabricaConexiones fc = new FabricaConexiones("aretico.com", 5432, "software_2", "grupo7_5", pwd);
        ObjectPool<Connection> pool = new GenericObjectPool<Connection>(fc);
        Connection c = pool.borrowObject();
        SQL = "INSERT INTO grupo7.RegistroHilos( registro, hilo, fecha) VALUES (?, ?, now());";
        PreparedStatement parametros = c.prepareStatement(SQL);
        parametros.setInt(1, x);
        parametros.setInt(2, (int) Thread.currentThread().getId());
        parametros.executeUpdate();
        pool.returnObject(c);
    }
    System.out.println("Tiempo de ejecucion " + (System.currentTimeMillis() - tiempo));
}

From source file:co.edu.unbosque.swii.PoolTest.java

@Test(expectedExceptions = org.postgresql.util.PSQLException.class, expectedExceptionsMessageRegExp = ".*too many connections.*")
public void soloDebeCrear5Conexiones() throws Exception {
    FabricaConexiones fc = new FabricaConexiones("aretico.com", 5432, "software_2", "grupo7_5", pwd);
    ObjectPool<Connection> pool = new GenericObjectPool<Connection>(fc);
    for (int i = 0; i < 6; i++) {
        pool.borrowObject();
    }//from   ww w.ja va  2  s  .co  m
}

From source file:co.edu.unbosque.swii.PoolTest.java

@Test
public void aprendiendoAControlarLasConexiones() throws Exception {
    FabricaConexiones fc = new FabricaConexiones("aretico.com", 5432, "software_2", "grupo7_5", pwd);
    ObjectPool<Connection> pool = new GenericObjectPool<Connection>(fc);
    for (int i = 0; i < 6; i++) {
        Connection c = pool.borrowObject();
        pool.returnObject(c);//from   www .  j ava 2 s. co m
    }
}

From source file:co.edu.unbosque.swii.PoolTest.java

@Test
public void quePasaCuandoSeCierraUnaConexionAntesDeRetornarla() throws Exception {
    FabricaConexiones fc = new FabricaConexiones("aretico.com", 5432, "software_2", "grupo7_5", pwd);
    ObjectPool<Connection> pool = new GenericObjectPool<Connection>(fc);
    for (int i = 0; i < 6; i++) {
        Connection c = pool.borrowObject();
        pool.returnObject(c);//from  w w  w. j  a  v a2 s  .c o  m
    }
}

From source file:com.mirth.connect.connectors.file.FileConnector.java

/**
 * Allocate a connection from the pool/*from  www .ja va 2 s  . c o  m*/
 * 
 * @param uri
 *            The URI of the endpoint for which the connection is being created.
 * @param message
 *            ??
 * @return The allocated connection.
 */
protected FileSystemConnection getConnection(FileSystemConnectionOptions fileSystemOptions) throws Exception {
    ObjectPool<FileSystemConnection> pool = getConnectionPool(fileSystemOptions);
    FileSystemConnection con = pool.borrowObject();
    if (!con.isConnected() || !con.isValid()) {
        destroyConnection(con, fileSystemOptions);
        con = pool.borrowObject();
    }
    synchronized (connections) {
        connections.add(con);
    }
    return con;
}

From source file:com.lambdaworks.redis.support.ConnectionPoolSupportTest.java

private void borrowAndReturn(ObjectPool<StatefulRedisConnection<String, String>> pool) throws Exception {

    for (int i = 0; i < 10; i++) {
        StatefulRedisConnection<String, String> connection = pool.borrowObject();
        RedisCommands<String, String> sync = connection.sync();
        sync.ping();//from  www. j a v a 2  s.c  om
        pool.returnObject(connection);
    }
}

From source file:com.lambdaworks.redis.support.ConnectionPoolSupportTest.java

private void borrowAndClose(ObjectPool<StatefulRedisConnection<String, String>> pool) throws Exception {

    for (int i = 0; i < 10; i++) {
        StatefulRedisConnection<String, String> connection = pool.borrowObject();
        RedisCommands<String, String> sync = connection.sync();
        sync.ping();/*w  w w  .j  a v  a2s. co  m*/
        sync.close();
    }
}

From source file:com.lambdaworks.redis.support.ConnectionPoolSupportTest.java

private void borrowAndCloseTryWithResources(ObjectPool<StatefulRedisConnection<String, String>> pool)
        throws Exception {

    for (int i = 0; i < 10; i++) {
        try (StatefulRedisConnection<String, String> connection = pool.borrowObject()) {
            RedisCommands<String, String> sync = connection.sync();
            sync.ping();/*  w ww.  ja  va 2 s.c  o m*/
        }
    }
}

From source file:io.lettuce.core.support.ConnectionPoolSupportTest.java

private void borrowAndClose(ObjectPool<StatefulRedisConnection<String, String>> pool) throws Exception {

    for (int i = 0; i < 10; i++) {
        StatefulRedisConnection<String, String> connection = pool.borrowObject();
        RedisCommands<String, String> sync = connection.sync();
        sync.ping();//ww w  . j  a v  a2s  . c om
        connection.close();
    }
}