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;
}
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()));
}
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());
}
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();
}
}
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();
}
}
Aggregations