Search in sources :

Example 1 with RefreshableDataSource

use of com.ctrip.platform.dal.dao.datasource.RefreshableDataSource in project dal by ctripcorp.

the class DalConnectionPoolTest method testValidation.

@Test
public void testValidation() throws Exception {
    DataSourceConfigure config = new DataSourceConfigure("test");
    config.setDriverClass("com.mysql.jdbc.Driver");
    config.setConnectionUrl("jdbc:mysql://10.32.20.117:3306/llj_test");
    config.setUserName("root");
    config.setPassword("!QAZ@WSX1qaz2wsx");
    config.setProperty(DataSourceConfigureConstants.VALIDATORCLASSNAME, "com.ctrip.platform.dal.dao.datasource.tomcat.MockValidator");
    config.setProperty(DataSourceConfigureConstants.VALIDATIONINTERVAL, "5000");
    RefreshableDataSource dataSource = new RefreshableDataSource(config.getName(), config);
    for (int i = 0; i < 20; i++) {
        try (Connection connection = dataSource.getConnection()) {
            System.out.println(connection.getMetaData().getURL());
        } catch (Throwable t) {
            System.out.println("exception type: " + t.getClass());
            t.printStackTrace();
        } finally {
            TimeUnit.SECONDS.sleep(1);
        }
    }
}
Also used : RefreshableDataSource(com.ctrip.platform.dal.dao.datasource.RefreshableDataSource) Connection(java.sql.Connection) DataSourceConfigure(com.ctrip.platform.dal.dao.configure.DataSourceConfigure) Test(org.junit.Test)

Example 2 with RefreshableDataSource

use of com.ctrip.platform.dal.dao.datasource.RefreshableDataSource in project dal by ctripcorp.

the class WaitTimeoutTest method test.

@Test
public void test() throws Exception {
    DataSourceConfigure config = new DataSourceConfigure("test");
    config.setDriverClass("com.mysql.jdbc.Driver");
    config.setConnectionUrl("jdbc:mysql://10.32.20.117:3306/llj_test?useUnicode=true&characterEncoding=UTF-8");
    config.setUserName("root");
    config.setPassword("!QAZ@WSX1qaz2wsx");
    config.setProperty(DataSourceConfigureConstants.VALIDATORCLASSNAME, "com.ctrip.platform.dal.dao.util.MockValidator");
    config.setProperty(DataSourceConfigureConstants.VALIDATIONINTERVAL, "2000");
    RefreshableDataSource dataSource = new RefreshableDataSource(config.getName(), config);
    while (true) {
        try (Connection connection = dataSource.getConnection()) {
            System.err.println("connId: " + connection.unwrap(MySQLConnection.class).getId());
            try (Statement statement = connection.createStatement()) {
                try (ResultSet rs = statement.executeQuery("show session variables like 'wait_timeout'")) {
                    if (rs != null && rs.next())
                        System.err.println("wait_timeout: " + rs.getInt(2));
                }
            }
        } catch (Throwable t) {
            t.printStackTrace();
        } finally {
            TimeUnit.MILLISECONDS.sleep(500);
        }
    }
}
Also used : RefreshableDataSource(com.ctrip.platform.dal.dao.datasource.RefreshableDataSource) Statement(java.sql.Statement) MySQLConnection(com.mysql.jdbc.MySQLConnection) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) DataSourceConfigure(com.ctrip.platform.dal.dao.configure.DataSourceConfigure) MySQLConnection(com.mysql.jdbc.MySQLConnection) Test(org.junit.Test)

Example 3 with RefreshableDataSource

use of com.ctrip.platform.dal.dao.datasource.RefreshableDataSource in project dal by ctripcorp.

the class DalConnectionTest method testDiscardConnection.

// @Test
public void testDiscardConnection() throws SQLException {
    Properties p = new Properties();
    p.setProperty(USER_NAME, "tt_daltest_3");
    p.setProperty(PASSWORD, "R0NeM30TcbAfWz7aHoWx");
    p.setProperty(CONNECTION_URL, "jdbc:mysql://10.2.22.223:55777/dalservice2db?useUnicode=true&characterEncoding=UTF-8");
    p.setProperty(DRIVER_CLASS_NAME, "com.mysql.jdbc.Driver");
    DataSourceConfigure c = new DataSourceConfigure("dalservice2db", p);
    RefreshableDataSource ds = new RefreshableDataSource("dalservice2db", c);
    PooledConnection pConn = null;
    try (Connection connection = ds.getConnection()) {
        pConn = connection.unwrap(PooledConnection.class);
        System.out.println("discarded0: " + pConn.isDiscarded());
        try {
            // PreparedStatement ps = connection.prepareStatement("select * from person5756 where id > 0");
            // ResultSet rs = ps.executeQuery();
            // System.out.println("result set: " + rs);
            PreparedStatement ps = connection.prepareStatement("update person33 set name = 'testDiscard' where id > 0");
            int rows = ps.executeUpdate();
            System.out.println("affected rows: " + rows);
            System.out.println("discarded1: " + pConn.isDiscarded());
        } catch (SQLException e) {
            System.out.println("error: " + e);
            System.out.println("error: " + e.getErrorCode());
            e.printStackTrace();
            System.out.println("discarded2: " + pConn.isDiscarded());
            throw e;
        } finally {
            // connection.close();
            System.out.println("discarded3: " + pConn.isDiscarded());
        }
    } catch (Throwable t) {
    // ignore
    }
    if (pConn != null)
        System.out.println("discarded4: " + pConn.isDiscarded());
}
Also used : RefreshableDataSource(com.ctrip.platform.dal.dao.datasource.RefreshableDataSource) PooledConnection(org.apache.tomcat.jdbc.pool.PooledConnection) PooledConnection(org.apache.tomcat.jdbc.pool.PooledConnection) Properties(java.util.Properties) DataSourceConfigure(com.ctrip.platform.dal.dao.configure.DataSourceConfigure)

Aggregations

DataSourceConfigure (com.ctrip.platform.dal.dao.configure.DataSourceConfigure)3 RefreshableDataSource (com.ctrip.platform.dal.dao.datasource.RefreshableDataSource)3 Connection (java.sql.Connection)2 Test (org.junit.Test)2 MySQLConnection (com.mysql.jdbc.MySQLConnection)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 Properties (java.util.Properties)1 PooledConnection (org.apache.tomcat.jdbc.pool.PooledConnection)1