IDEMPIERE-5256 Add Cache to MSession (#1282)

This commit is contained in:
igorpojzl 2022-04-07 03:33:41 +02:00 committed by GitHub
parent fb6aba0287
commit ef929dfae5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 107 additions and 41 deletions

View File

@ -20,13 +20,14 @@ import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.ResultSet;
import java.util.Properties;
import java.util.Vector;
import java.util.logging.Level;
import org.compiere.Adempiere;
import org.compiere.util.Env;
import org.compiere.util.TimeUtil;
import org.compiere.util.WebUtil;
import org.idempiere.cache.ImmutableIntPOCache;
import org.idempiere.cache.ImmutablePOSupport;
/**
* Session Model.
@ -39,7 +40,7 @@ import org.compiere.util.WebUtil;
* <li>BF [ 1810182 ] Session lost after cache reset
* <li>BF [ 1892156 ] MSession is not really cached
*/
public class MSession extends X_AD_Session
public class MSession extends X_AD_Session implements ImmutablePOSupport
{
/**
*
@ -52,30 +53,47 @@ public class MSession extends X_AD_Session
* @param ctx context
* @param createNew create if not found
* @return session session
* @deprecated get (Properties ctx, boolean createNew, boolean isCache) for caching support
*/
public static MSession get (Properties ctx, boolean createNew)
{
return get(ctx, createNew, false);
} // get
/**
* Get existing or create local session
* @param ctx context
* @param createNew create if not found
* @param isImmutable return Immutable Session Object (from Cache)
* @return session session
*/
public static MSession get (Properties ctx, boolean createNew, boolean isImmutable)
{
int AD_Session_ID = Env.getContextAsInt(ctx, Env.AD_SESSION_ID);
MSession session = null;
MSession session = isImmutable ? s_sessions.get(ctx, AD_Session_ID, e -> new MSession(ctx, e)) : new MSession(ctx, AD_Session_ID, null);
// Try to load
if (AD_Session_ID > 0 && s_sessions.contains(AD_Session_ID))
if (isImmutable && session == null && AD_Session_ID > 0)
{
session = new MSession(ctx, AD_Session_ID, null);
if (session.get_ID() != AD_Session_ID)
if (session.get_ID () == AD_Session_ID)
{
s_sessions.put (AD_Session_ID, session, e -> new MSession(Env.getCtx(), e));
} else
{
session = null;
s_sessions.remove(AD_Session_ID);
}
}
// Create New
if (session == null && createNew)
{
session = new MSession (ctx, null); // local session
session = new MSession (ctx, (String)null); // local session
session.saveEx();
AD_Session_ID = session.getAD_Session_ID();
Env.setContext (ctx, Env.AD_SESSION_ID, AD_Session_ID);
s_sessions.add (Integer.valueOf(AD_Session_ID));
}
if(isImmutable)
s_sessions.put (AD_Session_ID, session, e -> new MSession(Env.getCtx(), e));
}
return session;
} // get
@ -86,34 +104,42 @@ public class MSession extends X_AD_Session
* @param Remote_Host remote host
* @param WebSession web session
* @return session
* @deprecated use get (Properties ctx, String Remote_Addr, String Remote_Host, String WebSession, boolean isCache)
*/
public static MSession get (Properties ctx, String Remote_Addr, String Remote_Host, String WebSession)
{
return get(ctx, Remote_Addr, Remote_Host, WebSession, false);
} // get
/**
* Get existing or create remote session
* @param ctx context
* @param Remote_Addr remote address
* @param Remote_Host remote host
* @param WebSession web session
* @param isImmutable return Immutable Object (from Cache)
* @return session
*/
public static MSession get (Properties ctx, String Remote_Addr, String Remote_Host, String WebSession, boolean isImmutable)
{
int AD_Session_ID = Env.getContextAsInt(ctx, Env.AD_SESSION_ID);
MSession session = null;
// Try to load
if (AD_Session_ID > 0 && s_sessions.contains(AD_Session_ID))
{
session = new MSession(ctx, AD_Session_ID, null);
if (session.get_ID() != AD_Session_ID)
{
session = null;
s_sessions.remove(AD_Session_ID);
}
}
MSession session = get(ctx, false, isImmutable);
if (session == null)
{
session = new MSession (ctx, Remote_Addr, Remote_Host, WebSession, null); // remote session
session.saveEx();
AD_Session_ID = session.getAD_Session_ID();
Env.setContext(ctx, Env.AD_SESSION_ID, AD_Session_ID);
s_sessions.add(Integer.valueOf(AD_Session_ID));
if(isImmutable)
s_sessions.put (AD_Session_ID, session, e -> new MSession(Env.getCtx(), e));
}
return session;
} // get
/** Sessions */
private static Vector<Integer> s_sessions = new Vector<>();
/** Session Cache */
private static ImmutableIntPOCache<Integer,MSession> s_sessions = new ImmutableIntPOCache<Integer,MSession>(Table_Name, 20);
/**************************************************************************
@ -193,6 +219,37 @@ public class MSession extends X_AD_Session
log.log(Level.SEVERE, "No Local Host", e);
}
} // MSession
/**
*
* @param copy
*/
public MSession(MSession copy)
{
this(Env.getCtx(), copy);
}
/**
*
* @param ctx
* @param copy
*/
public MSession(Properties ctx, MSession copy)
{
this(ctx, copy, (String) null);
}
/**
*
* @param ctx
* @param copy
* @param trxName
*/
public MSession(Properties ctx, MSession copy, String trxName)
{
this(ctx, 0, trxName);
copyPO(copy);
}
/** Web Store Session */
private boolean m_webStoreSession = false;
@ -330,5 +387,14 @@ public class MSession extends X_AD_Session
public static int getCachedSessionCount() {
return s_sessions.size()-1;
}
@Override
public MSession markImmutable() {
if (is_Immutable())
return this;
makeImmutable();
return this;
}
} // MSession

