0

I am working with JPA, my web application is taking 60 sec to execute this method, I want to execute it faster how to achive ?

public boolean evaluateStudentTestPaper (long testPostID, long studentID, long howManyTimeWroteExam) {

    Gson uday = new Gson();
    Logger custLogger = Logger.getLogger("StudentDao.java");
    // custLogger.info("evaluateTestPaper test paper for testPostID: " +
    // testPostID);

    long subjectID = 0;

    // checking in table
    EntityManagerFactory EMF = EntityManagerFactoryProvider.get();
    EntityManager em = EMF.createEntityManager();
    List<StudentExamResponse> studentExamResponses = null;

    try {
        studentExamResponses = em
                .createQuery(
                        "SELECT o FROM StudentExamResponse o where o.studentId=:studentId And o.testPostID=:testPostID and o.howManyTimeWroteExam=:howManyTimeWroteExam")
                .setParameter("studentId", studentID).setParameter("testPostID", testPostID)
                .setParameter("howManyTimeWroteExam", howManyTimeWroteExam).getResultList();
        System.out.println("studentExamResponses--------------------------------------------------"
                + uday.toJson(studentExamResponses) + "---------------------------------------");
    } catch (Exception e) {
        custLogger.info("exception at getting student details:" + e.toString());
        studentExamResponses = null;
    }

    int studentExamResponseSize = studentExamResponses.size();

    if (AppConstants.SHOWLOGS.equalsIgnoreCase("true")) {
        custLogger.info("student questions list:" + studentExamResponseSize);
    }

    // Get all questions based on student id and test post id
    List<ExamPaperRequest> examPaperRequestList = new ArrayList<ExamPaperRequest>();
    List<Questions> questionsList = new ArrayList<Questions>();
    // StudentExamResponse [] studentExamResponsesArgs =
    // (StudentExamResponse[]) studentExamResponses.toArray();
    // custLogger.info("Total questions to be evaluated: " +
    // examPaperRequestList.size());
    List<StudentTestResults> studentTestResultsList = new ArrayList<StudentTestResults>();

    StudentTestResults studentTestResults = null;
    StudentResults studentResults = null;
    String subjectnames = "", subjectMarks = "";
    int count = 0;
    boolean lastIndex = false;

    if (studentExamResponses != null && studentExamResponseSize > 0) {
        // studentExamResponses.forEach(studentExamResponses->{

         for (StudentExamResponse o : studentExamResponses.stream().parallel()) {

           // 900 lines of coade inside which includes getting data from database Queries
    }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    We cannot guess which code is causing the delay. If I were to guess however, I would say it is within the 900 lines of code doing DB queries in a loop that are not shown here. Additionally we do not know the size of the data - is the `SELECT` fetching a million rows? My suggestion is to find a way to locate the delays; even printing the `currentTimeMillis` at appropriate places will help. Then see how to optimize specific code fragments that need optimization. – Nikos Paraskevopoulos Dec 09 '19 at 12:23
  • Just putting parallel() at steam doesn't make anything parallel. For loop is still a synchronous context. – Lev Kuznetsov Dec 09 '19 at 21:17
  • hi Lev thanks for the suggestion, im getting following error java.lang.NoClassDefFoundError: java.util.stream.Stream is a restricted class. Please see the Google App Engine developer's guide for more details. – udaykiran ch Dec 10 '19 at 13:06

2 Answers2

0

As @Nikos Paraskevopoulos mentioned, it should probably be the ~900 * N database iterations inside that for loop.

I'd say to avoid DB iterations as much as you can, specially inside a loop like that.

You can try to elaborate your current StudentExamResponse sql to englobe more clauses - those you're using inside your for mainly, which could even diminish the amount of items you iterate upon.

egzefer
  • 1
  • 1
0

My guess would be your select query is taking time. If possible, set query timeout to less than 60 seconds & confirm this.

Ways of setting query timeout can be found out there - How to set the timeout period on a JPA EntityManager query

If this is because of query, then you may need to work to make select query optimal.

Ashish Patil
  • 4,428
  • 1
  • 15
  • 36