Example usage for javax.sql PooledConnection getConnection

List of usage examples for javax.sql PooledConnection getConnection

Introduction

In this page you can find the example usage for javax.sql PooledConnection getConnection.

Prototype

Connection getConnection() throws SQLException;

Source Link

Document

Creates and returns a Connection object that is a handle for the physical connection that this PooledConnection object represents.

Usage

From source file:ConnPool.java

public static void main(String[] args) throws Exception {
    OracleConnectionPoolDataSource ocpds = new OracleConnectionPoolDataSource();
    ocpds.setURL("jdbc:oracle:thin:@localhost:1521:ORCL");
    ocpds.setUser("user");
    ocpds.setPassword("password");

    PooledConnection pc_1 = ocpds.getPooledConnection();

    Connection conn_1 = pc_1.getConnection();
    Statement stmt = conn_1.createStatement();

    ResultSet rs = stmt.executeQuery("SELECT count(*) FROM v$session WHERE username = 'SYS'");
    rs.next();/* w  w w  . j  a v  a2  s. c  om*/
    String msg = "Total connections after ";
    System.out.println(msg + "conn_1: " + rs.getString(1));

    Connection conn_2 = pc_1.getConnection();
    stmt = conn_2.createStatement();
    rs = stmt.executeQuery("SELECT count(*) FROM v$session WHERE username = 'SYS'");
    rs.next();
    System.out.println(msg + "conn_2: " + rs.getString(1));

    PooledConnection pc_2 = ocpds.getPooledConnection();
    rs = stmt.executeQuery("SELECT count(*) FROM v$session WHERE username = 'SYS'");
    rs.next();
    System.out.println(msg + "pc_2: " + rs.getString(1));

    conn_1.close();
    conn_2.close();
    pc_1.close();
    pc_2.close();
}

From source file:Main.java

private static Connection getConnection() throws NamingException, SQLException {
    InitialContext initCtx = createContext();
    String jndiName = "HrDS";
    ConnectionPoolDataSource dataSource = (ConnectionPoolDataSource) initCtx.lookup(jndiName);
    PooledConnection pooledConnection = dataSource.getPooledConnection();
    return pooledConnection.getConnection(); // Obtain connection from pool
}

From source file:PooledConnectionExample.java

private static Connection getConnection() throws NamingException, SQLException {
    InitialContext initCtx = createContext();
    String jndiName = "HrDS";
    ConnectionPoolDataSource dataSource = (ConnectionPoolDataSource) initCtx.lookup(jndiName);
    PooledConnection pooledConnection = dataSource.getPooledConnection();
    return pooledConnection.getConnection();
}

From source file:com.microsoft.sqlserver.jdbc.connection.PoolingTest.java

@Test
public void testConnectionPoolClose() throws SQLException {
    SQLServerXADataSource ds = new SQLServerXADataSource();
    ds.setURL(connectionString);/*from w  w  w .  ja va 2 s  .c  o m*/

    PooledConnection pc = ds.getPooledConnection();
    Connection con = pc.getConnection();

    pc.close();
    // assert that the first connection is closed.
    assertTrue(con.isClosed(), "Connection is not closed with pool close");
}

From source file:com.microsoft.sqlserver.jdbc.connection.PoolingTest.java

@Test
public void testConnectionPoolReget() throws SQLException {
    SQLServerXADataSource ds = new SQLServerXADataSource();
    ds.setURL(connectionString);/*from  ww w. java2s .c  o  m*/

    PooledConnection pc = ds.getPooledConnection();
    Connection con = pc.getConnection();

    // now reget a connection
    Connection con2 = pc.getConnection();

    // assert that the first connection is closed.
    assertTrue(con.isClosed(), "First connection is not closed");
}

From source file:com.microsoft.sqlserver.jdbc.connection.PoolingTest.java