View File

@ -2661,7 +2661,7 @@ public abstract class PO
lobReset();
// Change Log
MSession session = MSession.get (p_ctx, false);
MSession session = MSession.get (p_ctx, false, true);
if (session == null)
log.fine("No Session found");
int AD_ChangeLog_ID = 0;
@ -3123,7 +3123,7 @@ public abstract class PO
lobReset();
// Change Log
MSession session = MSession.get (p_ctx, false);
MSession session = MSession.get (p_ctx, false, true);
if (session == null)
log.fine("No Session found");
int AD_ChangeLog_ID = 0;
@ -3719,7 +3719,7 @@ public abstract class PO
if( p_info.isChangeLog())
{
// Change Log
MSession session = MSession.get (p_ctx, false);
MSession session = MSession.get (p_ctx, false, true);
if (session == null)
log.fine("No Session found");
else if (m_IDs.length == 1)

View File

@ -912,7 +912,7 @@ public class ProcessInfo implements Serializable
}
private Timestamp getLastServerRebootDate() {
MSession currentSession = MSession.get(Env.getCtx(), false);
MSession currentSession = MSession.get(Env.getCtx(), false, true);
if (currentSession == null)
return null;

View File

@ -176,7 +176,7 @@ public final class Env
//hengsin, avoid unncessary query of session when exit without log in
if (DB.isConnected(false)) {
// End Session
MSession session = MSession.get(Env.getCtx(), false); // finish
MSession session = MSession.get(Env.getCtx(), false, false); // finish
if (session != null)
session.logout();
}
@ -195,7 +195,7 @@ public final class Env
public static void logout()
{
// End Session
MSession session = MSession.get(Env.getCtx(), false); // finish
MSession session = MSession.get(Env.getCtx(), false, false); // finish
if (session != null)
session.logout();
//

View File

@ -98,7 +98,7 @@ public class AdempiereActivator extends AbstractActivator {
MSession localSession = null;
//Create Session to be able to create records in AD_ChangeLog
if (Env.getContextAsInt(Env.getCtx(), Env.AD_SESSION_ID) <= 0) {
localSession = MSession.get(Env.getCtx(), true);
localSession = MSession.get(Env.getCtx(), true, false);
localSession.setWebSession("AdempiereActivator");
localSession.saveEx();
}

View File

@ -212,7 +212,7 @@ public class Incremental2PackActivator extends AbstractActivator {
MSession localSession = null;
//Create Session to be able to create records in AD_ChangeLog
if (Env.getContextAsInt(Env.getCtx(), Env.AD_SESSION_ID) <= 0) {
localSession = MSession.get(Env.getCtx(), true);
localSession = MSession.get(Env.getCtx(), true, false);
localSession.setWebSession("Incremental2PackActivator");
localSession.saveEx();
}

View File

@ -101,7 +101,7 @@ public class PackInApplicationActivator extends AbstractActivator{
if (getDBLock()) {
//Create Session to be able to create records in AD_ChangeLog
if (Env.getContextAsInt(Env.getCtx(), Env.AD_SESSION_ID) <= 0) {
localSession = MSession.get(Env.getCtx(), true);
localSession = MSession.get(Env.getCtx(), true, false);
localSession.setWebSession("PackInApplicationActivator");
localSession.saveEx();
}

View File

@ -151,7 +151,7 @@ public class Version2PackActivator extends AbstractActivator{
if (getDBLock()) {
//Create Session to be able to create records in AD_ChangeLog
if (Env.getContextAsInt(Env.getCtx(), Env.AD_SESSION_ID) <= 0) {
localSession = MSession.get(Env.getCtx(), true);
localSession = MSession.get(Env.getCtx(), true, false);
localSession.setWebSession("Version2PackActivator");
localSession.saveEx();
}

View File

@ -119,7 +119,7 @@ public class AdempiereServerMgr implements ServiceTrackerCustomizer<IServerFacto
log.info("");
// Set Session
MSession session = MSession.get(getCtx(), true);
MSession session = MSession.get(getCtx(), true, false);
session.setWebStoreSession(false);
session.setWebSession("Server");
session.saveEx();

View File

@ -132,7 +132,7 @@ public class Scheduler extends AdempiereServer
Env.setContext(getCtx(), Env.DATE, dateFormat4Timestamp.format(ts)+" 00:00:00" ); // JDBC format
//Create new Session and set #AD_Session_ID to context
MSession session = MSession.get(getCtx(), true);
MSession session = MSession.get(getCtx(), true, false);
MProcess process = new MProcess(getCtx(), scheduler.getAD_Process_ID(), null);
try
{

View File

@ -273,7 +273,7 @@ public class AdempiereWebUI extends Window implements EventListener<Event>, IWeb
String x_Forward_IP = Executions.getCurrent().getHeader("X-Forwarded-For");
MSession mSession = MSession.get (ctx, x_Forward_IP!=null ? x_Forward_IP : Executions.getCurrent().getRemoteAddr(),
Executions.getCurrent().getRemoteHost(), httpSess.getId() );
Executions.getCurrent().getRemoteHost(), httpSess.getId(), false );
if (clientInfo.userAgent != null) {
mSession.setDescription(mSession.getDescription() + "\n" + clientInfo.toString());
mSession.saveEx();

View File

@ -211,7 +211,7 @@ public final class AEnv
}
windowCache.remove(sessionID);
// End Session
MSession session = MSession.get(Env.getCtx(), false); // finish
MSession session = MSession.get(Env.getCtx(), false, false); // finish
if (session != null)
session.logout();

View File

@ -265,7 +265,7 @@ public class SessionContextListener implements ExecutionInit,
}
}
MSession mSession = MSession.get(Env.getCtx(), false);
MSession mSession = MSession.get(Env.getCtx(), false, false);
if(mSession!=null && !mSession.isProcessed()) {
mSession.setProcessed(true);
@ -318,7 +318,7 @@ public class SessionContextListener implements ExecutionInit,
{
setupExecutionContextFromSession(Executions.getCurrent());
}
MSession mSession = MSession.get(Env.getCtx(), false);
MSession mSession = MSession.get(Env.getCtx(), false, false);
if(mSession!=null){
if (mSession.isProcessed()) {
mSession.setProcessed(false);

View File

@ -288,10 +288,10 @@ public class CompiereService {
Env.setContext(getCtx(), Env.LANGUAGE, m_language.getAD_Language());
// Create session
MSession session = MSession.get (getCtx(), false);
MSession session = MSession.get (getCtx(), false, false);
if (session == null){
log.fine("No Session found");
session = MSession.get (getCtx(), true);
session = MSession.get (getCtx(), true, false);
}
session.setWebSession("WebService");