0

我创建了这个代码来为 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);
    }
}
4

1 回答 1

0

您可以显式设置数据库的锁定超时(因为您的更新可能会使事务保持打开太久)。对于 MySQL,您可以在此处找到详细说明

基本上你在 /etc/my.cnf 中使用这样的命令永久设置它

[mysqld]
innodb_lock_wait_timeout=120

并重新启动mysql。如果此时无法重新启动 mysql,请运行以下命令:

SET GLOBAL innodb_lock_wait_timeout = 120; 

您也可以在会话期间设置它

SET innodb_lock_wait_timeout = 120; 
于 2013-08-28T14:24:04.533 回答