следующая логика изначально выполняется нормально, но после ее выполнения несколько раз она замедляется и, наконец, возникает взаимоблокировка, и если число пользователей больше одного, то это происходит даже за очень меньшее время.чтобы он снова заработал, мне нужно перезапустить сервер.
Технология: Spring 4.0 Java 1.8 Hibernate 3.6.7 Final Postgresql 9.5
Business Logic:
@RequestMapping(value = "saveallstudentsmarks.cu", method = RequestMethod.POST)
public String saveAllStudentsScore(@RequestParam("marksid") ArrayList<Long> marksids,
@RequestParam("studentacademicid") ArrayList<Long> studentacademicid,
@RequestParam("examid") ArrayList<Long> examid, @RequestParam("subjectid") ArrayList<Long> subjectid,
@RequestParam("currentcsssm") Long currentcsssm, @RequestParam("subject_id") Long subject_id,
@RequestParam("exam_id") Long exam_id, @RequestParam("marksscored") ArrayList<String> marksscored,
RedirectAttributes attribute, HttpServletRequest request) {
HttpSession session = request.getSession();
if (session.getAttribute("user_id") == null)
return "redirect:/";
if (session.getAttribute("privilegeList") == null)
return "redirect:/404_error";
@SuppressWarnings("unchecked")
List<Integer> userPrivileges = (List<Integer>) session.getAttribute("privilegeList");
if (!userPrivileges.contains(PrivilegeDescriptionConstant.STUDENTS_MARKS_ENTRY))
return "redirect:/404_error";
List<Marks> finalMarks = new ArrayList<Marks>();
List<Marks> finalMarks2 = new ArrayList<Marks>();
String usertype = (String) session.getAttribute("usertype");
if (usertype.equals("Student")) {
} else {
// Thread thread = new Thread(new Runnable(){
//
// @Override
// public void run() {
// // TODO Auto-generated method stub
Employee sessionEmployee = (Employee) session.getAttribute("profile");
ClassSectionStreamSubjectMapping rtnCsssm = classSectionStreamSubjectMappingService
.getClassSectionStreamSubjectMappingById(currentcsssm);
Subject rtnSubject = subjectService.getSubjectById(subject_id);
Exams exam = examService.getExamById(exam_id);
Exams bExam = examService.getExamById(exam.getBase_exam_idfk().getId());
int cntr = 0;
for (Long ids : marksids) {
Marks m = new Marks();
m.setId(ids);
m.setStudent_academic_id_fk(studentAcademicService.getStudentAcademicById(studentacademicid.get(cntr)));
Exams rtnExam = examService.getExamById(examid.get(cntr));
m.setExams_idfk(rtnExam);
m.setSubject_idfk(subjectService.getSubjectById(subjectid.get(cntr)));
if (ids != null) {
m.setMarks_updated_by(sessionEmployee);
m.setUpdate_date(new Date());
} else {
m.setMarks_entered_by(sessionEmployee);
m.setEntry_date(new Date());
}
if (rtnExam.getMarks_entry_type() == 1) {
double rqdmark = 0;
try {
rqdmark = Double.parseDouble(marksscored.get(cntr));
if (rqdmark != 0) {
m.setScored(rqdmark);
}
} catch (Exception e) {
// TODO: handle exception
}
} else if (rtnExam.getMarks_entry_type() == 3) {
m.setGrade_idfk(gradeService.getGradeById(Long.parseLong(marksscored.get(cntr))));
}
// System.out.println(" Marksid "+ids+" studentacademicid
// "+studentacademicid.get(cntr)+" examid "+examid.get(cntr)+"
// subjectid "+subjectid.get(cntr)+" scored
// "+marksscored.get(cntr));
cntr++;
finalMarks2.add(m);
// Marks rtnMarks = marksService.saveMarks(m);
}
marksService.saveBulkMarks(finalMarks2);
// marksServiceRepository.saveAll(finalMarks2);
List<ClassSubjectExams> rtnExams = examService.listExamsByCsssmSubjectBaseExam(rtnCsssm, rtnSubject, bExam);
for (Long ids : studentacademicid) {
StudentAcademic rtnStudentAcademic = studentAcademicService.getStudentAcademicById(ids);
for (ClassSubjectExams cse : rtnExams) {
if (cse.getExams_idfk().getMarks_entry_type() == 2) {
Marks eMarks = new Marks();
Marks checkMark = marksService.getMarksByExamStudentAcademicSubject(cse.getExams_idfk(),
rtnStudentAcademic, rtnSubject);
if (marksService.getMarksByExamStudentAcademicSubject(cse.getExams_idfk(), rtnStudentAcademic,
rtnSubject) != null) {
eMarks.setId(checkMark.getId());
}
eMarks.setSubject_idfk(rtnSubject);
eMarks.setStudent_academic_id_fk(rtnStudentAcademic);
eMarks.setExams_idfk(cse.getExams_idfk());
double total = 0;
double totalpassmark = 0;
ArrayList<Double> compare = new ArrayList<Double>();
for (int i = 0; i < cse.getExams_idfk().getExams_ids().length; i++) {
if (cse.getExams_idfk().getOperation() == 1) {
Marks rtnMarks = marksService.getMarksByExamStudentAcademicSubject(
examService.getExamById(cse.getExams_idfk().getExams_ids()[i]),
studentAcademicService.getStudentAcademicById(ids), rtnSubject);
if (rtnMarks != null) {
if (rtnMarks.getCompared_values() != null) {
for (int j = 0; j < rtnMarks.getCompared_values().length; j++) {
total += rtnMarks.getCompared_values()[j];
}
}
total += rtnMarks.getScored();
}
}
if (cse.getExams_idfk().getOperation() == 2) {
Marks rtnMarks = marksService.getMarksByExamStudentAcademicSubject(
examService.getExamById(cse.getExams_idfk().getExams_ids()[i]),
studentAcademicService.getStudentAcademicById(ids), rtnSubject);
total -= rtnMarks.getScored();
}
if (cse.getExams_idfk().getOperation() == 4) {
Marks rtnMarks = marksService.getMarksByExamStudentAcademicSubject(
examService.getExamById(cse.getExams_idfk().getExams_ids()[i]),
studentAcademicService.getStudentAcademicById(ids), rtnSubject);
if (rtnMarks != null) {
total += rtnMarks.getScored();
totalpassmark += rtnMarks.getExams_idfk().getTotal_marks();
total = (total / totalpassmark) * cse.getExams_idfk().getConvert_to();
}
}
if (cse.getExams_idfk().getOperation() == 5) {
Marks rtnMarks = marksService.getMarksByExamStudentAcademicSubject(
examService.getExamById(cse.getExams_idfk().getExams_ids()[i]),
studentAcademicService.getStudentAcademicById(ids), rtnSubject);
if (rtnMarks != null) {
compare.add(rtnMarks.getScored());
}
}
if (cse.getExams_idfk().getOperation() == 6) {
Marks rtnMarks = marksService.getMarksByExamStudentAcademicSubject(
examService.getExamById(cse.getExams_idfk().getExams_ids()[i]),
studentAcademicService.getStudentAcademicById(ids), rtnSubject);
if (rtnMarks != null) {
List<Grade> rtnGrades = gradeService.listActiveGrade();
for (Grade grd : rtnGrades) {
if (grd.getCondition() != null) {
String cnd[] = { grd.getCondition(), };
try {
cnd = cnd[0].split("-");
if (cnd != null) {
if (rtnMarks.getScored() > Integer.parseInt(cnd[0])
&& rtnMarks.getScored() <= Integer.parseInt(cnd[1])) {
eMarks.setGrade_idfk(grd);
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
}
}
if (cse.getExams_idfk().getOperation() == 5 && compare.size() != 0) {
if (cse.getExams_idfk().getTotal_marks() != 0
&& cse.getExams_idfk().getExams_ids().length >= cse.getExams_idfk()
.getTotal_marks()) {
double bestno[] = new double[(int) cse.getExams_idfk().getTotal_marks()];
for (int i = 0; i < cse.getExams_idfk().getTotal_marks(); i++) {
if (compare.size() != 0) {
int index = 0;
bestno[i] = compare.get(0);
for (int j = 0; j < compare.size(); j++) {
if (compare.get(j) > bestno[i]) {
bestno[i] = compare.get(j);
index = j;
}
}
compare.remove(index);
}
}
eMarks.setCompared_values(bestno);
} else {
double bestno[] = new double[2];
for (int i = 0; i < 2; i++) {
int index = 0;
bestno[i] = compare.get(0);
for (int j = 0; j < compare.size(); j++) {
if (compare.get(j) > bestno[i]) {
bestno[i] = compare.get(j);
index = j;
}
}
compare.remove(index);
}
eMarks.setCompared_values(bestno);
}
} else {
eMarks.setScored(round(total, 2));
}
finalMarks.add(eMarks);
// marksService.saveMarks(eMarks);
}
}
}
// }
// });
// thread.start();
}
marksService.saveBulkMarks(finalMarks);
// marksServiceRepository.saveAll(finalMarks);
// System.out.println(" Marksid "+marksids.size()+" studentacademicid
// "+studentacademicid.size()+" examid "+examid.size()+"
// subjectid"+subjectid.size());
// return
return "redirect:getstudentsbycsssmsubject.cu?currentcsssm=" + currentcsssm + "&subject_id=" + subject_id
+ "&exam_id=" + exam_id;
// return "";
}
Dao:
@Override
public void saveBulkMarks(List<Marks> mark) {
// TODO Auto-generated method stub
Session session = sessionFactory.openSession();
mark.forEach(action -> {
session.merge(action);
});
session.flush();
session.clear();
session.close();
}
Класс сущности:
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import com.cusya.cschool.entity.academics.Grade;
import com.cusya.cschool.entity.academics.StudentAcademic;
import com.cusya.cschool.entity.academics.Subject;
import com.cusya.cschool.entity.exam.Exams;
import com.cusya.cschool.entity.humanresources.Employee;
@Entity
@Table(name = "marks", schema = "public", uniqueConstraints = {
@UniqueConstraint(columnNames = { "student_academic_id_fk", "subject_idfk", "exams_idfk" }) })
public class Marks implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false, name = "id")
private Long id;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "student_academic_id_fk", referencedColumnName = "id")
private StudentAcademic student_academic_id_fk;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "subject_idfk", referencedColumnName = "id")
private Subject subject_idfk;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "exams_idfk", referencedColumnName = "id")
private Exams exams_idfk;
@Column(name = "scored")
private double scored;
@Column(name = "remark")
private String remark;
@Column(name = "compared_values")
private double[] compared_values;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "grade_idfk", referencedColumnName = "id")
private Grade grade_idfk;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "marks_entered_by", referencedColumnName = "id")
private Employee marks_entered_by;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "marks_updated_by", referencedColumnName = "id")
private Employee marks_updated_by;
@Column(name = "entry_date")
private Date entry_date;
@Column(name = "update_date")
private Date update_date;
/**
* @return the id
*/
public Long getId() {
return id;
}
/**
* @param id
* the id to set
*/
public void setId(Long id) {
this.id = id;
}
/**
* @return the student_academic_id_fk
*/
public StudentAcademic getStudent_academic_id_fk() {
return student_academic_id_fk;
}
/**
* @param student_academic_id_fk
* the student_academic_id_fk to set
*/
public void setStudent_academic_id_fk(StudentAcademic student_academic_id_fk) {
this.student_academic_id_fk = student_academic_id_fk;
}
/**
* @return the subject_idfk
*/
public Subject getSubject_idfk() {
return subject_idfk;
}
/**
* @param subject_idfk
* the subject_idfk to set
*/
public void setSubject_idfk(Subject subject_idfk) {
this.subject_idfk = subject_idfk;
}
/**
* @return the exams_idfk
*/
public Exams getExams_idfk() {
return exams_idfk;
}
/**
* @param exams_idfk
* the exams_idfk to set
*/
public void setExams_idfk(Exams exams_idfk) {
this.exams_idfk = exams_idfk;
}
/**
* @return the scored
*/
public double getScored() {
return scored;
}
/**
* @param scored
* the scored to set
*/
public void setScored(double scored) {
this.scored = scored;
}
/**
* @return the grade_idfk
*/
public Grade getGrade_idfk() {
return grade_idfk;
}
/**
* @param grade_idfk
* the grade_idfk to set
*/
public void setGrade_idfk(Grade grade_idfk) {
this.grade_idfk = grade_idfk;
}
/**
* @return the marks_entered_by
*/
public Employee getMarks_entered_by() {
return marks_entered_by;
}
/**
* @param marks_entered_by
* the marks_entered_by to set
*/
public void setMarks_entered_by(Employee marks_entered_by) {
this.marks_entered_by = marks_entered_by;
}
/**
* @return the marks_updated_by
*/
public Employee getMarks_updated_by() {
return marks_updated_by;
}
/**
* @param marks_updated_by
* the marks_updated_by to set
*/
public void setMarks_updated_by(Employee marks_updated_by) {
this.marks_updated_by = marks_updated_by;
}
/**
* @return the entry_date
*/
public Date getEntry_date() {
return entry_date;
}
/**
* @param entry_date
* the entry_date to set
*/
public void setEntry_date(Date entry_date) {
this.entry_date = entry_date;
}
/**
* @return the update_date
*/
public Date getUpdate_date() {
return update_date;
}
/**
* @param update_date
* the update_date to set
*/
public void setUpdate_date(Date update_date) {
this.update_date = update_date;
}
/**
* @return the compared_values
*/
public double[] getCompared_values() {
return compared_values;
}
/**
* @param compared_values
* the compared_values to set
*/
public void setCompared_values(double[] compared_values) {
this.compared_values = compared_values;
}
/**
* @return the remark
*/
public String getRemark() {
return remark;
}
/**
* @param remark
* the remark to set
*/
public void setRemark(String remark) {
this.remark = remark;
}
}