Search in sources :

Example 31 with DruidPooledConnection

use of com.alibaba.druid.pool.DruidPooledConnection in project druid by alibaba.

the class ConnectionTest4 method test_preCall_error.

public void test_preCall_error() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);
    conn.getConnection().close();
    {
        SQLException error = null;
        try {
            conn.prepareCall("SELECT 1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
        } catch (SQLException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    conn.close();
}
Also used : SQLException(java.sql.SQLException) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection)

Example 32 with DruidPooledConnection

use of com.alibaba.druid.pool.DruidPooledConnection in project druid by alibaba.

the class ConnectionTest4 method test_addListenerError2.

public void test_addListenerError2() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);
    conn.close();
    {
        Exception error = null;
        try {
            conn.addStatementEventListener(null);
        } catch (IllegalStateException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
}
Also used : DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) SQLException(java.sql.SQLException)

Example 33 with DruidPooledConnection

use of com.alibaba.druid.pool.DruidPooledConnection in project druid by alibaba.

the class ConnectionTest4 method test_prepareStatement_error.

public void test_prepareStatement_error() throws Exception {
    DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);
    conn.getConnection().close();
    {
        SQLException error = null;
        try {
            conn.prepareStatement("SELECT 1");
        } catch (SQLException ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    conn.close();
}
Also used : SQLException(java.sql.SQLException) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection)

Example 34 with DruidPooledConnection

use of com.alibaba.druid.pool.DruidPooledConnection in project druid by alibaba.

the class MySqlValidConnectionChecker method isValidConnection.

public boolean isValidConnection(Connection conn, String validateQuery, int validationQueryTimeout) throws Exception {
    if (conn.isClosed()) {
        return false;
    }
    if (usePingMethod) {
        if (conn instanceof DruidPooledConnection) {
            conn = ((DruidPooledConnection) conn).getConnection();
        }
        if (conn instanceof ConnectionProxy) {
            conn = ((ConnectionProxy) conn).getRawObject();
        }
        if (clazz.isAssignableFrom(conn.getClass())) {
            if (validationQueryTimeout <= 0) {
                validationQueryTimeout = DEFAULT_VALIDATION_QUERY_TIMEOUT;
            }
            try {
                ping.invoke(conn, true, validationQueryTimeout * 1000);
            } catch (InvocationTargetException e) {
                Throwable cause = e.getCause();
                if (cause instanceof SQLException) {
                    throw (SQLException) cause;
                }
                throw e;
            }
            return true;
        }
    }
    String query = validateQuery;
    if (validateQuery == null || validateQuery.isEmpty()) {
        query = DEFAULT_VALIDATION_QUERY;
    }
    Statement stmt = null;
    ResultSet rs = null;
    try {
        stmt = conn.createStatement();
        if (validationQueryTimeout > 0) {
            stmt.setQueryTimeout(validationQueryTimeout);
        }
        rs = stmt.executeQuery(query);
        return true;
    } finally {
        JdbcUtils.close(rs);
        JdbcUtils.close(stmt);
    }
}
Also used : SQLException(java.sql.SQLException) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) ConnectionProxy(com.alibaba.druid.proxy.jdbc.ConnectionProxy) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 35 with DruidPooledConnection

use of com.alibaba.druid.pool.DruidPooledConnection in project druid by alibaba.

the class EncodingConvertFilterTest2 method test_stat.

