我创建了此代码,用于为3个表添加数据。但是我收到了一个错误,超出了锁定等待时间。在我的数据库中,我正在为takerslist,q_enrolls和测试表添加值。我从qBank和问题表中获取了一些数据。 takerlist外键是stdId。我认为这不重要。 q_enroll的外键是qId(引用问题表),qBankId(引用QBank)和testId(引用测试)。
@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);
}
}
答案 0 :(得分:0)
您可以显式设置数据库的锁定超时(因为您的更新可能会使事务保持打开状态太长时间)。对于MySQL,您可以找到详细说明here
基本上你用/etc/my.cnf这样的命令用这条线永久地设置它
[mysqld]
innodb_lock_wait_timeout=120
并重启mysql。如果此时无法重启mysql,请运行:
SET GLOBAL innodb_lock_wait_timeout = 120;
您也可以在会话期间设置
SET innodb_lock_wait_timeout = 120;