How to pass instance from 1 java action to another?

Hi , I’m building a connector with the java SDK of our software , for the moment I have to intialize the connector object for each call & do a login. This is really bad for performance & will probably crash in production, So I was wondering if you could pass an instance from one java action to another? So I should have 1 action where I do login and retrieve the object “IrisNextCalls” in every other java action where the login is remained. In a standalone java application you will pass it by a constructor,  In a Spring application you will use @Autowired, @Inject , @Resource….. but as Mendix only offers custom code in the execute part I don’t really know how I can remain the “IrisNextCalls” Object in runtime where a login is done for each user & retrieve this already initialized object in other java actions. Any recommendations? Current code but new initialization should be removed (first 10 lines starting from ‘//BEGIN USER CODE until IrisNextCalls.login();’ ) , login should be kept per user // This file was generated by Mendix Studio Pro. // // WARNING: Only the following code will be retained when actions are regenerated: // - the import list // - the code between BEGIN USER CODE and END USER CODE // - the code between BEGIN EXTRA CODE and END EXTRA CODE // Other code you write will be lost the next time you deploy the project. // Special characters, e.g., é, ö, à, etc. are supported in comments. package irisnext_java.actions; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import; import com.irisims.irisNextConnectorLib.IrisNextCalls; import com.irisims.irisNextConnectorLib.IrisNextCallsImpl; import com.irisims.irisNextConnectorLib.IrisNextService; import com.irisims.irisNextConnectorLib.model.model_actual.MendixObjectDTO; import com.irisims.irisNextConnectorLib.model.model_actual.IndexCardDTO; import com.mendix.systemwideinterfaces.core.IContext; import com.mendix.webui.CustomJavaAction; import irisnext_restconsume_java.actions.impl.IrisNextImpl; import irisnext_restpublish.proxies.*; import com.mendix.systemwideinterfaces.core.IMendixObject; public class IrisNext_Search_ObjectList extends CustomJavaAction<java.util.List<IMendixObject>> { private java.lang.String objectType; private java.lang.String criteria; private IMendixObject __Login; private irisnext_core.proxies.Login_IrisNext Login; public IrisNext_Search_ObjectList(IContext context, java.lang.String objectType, java.lang.String criteria, IMendixObject Login) { super(context); this.objectType = objectType; this.criteria = criteria; this.__Login = Login; } @java.lang.Override public java.util.List<IMendixObject> executeAction() throws Exception { this.Login = this.__Login == null ? null : irisnext_core.proxies.Login_IrisNext.initialize(getContext(), __Login); // BEGIN USER CODE IrisNextCalls irisNextCalls = new IrisNextCallsImpl(); // SHOULD NOT BE IN A NEW INITIALISATION IrisNextImpl irisNextImpl = new IrisNextImpl(); ((IrisNextCallsImpl) irisNextCalls).setUrl(Login.getURL()); ((IrisNextCallsImpl) irisNextCalls).setUser(Login.getUser()); ((IrisNextCallsImpl) irisNextCalls).setPassword(Login.getPassword()); ((IrisNextCallsImpl) irisNextCalls).setDatabase(Login.getDatabase()); ((IrisNextCallsImpl) irisNextCalls).setLanguage(Login.getLanguage()); irisNextCalls.login(); IContext context = getContext(); List<MendixObjectDTO> irisnextObjectList = irisNextCalls.search_MendixObjects(objectType, criteria); //, map List<IRISNextCreate> mendixObjectList = new ArrayList<>(); irisnextObjectList.forEach(irisNextObject -> { IRISNextCreate irisNextCreate = new IRISNextCreate(context); irisNextCreate = irisNextImpl.convertObject(irisNextCreate, irisNextObject, context); mendixObjectList.add(irisNextCreate); }); return -> e.getMendixObject()).collect(Collectors.toList()); // END USER CODE } /** * Returns a string representation of this action * @return a string representation of this action */ @java.lang.Override public java.lang.String toString() { return "IrisNext_Search_ObjectList"; } // BEGIN EXTRA CODE // END EXTRA CODE }  
1 answers

Use a so called Singleton. This is one and only one object that stays in memory and can maintain the connection.


See for example the ConfigurationManager of MendixSSO.


Called like



place the code of this in a separate folder so the Java-actions won’t erase them. Add an import to use them like



import mendixsso.implementation.ConfigurationManager;

Be careful and use time-outs to close connections and cleanup objects that reside there.