public void test_stat() throws Exception {
    Assert.assertTrue(dataSource.isInited());
    EncodingConvertFilter filter = (EncodingConvertFilter) dataSource.getProxyFilters().get(0);
    DruidPooledConnection conn = dataSource.getConnection();
    final String PARAM_VALUE = "中国";
    CallableStatement stmt = conn.prepareCall("select ?");
    stmt.setString(1, PARAM_VALUE);
    MockCallableStatement raw = stmt.unwrap(MockCallableStatement.class);
    stmt.execute();
    String param1 = (String) raw.getParameters().get(0);
    String C_TEXT = new String(param1.getBytes(SERVER_ENCODING), CLIENT_ENCODING);
    Assert.assertEquals(PARAM_VALUE, C_TEXT);
    Assert.assertFalse(param1.equals(PARAM_VALUE));
    MyResultSet rawRs = new MyResultSet(raw);
    rawRs.setValue(filter.encode((ConnectionProxy) conn.getConnection(), text));
    raw.getOutParameters().add(rawRs);
    ResultSet rs = (ResultSet) stmt.getObject(1);
    rs.next();
    Assert.assertEquals(text, rs.getString(1));
    Assert.assertEquals(text, rs.getString("1"));
    Assert.assertEquals(text, rs.getObject(1));
    Assert.assertEquals(text, rs.getObject("1"));
    Assert.assertEquals(text, rs.getObject(1, Collections.<String, Class<?>>emptyMap()));
    Assert.assertEquals(text, rs.getObject("1", Collections.<String, Class<?>>emptyMap()));
    Assert.assertEquals(text, rs.getString(2));
    Assert.assertEquals(text, rs.getString("2"));
    Assert.assertEquals(text, rs.getObject(2));
    Assert.assertEquals(text, rs.getObject("2"));
    Assert.assertEquals(text, rs.getObject(2, Collections.<String, Class<?>>emptyMap()));
    Assert.assertEquals(text, rs.getObject("2", Collections.<String, Class<?>>emptyMap()));
    Assert.assertEquals(text, rs.getString(3));
    Assert.assertEquals(text, rs.getString("3"));
    Assert.assertEquals(text, rs.getObject(3));
    Assert.assertEquals(text, rs.getObject("3"));
    Assert.assertEquals(text, rs.getObject(3, Collections.<String, Class<?>>emptyMap()));
    Assert.assertEquals(text, rs.getObject("3", Collections.<String, Class<?>>emptyMap()));
    Assert.assertEquals(text, rs.getString(4));
    Assert.assertEquals(text, rs.getString("4"));
    Assert.assertEquals(text, rs.getObject(4));
    Assert.assertEquals(text, rs.getObject("4"));
    Assert.assertEquals(text, rs.getObject(4, Collections.<String, Class<?>>emptyMap()));
    Assert.assertEquals(text, rs.getObject("4", Collections.<String, Class<?>>emptyMap()));
    stmt.registerOutParameter(2, Types.VARCHAR);
    stmt.registerOutParameter(3, Types.CLOB);
    raw.getOutParameters().add(param1);
    raw.getOutParameters().add(param1);
    Assert.assertEquals(C_TEXT, stmt.getString(4));
    Assert.assertEquals(C_TEXT, stmt.getString("4"));
    Assert.assertEquals(C_TEXT, stmt.getObject(4));
    Assert.assertEquals(C_TEXT, stmt.getObject("4"));
    Assert.assertEquals(C_TEXT, stmt.getObject(4, Collections.<String, Class<?>>emptyMap()));
    Assert.assertEquals(C_TEXT, stmt.getObject("4", Collections.<String, Class<?>>emptyMap()));
    Assert.assertEquals(C_TEXT, stmt.getString(5));
    Assert.assertEquals(C_TEXT, stmt.getString("5"));
    Assert.assertEquals(C_TEXT, stmt.getObject(5));
    Assert.assertEquals(C_TEXT, stmt.getObject("5"));
    Assert.assertEquals(C_TEXT, stmt.getObject(5, Collections.<String, Class<?>>emptyMap()));
    Assert.assertEquals(C_TEXT, stmt.getObject("5", Collections.<String, Class<?>>emptyMap()));
    stmt.setObject(1, C_TEXT);
    Assert.assertEquals(param1, raw.getParameters().get(0));
    stmt.setObject(2, new StringReader(C_TEXT));
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(1)));
    stmt.setCharacterStream(3, new StringReader(C_TEXT));
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(2)));
    stmt.setCharacterStream(4, new StringReader(C_TEXT), C_TEXT.length());
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(3)));
    stmt.setCharacterStream(5, new StringReader(C_TEXT), (long) C_TEXT.length());
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(4)));
    stmt.setObject(6, C_TEXT, Types.VARCHAR);
    Assert.assertEquals(param1, raw.getParameters().get(5));
    stmt.setObject(7, new StringReader(C_TEXT), Types.VARCHAR);
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(6)));
    stmt.setObject(8, C_TEXT, Types.VARCHAR, 0);
    Assert.assertEquals(param1, raw.getParameters().get(7));
    stmt.setObject(9, new StringReader(C_TEXT), Types.VARCHAR, 0);
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(8)));
    stmt.setObject(10, 1, Types.INTEGER);
    Assert.assertEquals(1, raw.getParameters().get(9));
    stmt.setObject(11, 2, Types.INTEGER, 0);
    Assert.assertEquals(2, raw.getParameters().get(10));
    stmt.setObject(12, 3);
    Assert.assertEquals(3, raw.getParameters().get(11));
    stmt.setObject("13", C_TEXT, Types.VARCHAR);
    Assert.assertEquals(param1, raw.getParameters().get(12));
    stmt.setObject("14", new StringReader(C_TEXT), Types.VARCHAR);
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(13)));
    stmt.setObject("15", C_TEXT, Types.VARCHAR, 0);
    Assert.assertEquals(param1, raw.getParameters().get(14));
    stmt.setObject("16", new StringReader(C_TEXT), Types.VARCHAR, 0);
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(15)));
    stmt.setObject("17", 1, Types.INTEGER);
    Assert.assertEquals(1, raw.getParameters().get(16));
    stmt.setObject("18", 2, Types.INTEGER, 0);
    Assert.assertEquals(2, raw.getParameters().get(17));
    stmt.setObject("19", 3);
    Assert.assertEquals(3, raw.getParameters().get(18));
    stmt.setCharacterStream("20", new StringReader(C_TEXT));
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(19)));
    stmt.setCharacterStream("21", new StringReader(C_TEXT), C_TEXT.length());
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(20)));
    stmt.setCharacterStream("22", new StringReader(C_TEXT), (long) C_TEXT.length());
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(21)));
    stmt.setObject("23", C_TEXT);
    Assert.assertEquals(param1, raw.getParameters().get(22));
    stmt.setObject("24", new StringReader(C_TEXT));
    Assert.assertEquals(param1, Utils.read((Reader) raw.getParameters().get(23)));
    stmt.setObject("25", 1, Types.INTEGER);
    Assert.assertEquals(1, raw.getParameters().get(24));
    stmt.setObject("26", 2, Types.INTEGER, 0);
    Assert.assertEquals(2, raw.getParameters().get(25));
    stmt.setObject("27", 3);
    Assert.assertEquals(3, raw.getParameters().get(26));
    rs.close();
    stmt.close();
    conn.close();
}
Also used : MockCallableStatement(com.alibaba.druid.mock.MockCallableStatement) DruidPooledConnection(com.alibaba.druid.pool.DruidPooledConnection) MockCallableStatement(com.alibaba.druid.mock.MockCallableStatement) CallableStatement(java.sql.CallableStatement) MockResultSet(com.alibaba.druid.mock.MockResultSet) ResultSet(java.sql.ResultSet) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) EncodingConvertFilter(com.alibaba.druid.filter.encoding.EncodingConvertFilter) ConnectionProxy(com.alibaba.druid.proxy.jdbc.ConnectionProxy)

