java 无法在请求的时间内获得锁定问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15582682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:08:54  来源:igfitidea点击:

A lock could not obtained within the time requested issue

javadatabasenetbeansderby

提问by Wayne Daly

The title is the error I'm getting, when I click load my program freezes. I assume its because I'm doing a statement inside a statement, but from what I see its the only solution to my issue. By loading I want to just repopulate the list of patients, but to do so I need to do their conditions also. The code works, the bottom method is what I'm trying to fix. I think the issue is that I have 2 statements open but I am not sure. load:

标题是我得到的错误,当我点击加载我的程序冻结时。我认为这是因为我在声明中做声明,但从我看来,这是解决我问题的唯一方法。通过加载,我只想重新填充患者列表,但要这样做,我还需要满足他们的条件。代码有效,底部方法是我正在尝试修复的方法。我认为问题是我有 2 个声明打开,但我不确定。加载:

public void DatabaseLoad()
{
    try
    {
        String Name = "Wayne";
        String Pass= "Wayne";
        String Host = "jdbc:derby://localhost:1527/Patients";
        Connection con = DriverManager.getConnection( Host,Name, Pass);
        PatientList.clear();


        Statement stmt8 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,        
             ResultSet.CONCUR_UPDATABLE);
        String SQL8 = "SELECT * FROM PATIENTS";
        ResultSet rs8 = stmt8.executeQuery( SQL8 );
        ArrayList<PatientCondition> PatientConditions1 = new ArrayList();

        while(rs8.next())
        {
            PatientConditions1 = LoadPatientConditions();
        }

        Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,    
            ResultSet.CONCUR_UPDATABLE);
        String SQL = "SELECT * FROM PATIENTS";
        ResultSet rs = stmt.executeQuery( SQL );

        while(rs.next())
        {
            int id = (rs.getInt("ID"));
            String name = (rs.getString("NAME"));
            int age = (rs.getInt("AGE"));
            String address = (rs.getString("ADDRESS"));
            String sex = (rs.getString("SEX"));
            String phone = (rs.getString("PHONE"));

            Patient p = new Patient(id, name, age, address, sex, phone,
                PatientConditions1);
            PatientList.add(p);
       }

        UpdateTable();
        UpdateAllViews();

        DefaultListModel PatientListModel = new DefaultListModel();

        for (Patient s : PatientList) {
            PatientListModel.addElement(s.getAccountNumber() + "-" + s.getName());
        }

        PatientJList.setModel(PatientListModel);

       }

    catch(SQLException err)
    {
        System.out.println(err.getMessage());
    }


}

This is the method that returns the arraylist of patient conditions

这是返回患者条件数组列表的方法

public ArrayList LoadPatientConditions()     
{ 
    ArrayList<PatientCondition> PatientConditionsTemp = new ArrayList();
    try
    {
        String Name = "Wayne";
        String Pass= "Wayne";
        String Host = "jdbc:derby://localhost:1527/Patients";
        Connection con = DriverManager.getConnection( Host,Name, Pass);
        Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,   
            ResultSet.CONCUR_UPDATABLE);
        String SQL = "SELECT * FROM PATIENTCONDITIONS";
        ResultSet rs5 = stmt.executeQuery( SQL );

        int e = 0;
        while(rs5.next())
        {
            e++;
            String ConName = (rs5.getString("CONDITION"));
            PatientCondition k = new PatientCondition(e,ConName);
            PatientConditionsTemp.add(k);
        }   
     }
     catch(SQLException err)
     {
         System.out.println(err.getMessage());
     }

     return PatientConditionsTemp;
 }

回答by Aditya Gupta

Had a similar problem. I was connecting to derby db hosted on local server. I created 2 simultaneous connections:

有类似的问题。我正在连接到本地服务器上托管的 derby 数据库。我创建了 2 个同时连接:

  • With squirrel
  • With ijtool
  • 松鼠
  • 使用ij工具

When a connection makes a modification on a table, it first gets a lock for the particular table. This lock is released by the connection only after committingthe transaction. Thus if the second connection tries to read/write the same table, a msg prompts saying:
ERROR 40XL1: A lock could not be obtained within the time requested

To fix this, the connection which modified the table has to commit its transaction.

Hope this helps !

当连接对表进行修改时,它首先获得特定表的锁。只有在提交事务后,连接才会释放此锁。因此,如果第二个连接尝试读/写同一个表,消息会提示说:
错误 40XL1:无法在请求的时间内获得锁定

要解决此问题,修改表的连接必须提交其事务。

希望这可以帮助 !

回答by Bryan Pendleton

Here is a good place to start: http://wiki.apache.org/db-derby/LockDebugging

这是一个很好的起点:http: //wiki.apache.org/db-derby/LockDebugging

回答by zvikow

You need to close your statement and result set as well so that when you restart your program they won't be open. Add stmt.close();and rs.close();at the end of your lines of code within the try and catch statement.

您还需要关闭语句和结果集,以便在重新启动程序时它们不会打开。在 try 和 catch 语句中的代码行末尾添加stmt.close();rs.close();

回答by Rush

Why could you not use the same connection object to do both the queries? Like pass that connection object to the LoadPatientConditions() as a parameter and use it there.

为什么不能使用相同的连接对象来执行这两个查询?就像将该连接对象作为参数传递给 LoadPatientConditions() 并在那里使用它一样。