Search in sources :

Example 1 with ResultSet

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();
    }
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) HashSet(java.util.HashSet)

Example 2 with ResultSet

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();
    }
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 3 with ResultSet

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();
    }
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 4 with ResultSet

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;
    }
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) HashSet(java.util.HashSet)

Example 5 with ResultSet

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();
}
Also used : ClassLoadingObjectInputStream(org.eclipse.jetty.util.ClassLoadingObjectInputStream) ClassLoadingObjectInputStream(org.eclipse.jetty.util.ClassLoadingObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) AtomicReference(java.util.concurrent.atomic.AtomicReference) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet)

Aggregations

ResultSet (java.sql.ResultSet)16614 PreparedStatement (java.sql.PreparedStatement)9996 SQLException (java.sql.SQLException)7083 Connection (java.sql.Connection)6929 Statement (java.sql.Statement)4787 Test (org.junit.Test)3656 ArrayList (java.util.ArrayList)2584 Properties (java.util.Properties)1232 HashMap (java.util.HashMap)681 ResultSetMetaData (java.sql.ResultSetMetaData)660 CallableStatement (java.sql.CallableStatement)580 DatabaseMetaData (java.sql.DatabaseMetaData)512 List (java.util.List)471 IOException (java.io.IOException)465 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)414 Map (java.util.Map)409 Timestamp (java.sql.Timestamp)384 BigDecimal (java.math.BigDecimal)358 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)292 HashSet (java.util.HashSet)282