使用JPA,方法需要60秒

问题描述 投票:0回答:2

我正在使用JPA,我的Web应用程序需要60秒才能执行此方法,我想更快地执行它,如何才能实现?

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
    }
}
java performance jpa
2个回答
0
投票

正如@Nikos Paraskevopoulos所述,它可能应该是for循环内的〜900 * N个数据库迭代。

我想尽可能避免DB迭代,特别是在这样的循环内。

您可以尝试详细说明当前的StudentExamResponse sql以包含更多的子句-主要是在for内部使用的子句,这甚至可以减少迭代的项目数量。


0
投票

我的猜测是您的选择查询需要时间。如果可能,将查询超时设置为少于60秒并确认。

可以在此处找到设置查询超时的方法-How to set the timeout period on a JPA EntityManager query

如果这是因为查询,那么您可能需要使选择查询最佳化。

© www.soinside.com 2019 - 2024. All rights reserved.