-1

Can somebody please help to optimize the code below.

The problem statement is : i am trying to populate the struct array by looping through the List List. which is causing performance issue. is there a way to do it without the loop?

The code below works as expected but the UI hangs becuase of loop, can somebody please help optimise it.

public BigDecimal saveCSV(String dataSource,int rollNumber,String username,List<Project> projects) throws SQLException{
        Connection conn = getConnection(dataSource);
        Connection nativeConn=doGetNativeConnection(conn);
        nativeConn.setAutoCommit(false);
        CallableStatement cs= nativeConn.prepareCall(ProjectConstants.PROC);

        ArrayDescriptor des = ArrayDescriptor.createDescriptor("PROJECT_DETAILS_TYPE", nativeConn);
        Object [] data = projects.toArray();
        Array array_to_pass = new ARRAY(des,nativeConn,data);
        STRUCT[] structArrayOfProjects=new STRUCT[projects.size()];
         Object[] projObjectArray = null;

         for (int i = 0; i < projects.size(); ++i) {
            Project proj=projects.get(i);
            projObjectArray=new Object[]{proj.name,proj.activity};
            StructDescriptor desc = StructDescriptor.createDescriptor("PROJECT_DETAILS_TYPE", nativeConn);
            STRUCT structprojects = new STRUCT(desc, nativeConn, projObjectArray);
            structArrayOfProjects[i] = structprojects;
        }
        ArrayDescriptor projectTypeArrayDesc = ArrayDescriptor.createDescriptor("PROJECT_DETAILS_TAB_TYPE", nativeConn);
        ARRAY arrayOfProjects = new ARRAY(projectTypeArrayDesc, nativeConn, structArrayOfProjects);

        cs.setArray(1, array_to_pass);
        cs.setInt(2, rollNumber);
        cs.setString(3, username);
        cs.registerOutParameter(4, OracleTypes.ARRAY,"NUMBER_TAB_TYPE");
        cs.registerOutParameter(5, OracleTypes.ARRAY,"PROJECTS_ERROR_TAB_TYPE");

        cs.execute();
        nativeConn.commit();

        Array value=cs.getArray(4);
        BigDecimal[] projDetailsId = (BigDecimal[])value.getArray();
        BigDecimal rmt_id = null;
        try{
            rmt_id=projDetailsId[0];
        }
        catch(Exception e){
            e.printStackTrace();

        }
        return rmt_id;

    }
rkosegi
  • 14,165
  • 5
  • 50
  • 83
fiddle
  • 1,095
  • 5
  • 18
  • 33

1 Answers1

1

Use worker thread to perform DB tasks and UI thread to update your GUI.

Doing I/O and CPU intensive tasks on UI thread is discouraged.

As you didn't specify what kind of user interface you are using,

I assume Swing, if so read this guide how to handle such tasks.

UPDATE

After OP comment that environment where code is running is Spring MVC, here is suggestion.

Same logic applies to applications deployed into servlet containers. When you have long running task in request thread, you should use ExecutorService to create asynchronous task and return HTTP202 immediately.

Then you need to use some polling methods to periodically request completion status (or use websocket if possible).

Here are some examples : here, here or here.

rkosegi
  • 14,165
  • 5
  • 50
  • 83