A lock could not obtained within the time requested issue

别等时光非礼了梦想. 提交于 2019-12-10 12:14:56

问题


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:

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;
 }

回答1:


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

  • With squirrel
  • With ij tool

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 committing the 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 !




回答2:


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




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/15582682/a-lock-could-not-obtained-within-the-time-requested-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!