/** * Save the specified Session into this Store. Any previously saved information for the associated * session identifier is replaced. * * @param session Session to be saved * @exception IOException if an input/output error occurs */ @Override public void save(Session session) throws IOException { // Open an output stream to the specified pathname, if any File file = file(session.getIdInternal()); if (file == null) { return; } if (manager.getContext().getLogger().isDebugEnabled()) { manager .getContext() .getLogger() .debug( sm.getString( getStoreName() + ".saving", session.getIdInternal(), file.getAbsolutePath())); } try (FileOutputStream fos = new FileOutputStream(file.getAbsolutePath()); ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(fos))) { ((StandardSession) session).writeObjectData(oos); } }
/** * Save any currently active sessions in the appropriate persistence mechanism, if any. If * persistence is not supported, this method returns without doing anything. * * @exception IOException if an input/output error occurs */ public void unload() throws IOException { if (debug >= 1) log("Unloading persisted sessions"); // Open an output stream to the specified pathname, if any File file = file(); if (file == null) return; if (debug >= 1) log(sm.getString("standardManager.unloading", pathname)); FileOutputStream fos = null; ObjectOutputStream oos = null; try { fos = new FileOutputStream(file.getAbsolutePath()); oos = new ObjectOutputStream(new BufferedOutputStream(fos)); } catch (IOException e) { log(sm.getString("standardManager.unloading.ioe", e), e); if (oos != null) { try { oos.close(); } catch (IOException f) {; } oos = null; } throw e; } // Write the number of active sessions, followed by the details ArrayList list = new ArrayList(); synchronized (sessions) { if (debug >= 1) log("Unloading " + sessions.size() + " sessions"); try { oos.writeObject(new Integer(sessions.size())); Iterator elements = sessions.values().iterator(); while (elements.hasNext()) { StandardSession session = (StandardSession) elements.next(); list.add(session); session.passivate(); session.writeObjectData(oos); } } catch (IOException e) { log(sm.getString("standardManager.unloading.ioe", e), e); if (oos != null) { try { oos.close(); } catch (IOException f) {; } oos = null; } throw e; } } // Flush and close the output stream try { oos.flush(); oos.close(); oos = null; } catch (IOException e) { if (oos != null) { try { oos.close(); } catch (IOException f) {; } oos = null; } throw e; } // Expire all the sessions we just wrote if (debug >= 1) log("Expiring " + list.size() + " persisted sessions"); Iterator expires = list.iterator(); while (expires.hasNext()) { StandardSession session = (StandardSession) expires.next(); try { session.expire(false); } catch (Throwable t) {; } } if (debug >= 1) log("Unloading complete"); }
/** * Save a session to the Store. * * @param session the session to be stored * @exception IOException if an input/output error occurs */ public void save(Session session) throws IOException { String saveSql = "INSERT INTO " + sessionTable + " (" + sessionIdCol + ", " + sessionAppCol + ", " + sessionDataCol + ", " + sessionValidCol + ", " + sessionMaxInactiveCol + ", " + sessionLastAccessedCol + ") VALUES (?, ?, ?, ?, ?, ?)"; ObjectOutputStream oos = null; ByteArrayOutputStream bos = null; ByteArrayInputStream bis = null; InputStream in = null; synchronized (this) { Connection _conn = getConnection(); if (_conn == null) { return; } // If sessions already exist in DB, remove and insert again. // TODO: // * Check if ID exists in database and if so use UPDATE. remove(session.getId()); try { bos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(new BufferedOutputStream(bos)); ((StandardSession) session).writeObjectData(oos); oos.close(); byte[] obs = bos.toByteArray(); int size = obs.length; bis = new ByteArrayInputStream(obs, 0, size); in = new BufferedInputStream(bis, size); if (preparedSaveSql == null) { preparedSaveSql = _conn.prepareStatement(saveSql); } preparedSaveSql.setString(1, session.getId()); preparedSaveSql.setString(2, getName()); preparedSaveSql.setBinaryStream(3, in, size); preparedSaveSql.setString(4, session.isValid() ? "1" : "0"); preparedSaveSql.setInt(5, session.getMaxInactiveInterval()); preparedSaveSql.setLong(6, session.getLastAccessedTime()); preparedSaveSql.execute(); } catch (SQLException e) { log(sm.getString(getStoreName() + ".SQLException", e)); } catch (IOException e) {; } finally { if (bis != null) { bis.close(); } if (in != null) { in.close(); } release(_conn); } } if (debug > 0) { log(sm.getString(getStoreName() + ".saving", session.getId(), sessionTable)); } }