Search in sources :

Example 41 with HikariDataSource

use of com.zaxxer.hikari.HikariDataSource in project jDialects by drinkjava2.

the class DialectTest method buildH2Datasource.

// =======test guess dialects=======
private static HikariDataSource buildH2Datasource() {
    HikariDataSource ds = new HikariDataSource();
    ds.addDataSourceProperty("cachePrepStmts", true);
    ds.addDataSourceProperty("prepStmtCacheSize", 250);
    ds.addDataSourceProperty("prepStmtCacheSqlLimit", 2048);
    ds.addDataSourceProperty("useServerPrepStmts", true);
    ds.setMaximumPoolSize(3);
    ds.setConnectionTimeout(5000);
    ds.setJdbcUrl("jdbc:h2:mem:DBName;MODE=MYSQL;DB_CLOSE_DELAY=-1;TRACE_LEVEL_SYSTEM_OUT=0");
    ds.setDriverClassName("org.h2.Driver");
    ds.setUsername("sa");
    ds.setPassword("");
    return ds;
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource)

Example 42 with HikariDataSource

use of com.zaxxer.hikari.HikariDataSource in project jDialects by drinkjava2.

the class TestDemo method doTest.

@Test
public void doTest() {
    // DataSource
    HikariDataSource ds = new HikariDataSource();
    // H2 is a memory database
    ds.setDriverClassName("org.h2.Driver");
    ds.setJdbcUrl("jdbc:h2:mem:DBName;MODE=MYSQL;DB_CLOSE_DELAY=-1;TRACE_LEVEL_SYSTEM_OUT=0");
    ds.setUsername("sa");
    ds.setPassword("");
    // MySQL
    // ds.setDriverClassName("com.mysql.jdbc.Driver");
    // ds.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test?rewriteBatchedStatements=true&useSSL=false");
    // ds.setUsername("root");
    // ds.setPassword("root888");
    // MS-SqlServer
    // ds.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    // ds.setJdbcUrl("jdbc:sqlserver://localhost:1433;databaseName=test");
    // ds.setUsername("sa");
    // ds.setPassword("root888");
    // ORACLE
    // ds.setDriverClassName("oracle.jdbc.OracleDriver");
    // ds.setJdbcUrl("jdbc:oracle:thin:@127.0.0.1:1521:XE");
    // ds.setUsername("root");
    // ds.setPassword("root888");
    Dialect dialect = Dialect.guessDialect(ds);
    // Dialect.setAllowShowDialectLog(true);
    Connection conn = null;
    try {
        conn = ds.getConnection();
        TableModel t = new TableModel("users");
        t.column("firstName").VARCHAR(20).pkey();
        t.column("lastName").VARCHAR(20).pkey();
        t.column("age").INTEGER();
        String[] ddlArray = dialect.toDropAndCreateDDL(t);
        for (String ddl : ddlArray) try {
            execute(conn, ddl);
        } catch (Exception e) {
        }
        for (int i = 1; i <= 100; i++) execute(conn, "insert into users (firstName, lastName, age) values(?,?,?)", "Foo" + i, "Bar" + i, i);
        Assert.assertEquals(100L, ((Number) queryForObject(conn, "select count(*) from users")).longValue());
        List<Map<String, Object>> users = queryForMapList(conn, dialect.paginAndTrans(2, 10, "select concat(firstName, ' ', lastName) as UserName, age from users where age>?"), 50);
        Assert.assertEquals(10, users.size());
        for (Map<String, Object> map : users) System.out.println("UserName=" + map.get("USERNAME") + ", age=" + map.get("AGE"));
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    ds.close();
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) Connection(java.sql.Connection) SQLException(java.sql.SQLException) Dialect(com.github.drinkjava2.jdialects.Dialect) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TableModel(com.github.drinkjava2.jdialects.model.TableModel) Test(org.junit.Test)

Example 43 with HikariDataSource

use of com.zaxxer.hikari.HikariDataSource in project HikariCP by brettwooldridge.

the class TestSealedConfig method testSealedAccessibleMethods.

@Test
public void testSealedAccessibleMethods() throws SQLException {
    HikariConfig config = newHikariConfig();
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        ds.setConnectionTimeout(5000);
        ds.setValidationTimeout(5000);
        ds.setIdleTimeout(30000);
        ds.setLeakDetectionThreshold(60000);
        ds.setMaxLifetime(1800000);
        ds.setMinimumIdle(5);
        ds.setMaximumPoolSize(8);
        ds.setPassword("password");
        ds.setUsername("username");
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Example 44 with HikariDataSource

use of com.zaxxer.hikari.HikariDataSource in project HikariCP by brettwooldridge.

the class TestSealedConfig method testSealed2.

@Test(expected = IllegalStateException.class)
public void testSealed2() throws SQLException {
    HikariDataSource ds = new HikariDataSource();
    ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource closeable = ds) {
        try (Connection connection = ds.getConnection()) {
            ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
            fail("Exception should have been thrown");
        }
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) Connection(java.sql.Connection) Test(org.junit.Test)

Example 45 with HikariDataSource

use of com.zaxxer.hikari.HikariDataSource in project HikariCP by brettwooldridge.

the class TestSealedConfig method testSealed3.

@Test(expected = IllegalStateException.class)
public void testSealed3() throws SQLException {
    HikariDataSource ds = new HikariDataSource();
    ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource closeable = ds) {
        try (Connection connection = ds.getConnection()) {
            ds.setAutoCommit(false);
            fail("Exception should have been thrown");
        }
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) Connection(java.sql.Connection) Test(org.junit.Test)

Aggregations

HikariDataSource (com.zaxxer.hikari.HikariDataSource)185 HikariConfig (com.zaxxer.hikari.HikariConfig)109 Test (org.junit.Test)106 Connection (java.sql.Connection)61 TestElf.newHikariConfig (com.zaxxer.hikari.pool.TestElf.newHikariConfig)57 SQLException (java.sql.SQLException)33 StubConnection (com.zaxxer.hikari.mocks.StubConnection)30 TestElf.newHikariDataSource (com.zaxxer.hikari.pool.TestElf.newHikariDataSource)18 StubDataSource (com.zaxxer.hikari.mocks.StubDataSource)11 MetricRegistry (com.codahale.metrics.MetricRegistry)9 DataSource (javax.sql.DataSource)8 Statement (java.sql.Statement)7 ArrayList (java.util.ArrayList)7 FacesMessage (javax.faces.application.FacesMessage)7 BoneCPDataSource (com.jolbox.bonecp.BoneCPDataSource)6 PoolInitializationException (com.zaxxer.hikari.pool.HikariPool.PoolInitializationException)6 Properties (java.util.Properties)6 ConnexionTest (connexion.ConnexionTest)5 File (java.io.File)5 FileInputStream (java.io.FileInputStream)5