Aggregations

DruidPooledConnection (com.alibaba.druid.pool.DruidPooledConnection)152 SQLException (java.sql.SQLException)99 PreparedStatement (java.sql.PreparedStatement)61 MockConnection (com.alibaba.druid.mock.MockConnection)49 Statement (java.sql.Statement)46 Connection (java.sql.Connection)20 MockPreparedStatement (com.alibaba.druid.mock.MockPreparedStatement)10 ConnectionProxy (com.alibaba.druid.proxy.jdbc.ConnectionProxy)7 DruidConnectionHolder (com.alibaba.druid.pool.DruidConnectionHolder)5 ResultSet (java.sql.ResultSet)5 DruidPooledPreparedStatement (com.alibaba.druid.pool.DruidPooledPreparedStatement)4 PreparedStatementHolder (com.alibaba.druid.pool.PreparedStatementHolder)4 Properties (java.util.Properties)4 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)3 EncodingConvertFilter (com.alibaba.druid.filter.encoding.EncodingConvertFilter)2 MockCallableStatement (com.alibaba.druid.mock.MockCallableStatement)2 MockResultSet (com.alibaba.druid.mock.MockResultSet)2 PreparedStatementKey (com.alibaba.druid.pool.DruidPooledPreparedStatement.PreparedStatementKey)2 PreparedStatementPool (com.alibaba.druid.pool.PreparedStatementPool)2 CountDownLatch (java.util.concurrent.CountDownLatch)2