Changeset 7900


Ignore:
Timestamp:
Jan 21, 2021, 8:18:54 AM (2 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #2238: Mark sessions without a logout time as inactive when BASE is starting

Location:
trunk/src/core/net/sf/basedb/core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/core/Application.java

    r7899 r7900  
    564564        // BouncyCastle
    565565        if (Security.getProvider("BC") == null) Security.addProvider(new BouncyCastleProvider());
     566       
     567        // Auto-logout existing session without logout time
     568        if (options.autoLogoutSessions)
     569        {
     570          int numSessions = Session.autoLogout();
     571          log.info(numSessions + " sessions without a logout time was automatically logged out.");
     572        }
    566573       
    567574        // Initialize extensions system
  • trunk/src/core/net/sf/basedb/core/Session.java

    r7413 r7900  
    2424
    2525import net.sf.basedb.core.data.SessionData;
     26import net.sf.basedb.core.hibernate.TypeWrapper;
    2627import net.sf.basedb.core.query.Restrictions;
    2728import net.sf.basedb.core.query.Hql;
     
    9899    return query;
    99100  }
     101 
     102  /**
     103    Cleanup method that sets a logout-time on all sessions
     104    that doesn't have a logout time.
     105    @return The number of sessions updated
     106    @since 3.18
     107  */
     108  static int autoLogout()
     109  {
     110    org.hibernate.Transaction tx = null;
     111    org.hibernate.Session session = null;
     112    int numUpdated = 0;
     113    try
     114    {
     115      session = HibernateUtil.newSession();
     116      tx = HibernateUtil.newTransaction(session);
     117     
     118      // Update 'location'
     119      org.hibernate.query.Query<?> query = HibernateUtil.createQuery(session,
     120        "UPDATE SessionData s " +
     121        "SET s.logoutTime = :now " +
     122        "WHERE s.logoutTime IS NULL");
     123      query.setParameter("now", new Date(), TypeWrapper.H_TIMESTAMP);
     124      numUpdated = query.executeUpdate();
     125      HibernateUtil.commit(tx);
     126    }
     127    catch (Exception ex)
     128    {
     129      // This should now be a fatal error, log a warning only
     130      Application.getLogger().warn("Could not auto-logout existing sessions", ex);
     131    }
     132    finally
     133    {
     134      HibernateUtil.close(session);
     135    }
     136    return numUpdated;
     137  }
    100138
    101139  Session(SessionData sessionData)
Note: See TracChangeset for help on using the changeset viewer.