@Test
public void testConnectionPoolConnFunctions() throws SQLException {
    String tableName = RandomUtil.getIdentifier("table");
    tableName = DBTable.escapeIdentifier(tableName);

    String sql1 = "if exists (select * from dbo.sysobjects where name = '" + tableName + "' and type = 'U')\n"
            + "drop table " + tableName + "\n" + "create table " + tableName + "\n" + "(\n"
            + "wibble_id int primary key not null,\n" + "counter int null\n" + ");";
    String sql2 = "if exists (select * from dbo.sysobjects where name = '" + tableName + "' and type = 'U')\n"
            + "drop table " + tableName + "\n";

    SQLServerXADataSource ds = new SQLServerXADataSource();
    ds.setURL(connectionString);//  www.  j ava 2s .co  m

    PooledConnection pc = ds.getPooledConnection();
    Connection con = pc.getConnection();

    Statement statement = con.createStatement();
    statement.execute(sql1);
    statement.execute(sql2);
    con.clearWarnings();
    pc.close();
}

From source file:com.microsoft.sqlserver.jdbc.connection.PoolingTest.java

@Test
public void testPooling() throws SQLException {
    assumeTrue(!DBConnection.isSqlAzure(DriverManager.getConnection(connectionString)),
            "Skipping test case on Azure SQL.");

    String randomTableName = RandomUtil.getIdentifier("table");

    // make the table a temporary table (will be created in tempdb database)
    String tempTableName = "#" + randomTableName;

    SQLServerXADataSource XADataSource1 = new SQLServerXADataSource();
    XADataSource1.setURL(connectionString);
    XADataSource1.setDatabaseName("tempdb");

    PooledConnection pc = XADataSource1.getPooledConnection();
    Connection conn = pc.getConnection();

    // create table in tempdb database
    conn.createStatement().execute("create table [" + tempTableName + "] (myid int)");
    conn.createStatement().execute("insert into [" + tempTableName + "] values (1)");
    conn.close();// ww w . j av a  2 s.c  o  m

    conn = pc.getConnection();

    boolean tempTableFileRemoved = false;
    try {
        conn.createStatement().executeQuery("select * from [" + tempTableName + "]");
    } catch (SQLServerException e) {
        // make sure the temporary table is not found.
        if (e.getMessage().startsWith("Invalid object name")) {
            tempTableFileRemoved = true;
        }
    }
    assertTrue(tempTableFileRemoved, "Temporary table is not removed.");
}

From source file:com.microsoft.sqlserver.jdbc.connection.PoolingTest.java

@Test
public void testConnectionPoolClientConnectionId() throws SQLException {
    SQLServerXADataSource ds = new SQLServerXADataSource();
    ds.setURL(connectionString);/*w  w w  .j  a  va  2  s  .com*/

    PooledConnection pc = ds.getPooledConnection();
    ISQLServerConnection con = (ISQLServerConnection) pc.getConnection();

    UUID Id1 = con.getClientConnectionId();
    assertTrue(Id1 != null, "Unexecepted: ClientConnectionId is null from Pool");
    con.close();

    // now reget the connection
    ISQLServerConnection con2 = (ISQLServerConnection) pc.getConnection();

    UUID Id2 = con2.getClientConnectionId();
    con2.close();

    assertEquals(Id1, Id2, "ClientConnection Ids from pool are not the same.");
}

From source file:com.alibaba.wasp.jdbcx.JdbcConnectionPool.java

private Connection getConnectionNow() throws SQLException {
    if (isDisposed) {
        throw new IllegalStateException("Connection pool has been disposed.");
    }/*from w  w  w .j av a  2 s  .  co  m*/
    PooledConnection pc;
    if (!recycledConnections.isEmpty()) {
        pc = recycledConnections.remove(recycledConnections.size() - 1);
    } else {
        pc = dataSource.getPooledConnection();
    }
    Connection conn = pc.getConnection();
    activeConnections.incrementAndGet();
    pc.addConnectionEventListener(this);
    return conn;
}