IDEMPIERE-5256 Add Cache to MSession Refactor (#1283)

This commit is contained in:
igorpojzl 2022-04-20 13:00:13 +02:00 committed by GitHub
parent e9ab40a18f
commit 8d5f7768e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 83 additions and 58 deletions

View File

@ -53,26 +53,27 @@ public class MSession extends X_AD_Session implements ImmutablePOSupport
* @param ctx context
* @param createNew create if not found
* @return session session
* @deprecated get (Properties ctx, boolean createNew, boolean isCache) for caching support
* @deprecated use Get and Create functions.
*/
public static MSession get (Properties ctx, boolean createNew)
{
return get(ctx, createNew, false);
MSession session = get(ctx);
if(session == null && createNew)
return MSession.create(ctx);
return session;
} // get
/**
* Get existing or create local session
* Get existing 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)
public static MSession get (Properties ctx)
{
int AD_Session_ID = Env.getContextAsInt(ctx, Env.AD_SESSION_ID);
MSession session = isImmutable ? s_sessions.get(ctx, AD_Session_ID, e -> new MSession(ctx, e)) : new MSession(ctx, AD_Session_ID, null);
MSession session = s_sessions.get(ctx, AD_Session_ID, e -> new MSession(ctx, e));
// Try to load
if (isImmutable && session == null && AD_Session_ID > 0)
if (session == null && AD_Session_ID > 0)
{
session = new MSession(ctx, AD_Session_ID, null);
if (session.get_ID () == AD_Session_ID)
@ -83,17 +84,22 @@ public class MSession extends X_AD_Session implements ImmutablePOSupport
session = null;
}
}
// Create New
if (session == null && createNew)
{
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);
if(isImmutable)
s_sessions.put (AD_Session_ID, session, e -> new MSession(Env.getCtx(), e));
}
return session;
} // 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 create (Properties ctx)
{
MSession session = new MSession (ctx, (String)null); // local session
session.saveEx();
int AD_Session_ID = session.getAD_Session_ID();
Env.setContext (ctx, Env.AD_SESSION_ID, AD_Session_ID);
return session;
} // get
@ -104,26 +110,11 @@ public class MSession extends X_AD_Session implements ImmutablePOSupport
* @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 = get(ctx, false, isImmutable);
MSession session = get(ctx);
if (session == null)
{
@ -131,8 +122,8 @@ public class MSession extends X_AD_Session implements ImmutablePOSupport
session.saveEx();
AD_Session_ID = session.getAD_Session_ID();
Env.setContext(ctx, Env.AD_SESSION_ID, AD_Session_ID);
if(isImmutable)
s_sessions.put (AD_Session_ID, session, e -> new MSession(Env.getCtx(), e));
} else {
session = new MSession(ctx, session.getAD_Session_ID(), null);
}
return session;

View File

@ -2661,7 +2661,7 @@ public abstract class PO
lobReset();
// Change Log
MSession session = MSession.get (p_ctx, false, true);
MSession session = MSession.get (p_ctx);
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, true);
MSession session = MSession.get (p_ctx);
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, true);
MSession session = MSession.get (p_ctx);
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, true);
MSession currentSession = MSession.get(Env.getCtx());
if (currentSession == null)
return null;

View File

@ -176,10 +176,12 @@ 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, false); // finish
if (session != null)
MSession session = MSession.get(Env.getCtx()); // finish
if (session != null) {
session = new MSession(getCtx(), session.getAD_Session_ID(), null);
session.logout();
}
}
//
reset(true); // final cache reset
//
@ -195,9 +197,11 @@ public final class Env
public static void logout()
{
// End Session
MSession session = MSession.get(Env.getCtx(), false, false); // finish
if (session != null)
MSession session = MSession.get(Env.getCtx()); // finish
if (session != null) {
session = new MSession(getCtx(), session.getAD_Session_ID(), null);
session.logout();
}
//
reset(true); // final cache reset
//

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, false);
localSession = MSession.create(Env.getCtx());
localSession.setWebSession("AdempiereActivator");
localSession.saveEx();
}

View File

@ -212,7 +212,12 @@ 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, false);
localSession = MSession.get(Env.getCtx());
if(localSession == null) {
localSession = MSession.create(Env.getCtx());
} else {
localSession = new MSession(Env.getCtx(), localSession.getAD_Session_ID(), null);
}
localSession.setWebSession("Incremental2PackActivator");
localSession.saveEx();
}

View File

@ -101,7 +101,12 @@ 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, false);
localSession = MSession.get(Env.getCtx());
if(localSession == null) {
localSession = MSession.create(Env.getCtx());
} else {
localSession = new MSession(Env.getCtx(), localSession.getAD_Session_ID(), null);
}
localSession.setWebSession("PackInApplicationActivator");
localSession.saveEx();
}

View File

@ -151,7 +151,12 @@ 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, false);
localSession = MSession.get(Env.getCtx());
if(localSession == null) {
localSession = MSession.create(Env.getCtx());
} else {
localSession = new MSession(Env.getCtx(), localSession.getAD_Session_ID(), null);
}
localSession.setWebSession("Version2PackActivator");
localSession.saveEx();
}

View File

@ -119,7 +119,12 @@ public class AdempiereServerMgr implements ServiceTrackerCustomizer<IServerFacto
log.info("");
// Set Session
MSession session = MSession.get(getCtx(), true, false);
MSession session = MSession.get(getCtx());
if(session == null) {
session = MSession.create(getCtx());
} else {
session = new MSession(getCtx(), session.getAD_Session_ID(), null);
}
session.setWebStoreSession(false);
session.setWebSession("Server");
session.saveEx();

View File

@ -132,7 +132,12 @@ 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, false);
MSession session = MSession.get(Env.getCtx());
if(session == null) {
session = MSession.create(Env.getCtx());
} else {
session = new MSession(Env.getCtx(), session.getAD_Session_ID(), null);
}
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(), false );
Executions.getCurrent().getRemoteHost(), httpSess.getId());
if (clientInfo.userAgent != null) {
mSession.setDescription(mSession.getDescription() + "\n" + clientInfo.toString());
mSession.saveEx();

View File

@ -211,7 +211,8 @@ public final class AEnv
}
windowCache.remove(sessionID);
// End Session
MSession session = MSession.get(Env.getCtx(), false, false); // finish
int ad_Session_ID = Env.getContextAsInt(Env.getCtx(), Env.AD_SESSION_ID);
MSession session = ad_Session_ID > 0 ? new MSession(Env.getCtx(), ad_Session_ID, null) : null; // finish
if (session != null)
session.logout();

View File

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

View File

@ -288,11 +288,14 @@ public class CompiereService {
Env.setContext(getCtx(), Env.LANGUAGE, m_language.getAD_Language());
// Create session
MSession session = MSession.get (getCtx(), false, false);
MSession session = MSession.get (getCtx());
if (session == null){
log.fine("No Session found");
session = MSession.get (getCtx(), true, false);
session = MSession.create (getCtx());
} else {
session = new MSession(getCtx(), session.getAD_Session_ID(), null);
}
session.setWebSession("WebService");
session.setDescription(session.getDescription() + "\nUser Agent: " + getCtx().getProperty("#UserAgent"));