How to create extra savepoints in Java

0
I am still in the process to try to elminate 3 million records from a database. I have written the below javacode to do this (given two objects with a relation). Now I want to create extra savepoints to this action like described here But simce my Java skills are limited I have probllems adding these savepoints. I assumed these savepoints would be usefull just before the batch.remove code (because now I still get javaheap space problems and a rollback is performed hence the extra savepoint after each batch). Or should I do seperate transactions for the batch.remove part? But how do I do that from this code? Regards, Ronald // This file was generated by Mendix Business Modeler 2.5. // // 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 gegevensimport.actions; import com.mendix.core.Core; import com.mendix.systemwideinterfaces.core.IContext; import com.mendix.systemwideinterfaces.core.IRemoveBatch; import com.mendix.systemwideinterfaces.core.UserAction; import com.mendix.systemwideinterfaces.core.IMendixObject; import gegevensimport.proxies.ContractOUD; import gegevensimport.proxies.GegevensImport; import java.util.List; import java.util.ArrayList; /** * */ public class DeleteGegevensImport_ContractOud extends UserAction<Boolean> { private IMendixObject __gegevensImport; private gegevensimport.proxies.GegevensImport gegevensImport; private IMendixObject __contractOud; private gegevensimport.proxies.ContractOUD contractOud; public DeleteGegevensImport_ContractOud(IMendixObject gegevensImport, IMendixObject contractOud) { super(); this.__gegevensImport = gegevensImport; this.__contractOud = contractOud; } @Override public Boolean executeAction() throws Exception { this.gegevensImport = __gegevensImport == null ? null : gegevensimport.proxies.GegevensImport.initialize(this.getContext(), __gegevensImport); this.contractOud = __contractOud == null ? null : gegevensimport.proxies.ContractOUD.initialize(this.getContext(), __contractOud); // BEGIN USER CODE boolean done = false; if (contractOud != null) { String entityName1 = ContractOUD.entityName; int entityName1Length = entityName1.length(); if (entityName1Length != 0) { String entityName2 = GegevensImport.entityName; int entityName2Length = entityName2.length(); int index = entityName2.indexOf(".")+1; String cleanEntityName2 = entityName2.substring(index, entityName2Length); String relationName = entityName1 + "_" + cleanEntityName2; String objectID = gegevensImport.getGUID(); IContext context = this.getContext(); IRemoveBatch batch = Core.removeBatch(context, entityName1, 1000, false, false); while (!done) { List<IMendixObject> lijst = Core.retrieveXPathQueryEscaped(getContext(), "//%s[%s='%s']", entityName1, relationName, objectID); if (!lijst.isEmpty()) { for (final IMendixObject object : lijst) { batch.removeObject(object); } batch.commit(); lijst.clear(); } else { done = true; } } } } return done; // END USER CODE } /** * Returns a string representation of this action */ @Override public String toString() { return "DeleteGegevensImport_ContractOud"; } // BEGIN EXTRA CODE // END EXTRA CODE }
asked
2 answers
1

Your problem is retrieving 3m records at once, this will most likely cause a heapspace regardless of what you do elsewhere. You should consider using using a limit and offset in your: Core.retrieveXPathQueryEscaped statement.

For example, instead of:

        List<IMendixObject> lijst = Core.retrieveXPathQueryEscaped(getContext(), "//%s[%s='%s']", entityName1, relationName, objectID);
    if (!lijst.isEmpty()) 
        {
            for (final IMendixObject object : lijst) 
            {
                batch.removeObject(object);
            }
            batch.commit();

            lijst.clear();
        }

Do this:

    int limit = 5000;
    int offset = 0;

    HashMap<String, String> sortMap = new HashMap<String, String>();
    sortMap.put("id", "asc");
    int count = 0;
    int depth = 0;

    do {
        List<IMendixObject> lijst = Core.retrieveXPathQueryEscaped(getContext(), "//%s[%s='%s']", limit, offset, sortMap, depth, entityName1, relationName, objectID);
        count = lijst.size();

        for (final IMendixObject object : lijst) 
        {
            batch.removeObject(object);
        }
        batch.commit();

        lijst.clear();
     } while (count == limit);
answered
0

Thanks Jaap, but I get this error:

The method retrieveXPathQueryEscaped(IContext, String, int, int, Map<String,String>, int, String...) in the type Core is not applicable for the arguments (IContext, String, int, int, HashMap<String,String>, String, String, String)

Could this be that this error is because this code needs to work in 2.5.6?

answered