我创建了这个代码来为 3 个表添加数据。但是我得到了一个错误,超过了锁定等待时间。在我的数据库中,我正在为 takerslist、q_enrolls 和测试表添加值。我从 qBank 和问题表中获取了一些数据。takerlist 外键是 stdId。我觉得这里不重要。q_enroll 的外键是 qId(References question table)、qBankId(references QBank) 和 testId(references test)。
@Override
public int add(Test e, Connection connection) throws SQLException, ClassNotFoundException, RemoteException {
String query = "INSERT INTO Test VALUES(?,?,?,?,?,?,?)";
Object[] data = {e.getTestId(), e.getTestName(), e.getTestFrom(), e.getTestTo(), e.getTotalQuestions(), e.getTestDate(), e.getPassMark()};
try {
connection.setAutoCommit(false);
int res = DBHandle.setData(connection, query, data);
if (res > 0) {
List<TakerList> list = e.getListTakers();
TakerListManagementModel takerListModel = new TakerListManagementModel();
QuestionEnrollManagementModel enrollModel = new QuestionEnrollManagementModel();
takerListModel.setAddCommonBehavior(true);
enrollModel.setAddCommonBehavior(true);
List<List<BankQuestion>> selQs = null;
String testId = null;
for (TakerList tl : list) {
int resTakers = takerListModel.performAdd(tl);
if (resTakers > 0) {
selQs = e.getListQBanks();
testId = tl.getTestId();
} else {
connection.rollback();
return 0;
}
}
for (List<BankQuestion> list1 : selQs) {
for (BankQuestion bankQuestion : list1) {
QuestionEnroll enroll = new QuestionEnroll(bankQuestion.getqId(), testId, bankQuestion.getqBankId());
int resQList = enrollModel.performAdd(enroll);
if (resQList==0) {
return 0;
}
}
}
connection.commit();
return 1;
} else {
connection.rollback();
return 0;
}
} catch (SQLException ex) {
connection.rollback();
throw ex;
} finally {
connection.setAutoCommit(true);
}
}