Search in sources :

Example 21 with PreparedStatement

use of java.sql.PreparedStatement in project tomcat by apache.

the class JDBCStore method keys.

/**
     * Return an array containing the session identifiers of all Sessions
     * currently saved in this Store.  If there are no such Sessions, a
     * zero-length array is returned.
     *
     * @param expiredOnly flag, whether only keys of expired sessions should
     *        be returned
     * @return array containing the list of session IDs
     *
     * @exception IOException if an input/output error occurred
     */
private String[] keys(boolean expiredOnly) throws IOException {
    String[] keys = null;
    synchronized (this) {
        int numberOfTries = 2;
        while (numberOfTries > 0) {
            Connection _conn = getConnection();
            if (_conn == null) {
                return new String[0];
            }
            try {
                String keysSql = "SELECT " + sessionIdCol + " FROM " + sessionTable + " WHERE " + sessionAppCol + " = ?";
                if (expiredOnly) {
                    keysSql += " AND (" + sessionLastAccessedCol + " + " + sessionMaxInactiveCol + " * 1000 < ?)";
                }
                try (PreparedStatement preparedKeysSql = _conn.prepareStatement(keysSql)) {
                    preparedKeysSql.setString(1, getName());
                    if (expiredOnly) {
                        preparedKeysSql.setLong(2, System.currentTimeMillis());
                    }
                    try (ResultSet rst = preparedKeysSql.executeQuery()) {
                        ArrayList<String> tmpkeys = new ArrayList<>();
                        if (rst != null) {
                            while (rst.next()) {
                                tmpkeys.add(rst.getString(1));
                            }
                        }
                        keys = tmpkeys.toArray(new String[tmpkeys.size()]);
                        // Break out after the finally block
                        numberOfTries = 0;
                    }
                }
            } catch (SQLException e) {
                manager.getContext().getLogger().error(sm.getString(getStoreName() + ".SQLException", e));
                keys = new String[0];
                // Close the connection so that it gets reopened next time
                if (dbConnection != null)
                    close(dbConnection);
            } finally {
                release(_conn);
            }
            numberOfTries--;
        }
    }
    return keys;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement)

Example 22 with PreparedStatement

use of java.sql.PreparedStatement in project tomcat by apache.

the class TestSlowQueryReport method testSlowSqlJmx.

@Test
public void testSlowSqlJmx() throws Exception {
    int count = 1;
    Connection con = this.datasource.getConnection();
    for (int i = 0; i < count; i++) {
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(superSlowSql);
        rs.close();
        st.close();
    }
    Map<String, SlowQueryReport.QueryStats> map = SlowQueryReport.getPoolStats(datasource.getPool().getName());
    Assert.assertNotNull(map);
    Assert.assertEquals(1, map.size());
    String key = map.keySet().iterator().next();
    SlowQueryReport.QueryStats stats = map.get(key);
    System.out.println("Stats:" + stats);
    ClientListener listener = new ClientListener();
    ConnectionPool pool = datasource.getPool();
    ManagementFactory.getPlatformMBeanServer().addNotificationListener(new SlowQueryReportJmx().getObjectName(SlowQueryReportJmx.class, pool.getName()), listener, null, null);
    for (int i = 0; i < count; i++) {
        PreparedStatement st = con.prepareStatement(superSlowSql);
        ResultSet rs = st.executeQuery();
        rs.close();
        st.close();
    }
    System.out.println("Stats:" + stats);
    for (int i = 0; i < count; i++) {
        CallableStatement st = con.prepareCall(superSlowSql);
        ResultSet rs = st.executeQuery();
        rs.close();
        st.close();
    }
    System.out.println("Stats:" + stats);
    Assert.assertEquals("Expecting to have received " + (2 * count) + " notifications.", 2 * count, listener.notificationCount.get());
    con.close();
    tearDown();
    //make sure we actually did clean up when the pool closed
    Assert.assertNull(SlowQueryReport.getPoolStats(pool.getName()));
}
Also used : ConnectionPool(org.apache.tomcat.jdbc.pool.ConnectionPool) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) SlowQueryReportJmx(org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReportJmx) SlowQueryReport(org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReport) CallableStatement(java.sql.CallableStatement) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 23 with PreparedStatement

use of java.sql.PreparedStatement in project tomcat by apache.

the class TestStatementCache method testPreparedStatementCache2.

@Test
public void testPreparedStatementCache2() throws Exception {
    init();
    config(false, false, 100);
    Connection con = datasource.getConnection();
    PreparedStatement ps1 = con.prepareStatement("select 1");
    PreparedStatement ps2 = con.prepareStatement("select 1");
    Assert.assertEquals(0, interceptor.getCacheSize().get());
    ps1.close();
    Assert.assertTrue(ps1.isClosed());
    Assert.assertEquals(0, interceptor.getCacheSize().get());
    PreparedStatement ps3 = con.prepareStatement("select 1");
    Assert.assertEquals(0, interceptor.getCacheSize().get());
    ps2.close();
    Assert.assertTrue(ps2.isClosed());
    ps3.close();
    Assert.assertTrue(ps3.isClosed());
    Assert.assertEquals(0, interceptor.getCacheSize().get());
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 24 with PreparedStatement

use of java.sql.PreparedStatement in project sharding-jdbc by dangdangdotcom.

the class DynamicShardingBothForPStatementWithDMLTest method assertDeleteWithoutShardingValue.

@Test(expected = IllegalStateException.class)
public void assertDeleteWithoutShardingValue() throws SQLException, DatabaseUnitException {
    String sql = "DELETE `t_order` WHERE `status` = ?";
    try (Connection connection = getShardingDataSource().getConnection()) {
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, "init");
        preparedStatement.executeUpdate();
    }
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) AbstractShardingBothForPStatementWithDMLTest(com.dangdang.ddframe.rdb.integrate.dbtbl.common.pstatement.AbstractShardingBothForPStatementWithDMLTest) Test(org.junit.Test)

Example 25 with PreparedStatement

use of java.sql.PreparedStatement in project sharding-jdbc by dangdangdotcom.

the class DynamicShardingBothForPStatementWithDMLTest method assertUpdateWithoutShardingValue.

@Test(expected = IllegalStateException.class)
public void assertUpdateWithoutShardingValue() throws SQLException, DatabaseUnitException {
    String sql = "UPDATE `t_order` SET `status` = ? WHERE `status` = ?";
    try (Connection connection = getShardingDataSource().getConnection()) {
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, "updated");
        preparedStatement.setString(2, "init");
        preparedStatement.executeUpdate();
    }
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) AbstractShardingBothForPStatementWithDMLTest(com.dangdang.ddframe.rdb.integrate.dbtbl.common.pstatement.AbstractShardingBothForPStatementWithDMLTest) Test(org.junit.Test)

Aggregations

PreparedStatement (java.sql.PreparedStatement)5900 ResultSet (java.sql.ResultSet)3733 SQLException (java.sql.SQLException)3074 Connection (java.sql.Connection)2478 Test (org.junit.Test)1099 ArrayList (java.util.ArrayList)851 Properties (java.util.Properties)742 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)345 DatabaseException (net.jforum.exceptions.DatabaseException)254 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)251 Timestamp (java.sql.Timestamp)248 Statement (java.sql.Statement)243 BigDecimal (java.math.BigDecimal)206 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)174 HashMap (java.util.HashMap)169 DbConnection (com.zimbra.cs.db.DbPool.DbConnection)165 List (java.util.List)136 Date (java.util.Date)124 Date (java.sql.Date)123 BaseTest (org.apache.phoenix.query.BaseTest)114