Search in sources :

Example 61 with Connection

use of java.sql.Connection in project HikariCP by brettwooldridge.

the class SaturatedPoolTest830 method saturatedPoolTest.

@Test
public void saturatedPoolTest() throws Exception {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(5);
    config.setMaximumPoolSize(MAX_POOL_SIZE);
    config.setInitializationFailTimeout(Long.MAX_VALUE);
    config.setConnectionTimeout(1000);
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    StubConnection.slowCreate = true;
    StubStatement.setSimulatedQueryTime(1000);
    setSlf4jLogLevel(HikariPool.class, Level.DEBUG);
    System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "5000");
    final long start = currentTime();
    try (final HikariDataSource ds = new HikariDataSource(config)) {
        LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(50, /*core*/
        50, /*max*/
        2, /*keepalive*/
        SECONDS, queue, new ThreadPoolExecutor.CallerRunsPolicy());
        threadPool.allowCoreThreadTimeOut(true);
        AtomicInteger windowIndex = new AtomicInteger();
        boolean[] failureWindow = new boolean[100];
        Arrays.fill(failureWindow, true);
        // Initial saturation
        for (int i = 0; i < 50; i++) {
            threadPool.execute(() -> {
                try (Connection conn = ds.getConnection();
                    Statement stmt = conn.createStatement()) {
                    stmt.execute("SELECT bogus FROM imaginary");
                } catch (SQLException e) {
                    LOGGER.info(e.getMessage());
                }
            });
        }
        long sleep = 80;
        outer: while (true) {
            quietlySleep(sleep);
            if (elapsedMillis(start) > SECONDS.toMillis(12) && sleep < 100) {
                sleep = 100;
                LOGGER.warn("Switching to 100ms sleep");
            } else if (elapsedMillis(start) > SECONDS.toMillis(6) && sleep < 90) {
                sleep = 90;
                LOGGER.warn("Switching to 90ms sleep");
            }
            threadPool.execute(() -> {
                int ndx = windowIndex.incrementAndGet() % failureWindow.length;
                try (Connection conn = ds.getConnection();
                    Statement stmt = conn.createStatement()) {
                    stmt.execute("SELECT bogus FROM imaginary");
                    failureWindow[ndx] = false;
                } catch (SQLException e) {
                    LOGGER.info(e.getMessage());
                    failureWindow[ndx] = true;
                }
            });
            for (int i = 0; i < failureWindow.length; i++) {
                if (failureWindow[i]) {
                    if (elapsedMillis(start) % (SECONDS.toMillis(1) - sleep) < sleep) {
                        LOGGER.info("Active threads {}, submissions per second {}, waiting threads {}", threadPool.getActiveCount(), SECONDS.toMillis(1) / sleep, getPool(ds).getThreadsAwaitingConnection());
                    }
                    continue outer;
                }
            }
            LOGGER.info("Timeouts have subsided.");
            LOGGER.info("Active threads {}, submissions per second {}, waiting threads {}", threadPool.getActiveCount(), SECONDS.toMillis(1) / sleep, getPool(ds).getThreadsAwaitingConnection());
            break;
        }
        LOGGER.info("Waiting for completion of {} active tasks.", threadPool.getActiveCount());
        while (getPool(ds).getActiveConnections() > 0) {
            quietlySleep(50);
        }
        assertEquals("Rate not in balance at 10req/s", SECONDS.toMillis(1) / sleep, 10L);
    } finally {
        StubStatement.setSimulatedQueryTime(0);
        StubConnection.slowCreate = false;
        System.clearProperty("com.zaxxer.hikari.housekeeping.periodMs");
        setSlf4jLogLevel(HikariPool.class, Level.INFO);
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) Statement(java.sql.Statement) StubStatement(com.zaxxer.hikari.mocks.StubStatement) Connection(java.sql.Connection) StubConnection(com.zaxxer.hikari.mocks.StubConnection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Test(org.junit.Test)

Example 62 with Connection

use of java.sql.Connection in project HikariCP by brettwooldridge.

the class StatementTest method testStatementClose.

@Test
public void testStatementClose() throws SQLException {
    ds.getConnection().close();
    HikariPool pool = getPool(ds);
    assertTrue("Total connections not as expected", pool.getTotalConnections() >= 1);
    assertTrue("Idle connections not as expected", pool.getIdleConnections() >= 1);
    try (Connection connection = ds.getConnection()) {
        assertNotNull(connection);
        assertTrue("Total connections not as expected", pool.getTotalConnections() >= 1);
        assertTrue("Idle connections not as expected", pool.getIdleConnections() >= 0);
        Statement statement = connection.createStatement();
        assertNotNull(statement);
        connection.close();
        assertTrue(statement.isClosed());
    }
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) Test(org.junit.Test)

Example 63 with Connection

use of java.sql.Connection in project HikariCP by brettwooldridge.

the class UnwrapTest method testUnwrapConnection.

@Test
public void testUnwrapConnection() throws SQLException {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(1);
    config.setMaximumPoolSize(1);
    config.setInitializationFailTimeout(0);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        ds.getConnection().close();
        assertSame("Idle connections not as expected", 1, getPool(ds).getIdleConnections());
        Connection connection = ds.getConnection();
        assertNotNull(connection);
        StubConnection unwrapped = connection.unwrap(StubConnection.class);
        assertTrue("unwrapped connection is not instance of StubConnection: " + unwrapped, (unwrapped != null && unwrapped instanceof StubConnection));
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) StubConnection(com.zaxxer.hikari.mocks.StubConnection) Connection(java.sql.Connection) StubConnection(com.zaxxer.hikari.mocks.StubConnection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Example 64 with Connection

use of java.sql.Connection in project HikariCP by brettwooldridge.

the class TestProxies method testOtherExceptions.

@Test
public void testOtherExceptions() throws SQLException {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(0);
    config.setMaximumPoolSize(1);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        try (Connection conn = ds.getConnection()) {
            StubConnection stubConnection = conn.unwrap(StubConnection.class);
            stubConnection.throwException = true;
            try {
                conn.setTransactionIsolation(Connection.TRANSACTION_NONE);
                fail();
            } catch (SQLException e) {
            // pass
            }
            try {
                conn.isReadOnly();
                fail();
            } catch (SQLException e) {
            // pass
            }
            try {
                conn.setReadOnly(false);
                fail();
            } catch (SQLException e) {
            // pass
            }
            try {
                conn.setCatalog("");
                fail();
            } catch (SQLException e) {
            // pass
            }
            try {
                conn.setAutoCommit(false);
                fail();
            } catch (SQLException e) {
            // pass
            }
            try {
                conn.clearWarnings();
                fail();
            } catch (SQLException e) {
            // pass
            }
            try {
                conn.isValid(0);
                fail();
            } catch (SQLException e) {
            // pass
            }
            try {
                conn.isWrapperFor(getClass());
                fail();
            } catch (SQLException e) {
            // pass
            }
            try {
                conn.unwrap(getClass());
                fail();
            } catch (SQLException e) {
            // pass
            }
            try {
                conn.close();
                fail();
            } catch (SQLException e) {
            // pass
            }
            try {
                assertFalse(conn.isValid(0));
            } catch (SQLException e) {
                fail();
            }
        }
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) StubConnection(com.zaxxer.hikari.mocks.StubConnection) Connection(java.sql.Connection) StubConnection(com.zaxxer.hikari.mocks.StubConnection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Example 65 with Connection

use of java.sql.Connection in project HikariCP by brettwooldridge.

the class TestProxies method testStatementExceptions.

@Test
public void testStatementExceptions() throws SQLException {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(0);
    config.setMaximumPoolSize(1);
    config.setConnectionTimeout(TimeUnit.SECONDS.toMillis(1));
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        Connection conn = ds.getConnection();
        StubConnection stubConnection = conn.unwrap(StubConnection.class);
        stubConnection.throwException = true;
        try {
            conn.createStatement();
            fail();
        } catch (SQLException e) {
        // pass
        }
        try {
            conn.createStatement(0, 0);
            fail();
        } catch (SQLException e) {
        // pass
        }
        try {
            conn.createStatement(0, 0, 0);
            fail();
        } catch (SQLException e) {
        // pass
        }
        try {
            conn.prepareCall("");
            fail();
        } catch (SQLException e) {
        // pass
        }
        try {
            conn.prepareCall("", 0, 0);
            fail();
        } catch (SQLException e) {
        // pass
        }
        try {
            conn.prepareCall("", 0, 0, 0);
            fail();
        } catch (SQLException e) {
        // pass
        }
        try {
            conn.prepareStatement("");
            fail();
        } catch (SQLException e) {
        // pass
        }
        try {
            conn.prepareStatement("", 0);
            fail();
        } catch (SQLException e) {
        // pass
        }
        try {
            conn.prepareStatement("", new int[0]);
            fail();
        } catch (SQLException e) {
        // pass
        }
        try {
            conn.prepareStatement("", new String[0]);
            fail();
        } catch (SQLException e) {
        // pass
        }
        try {
            conn.prepareStatement("", 0, 0);
            fail();
        } catch (SQLException e) {
        // pass
        }
        try {
            conn.prepareStatement("", 0, 0, 0);
            fail();
        } catch (SQLException e) {
        // pass
        }
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) StubConnection(com.zaxxer.hikari.mocks.StubConnection) Connection(java.sql.Connection) StubConnection(com.zaxxer.hikari.mocks.StubConnection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Aggregations

Connection (java.sql.Connection)7652 PreparedStatement (java.sql.PreparedStatement)3358 SQLException (java.sql.SQLException)3242 ResultSet (java.sql.ResultSet)3236 Test (org.junit.Test)2507 Statement (java.sql.Statement)1631 Properties (java.util.Properties)1202 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)689 ArrayList (java.util.ArrayList)631 BaseConnectionlessQueryTest (org.apache.phoenix.query.BaseConnectionlessQueryTest)232 IOException (java.io.IOException)227 DataSource (javax.sql.DataSource)223 BaseTest (org.apache.phoenix.query.BaseTest)201 CallableStatement (java.sql.CallableStatement)194 DatabaseMetaData (java.sql.DatabaseMetaData)174 HashMap (java.util.HashMap)164 Reader (java.io.Reader)145 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)143 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)134 Map (java.util.Map)126