Search in sources :

Example 56 with Connection

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

the class IsolationTest method testNonIsolation.

@Test
public void testNonIsolation() throws SQLException {
    try (HikariDataSource ds = newHikariDataSource()) {
        ds.setMinimumIdle(1);
        ds.setMaximumPoolSize(1);
        ds.setIsolateInternalQueries(false);
        ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
        try (Connection connection = ds.getConnection()) {
            connection.close();
            try (Connection connection2 = ds.getConnection()) {
                connection2.close();
                assertSame(connection.unwrap(Connection.class), connection2.unwrap(Connection.class));
            }
        }
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) TestElf.newHikariDataSource(com.zaxxer.hikari.pool.TestElf.newHikariDataSource) Connection(java.sql.Connection) Test(org.junit.Test)

Example 57 with Connection

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

the class IsolationTest method testIsolation.

@Test
public void testIsolation() throws SQLException {
    try (HikariDataSource ds = newHikariDataSource()) {
        ds.setMinimumIdle(1);
        ds.setMaximumPoolSize(1);
        ds.setIsolateInternalQueries(true);
        ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
        try (Connection connection = ds.getConnection()) {
            connection.close();
            try (Connection connection2 = ds.getConnection()) {
                connection2.close();
                assertNotSame(connection, connection2);
                assertSame(connection.unwrap(Connection.class), connection2.unwrap(Connection.class));
            }
        }
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) TestElf.newHikariDataSource(com.zaxxer.hikari.pool.TestElf.newHikariDataSource) Connection(java.sql.Connection) Test(org.junit.Test)

Example 58 with Connection

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

the class JdbcDriverTest method driverTest1.

@Test
public void driverTest1() throws SQLException {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(1);
    config.setMaximumPoolSize(1);
    config.setConnectionTestQuery("VALUES 1");
    config.setDriverClassName("com.zaxxer.hikari.mocks.StubDriver");
    config.setJdbcUrl("jdbc:stub");
    config.addDataSourceProperty("user", "bart");
    config.addDataSourceProperty("password", "simpson");
    ds = new HikariDataSource(config);
    assertTrue(ds.isWrapperFor(DriverDataSource.class));
    DriverDataSource unwrap = ds.unwrap(DriverDataSource.class);
    assertNotNull(unwrap);
    try (Connection connection = ds.getConnection()) {
    // test that getConnection() succeeds
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) DriverDataSource(com.zaxxer.hikari.util.DriverDataSource) Connection(java.sql.Connection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Example 59 with Connection

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

the class MetricsTrackerTest method connectionTimeoutIsRecorded.

@Test(expected = SQLTransientConnectionException.class)
public void connectionTimeoutIsRecorded() throws Exception {
    int timeoutMillis = 1000;
    int timeToCreateNewConnectionMillis = timeoutMillis * 2;
    StubDataSource stubDataSource = new StubDataSource();
    stubDataSource.setConnectionAcquistionTime(timeToCreateNewConnectionMillis);
    StubMetricsTracker metricsTracker = new StubMetricsTracker();
    try (HikariDataSource ds = newHikariDataSource()) {
        ds.setMinimumIdle(0);
        ds.setMaximumPoolSize(1);
        ds.setConnectionTimeout(timeoutMillis);
        ds.setDataSource(stubDataSource);
        ds.setMetricsTrackerFactory((poolName, poolStats) -> metricsTracker);
        try (Connection c = ds.getConnection()) {
            fail("Connection shouldn't have been successfully created due to configured connection timeout");
        } finally {
            // assert that connection timeout was measured
            assertThat(metricsTracker.connectionTimeoutRecorded, is(true));
            // assert that measured time to acquire connection should be roughly equal or greater than the configured connection timeout time
            assertTrue(metricsTracker.connectionAcquiredNanos >= TimeUnit.NANOSECONDS.convert(timeoutMillis, TimeUnit.MILLISECONDS));
        }
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) TestElf.newHikariDataSource(com.zaxxer.hikari.pool.TestElf.newHikariDataSource) Connection(java.sql.Connection) StubDataSource(com.zaxxer.hikari.mocks.StubDataSource) Test(org.junit.Test)

Example 60 with Connection

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

the class RampUpDown method rampUpDownTest.

@Test
public void rampUpDownTest() throws SQLException {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(5);
    config.setMaximumPoolSize(60);
    config.setInitializationFailTimeout(0);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "250");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        ds.setIdleTimeout(1000);
        HikariPool pool = getPool(ds);
        // wait two housekeeping periods so we don't fail if this part of test runs too quickly
        quietlySleep(500);
        Assert.assertSame("Total connections not as expected", 5, pool.getTotalConnections());
        Connection[] connections = new Connection[ds.getMaximumPoolSize()];
        for (int i = 0; i < connections.length; i++) {
            connections[i] = ds.getConnection();
        }
        assertSame("Total connections not as expected", 60, pool.getTotalConnections());
        for (Connection connection : connections) {
            connection.close();
        }
        quietlySleep(500);
        assertSame("Total connections not as expected", 5, pool.getTotalConnections());
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) Connection(java.sql.Connection) 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