use of java.sql.ResultSet in project jetty.project by eclipse.
the class JdbcTestHelper method getSessionIds.
public static Set<String> getSessionIds() throws Exception {
HashSet<String> ids = new HashSet<String>();
Class.forName(DRIVER_CLASS);
Connection con = null;
try {
con = DriverManager.getConnection(DEFAULT_CONNECTION_URL);
PreparedStatement statement = con.prepareStatement("select " + ID_COL + " from " + TABLE);
ResultSet result = statement.executeQuery();
while (result.next()) {
ids.add(result.getString(1));
}
return ids;
} finally {
if (con != null)
con.close();
}
}
use of java.sql.ResultSet in project jetty.project by eclipse.
the class JdbcTestHelper method existsInSessionTable.
public static boolean existsInSessionTable(String id, boolean verbose) throws Exception {
Class.forName(DRIVER_CLASS);
Connection con = null;
try {
con = DriverManager.getConnection(DEFAULT_CONNECTION_URL);
PreparedStatement statement = con.prepareStatement("select * from " + TABLE + " where " + ID_COL + " = ?");
statement.setString(1, id);
ResultSet result = statement.executeQuery();
if (verbose) {
boolean results = false;
while (result.next()) {
results = true;
}
return results;
} else
return result.next();
} finally {
if (con != null)
con.close();
}
}
use of java.sql.ResultSet in project sharding-jdbc by dangdangdotcom.
the class AbstractShardingBothDataBasesAndTablesSpringDBUnitTest method selectData.
private void selectData() throws SQLException {
String sql = "SELECT i.order_id, i.order_item_id FROM `t_order` o JOIN `t_order_item` i ON o.user_id = i.user_id AND o.order_id = i.order_id" + " WHERE o.`user_id` = ? AND o.`order_id` = ? AND i.`order_id` = ? ORDER BY i.order_item_id DESC";
try (Connection connection = getShardingDataSource().getConnection()) {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, 1);
preparedStatement.setInt(2, 1);
preparedStatement.setInt(3, 1);
ResultSet resultSet = preparedStatement.executeQuery();
int count = 0;
while (resultSet.next()) {
if (0 == count) {
assertThat(resultSet.getInt(1), is(1));
assertThat(resultSet.getInt(2), is(5));
} else if (1 == count) {
assertThat(resultSet.getInt(1), is(1));
assertThat(resultSet.getInt(2), is(1));
}
count++;
}
preparedStatement.close();
}
}
use of java.sql.ResultSet in project jetty.project by eclipse.
the class JDBCSessionDataStore method doGetExpired.
/**
* @see org.eclipse.jetty.server.session.SessionDataStore#getExpired(Set)
*/
@Override
public Set<String> doGetExpired(Set<String> candidates) {
if (LOG.isDebugEnabled())
LOG.debug("Getting expired sessions " + System.currentTimeMillis());
long now = System.currentTimeMillis();
Set<String> expiredSessionKeys = new HashSet<>();
try (Connection connection = _dbAdaptor.getConnection()) {
connection.setAutoCommit(true);
/*
* 1. Select sessions managed by this node for our context that have expired
*/
long upperBound = now;
if (LOG.isDebugEnabled())
LOG.debug("{}- Pass 1: Searching for sessions for context {} managed by me {} and expired before {}", _context.getCanonicalContextPath(), _context.getWorkerName(), upperBound);
try (PreparedStatement statement = _sessionTableSchema.getExpiredSessionsStatement(connection, _context.getCanonicalContextPath(), _context.getVhost(), upperBound)) {
try (ResultSet result = statement.executeQuery()) {
while (result.next()) {
String sessionId = result.getString(_sessionTableSchema.getIdColumn());
long exp = result.getLong(_sessionTableSchema.getExpiryTimeColumn());
expiredSessionKeys.add(sessionId);
if (LOG.isDebugEnabled())
LOG.debug(_context.getCanonicalContextPath() + "- Found expired sessionId=" + sessionId);
}
}
}
/*
* 2. Select sessions for any node or context that have expired
* at least 1 graceperiod since the last expiry check. If we haven't done previous expiry checks, then check
* those that have expired at least 3 graceperiod ago.
*/
try (PreparedStatement selectExpiredSessions = _sessionTableSchema.getAllAncientExpiredSessionsStatement(connection)) {
if (_lastExpiryCheckTime <= 0)
upperBound = (now - (3 * (1000L * _gracePeriodSec)));
else
upperBound = _lastExpiryCheckTime - (1000L * _gracePeriodSec);
if (LOG.isDebugEnabled())
LOG.debug("{}- Pass 2: Searching for sessions expired before {}", _context.getWorkerName(), upperBound);
selectExpiredSessions.setLong(1, upperBound);
try (ResultSet result = selectExpiredSessions.executeQuery()) {
while (result.next()) {
String sessionId = result.getString(_sessionTableSchema.getIdColumn());
String ctxtpth = result.getString(_sessionTableSchema.getContextPathColumn());
String vh = result.getString(_sessionTableSchema.getVirtualHostColumn());
expiredSessionKeys.add(sessionId);
if (LOG.isDebugEnabled())
LOG.debug("{}- Found expired sessionId=", _context.getWorkerName(), sessionId);
}
}
}
Set<String> notExpiredInDB = new HashSet<>();
for (String k : candidates) {
//expiry time was updated
if (!expiredSessionKeys.contains(k))
notExpiredInDB.add(k);
}
if (!notExpiredInDB.isEmpty()) {
//we have some sessions to check
try (PreparedStatement checkSessionExists = _sessionTableSchema.getCheckSessionExistsStatement(connection, _context.getCanonicalContextPath())) {
for (String k : notExpiredInDB) {
_sessionTableSchema.fillCheckSessionExistsStatement(checkSessionExists, k, _context);
try (ResultSet result = checkSessionExists.executeQuery()) {
if (!result.next()) {
//session doesn't exist any more, can be expired
expiredSessionKeys.add(k);
}
//else its expiry time has not been reached
} catch (Exception e) {
LOG.warn("Problem checking if potentially expired session {} exists in db", k, e);
}
}
}
}
return expiredSessionKeys;
} catch (Exception e) {
LOG.warn(e);
//return whatever we got
return expiredSessionKeys;
}
}
use of java.sql.ResultSet in project jetty.project by eclipse.
the class JDBCSessionDataStore method load.
/**
* @see org.eclipse.jetty.server.session.SessionDataStore#load(java.lang.String)
*/
@Override
public SessionData load(String id) throws Exception {
final AtomicReference<SessionData> reference = new AtomicReference<SessionData>();
final AtomicReference<Exception> exception = new AtomicReference<Exception>();
Runnable r = new Runnable() {
public void run() {
try (Connection connection = _dbAdaptor.getConnection();
PreparedStatement statement = _sessionTableSchema.getLoadStatement(connection, id, _context);
ResultSet result = statement.executeQuery()) {
SessionData data = null;
if (result.next()) {
data = newSessionData(id, result.getLong(_sessionTableSchema.getCreateTimeColumn()), result.getLong(_sessionTableSchema.getAccessTimeColumn()), result.getLong(_sessionTableSchema.getLastAccessTimeColumn()), result.getLong(_sessionTableSchema.getMaxIntervalColumn()));
data.setCookieSet(result.getLong(_sessionTableSchema.getCookieTimeColumn()));
data.setLastNode(result.getString(_sessionTableSchema.getLastNodeColumn()));
data.setLastSaved(result.getLong(_sessionTableSchema.getLastSavedTimeColumn()));
data.setExpiry(result.getLong(_sessionTableSchema.getExpiryTimeColumn()));
//TODO needed? this is part of the key now
data.setContextPath(result.getString(_sessionTableSchema.getContextPathColumn()));
//TODO needed??? this is part of the key now
data.setVhost(result.getString(_sessionTableSchema.getVirtualHostColumn()));
try (InputStream is = _dbAdaptor.getBlobInputStream(result, _sessionTableSchema.getMapColumn());
ClassLoadingObjectInputStream ois = new ClassLoadingObjectInputStream(is)) {
Object o = ois.readObject();
data.putAllAttributes((Map<String, Object>) o);
} catch (Exception e) {
throw new UnreadableSessionDataException(id, _context, e);
}
if (LOG.isDebugEnabled())
LOG.debug("LOADED session {}", data);
} else if (LOG.isDebugEnabled())
LOG.debug("No session {}", id);
reference.set(data);
} catch (Exception e) {
exception.set(e);
}
}
};
//ensure this runs with context classloader set
_context.run(r);
if (exception.get() != null)
throw exception.get();
return reference.get();
}
Aggregations