konstantin
konstantin

Reputation: 893

Sqlite problems: database file is locked

I ve got a sqlite database with several tables. One of them called studentsession. Inside my code I am calling plenty times the table for select, insert, updates. After a while I am receiving the message:

java.sql.SQLException: [SQLITE_BUSY] The database file is locked (database is locked)

One instance of the calling is the following:

    String query3 = "select * from studentssession where id= ?  and math= ? and level = ?";
    PreparedStatement pst__ = connectionUsers.prepareStatement(query3);
    pst__.setString(1, x);
    pst__.setString(2, x1);
    pst__.setString(3, x2); 
    ts2 = pst__.executeQuery();

I am trying to figure out if I have or not to close every time the prepared statement, and if there is a case that this is causing my problems.

EDIT: Is it possible to have a check for possible open references in the database, using for example a javafxbutton?

EDIT: Is there a way that I can check in my code whether there is a problem in the references to the table and locate and possible close them?

Upvotes: 0

Views: 871

Answers (1)

U.Swap
U.Swap

Reputation: 1949

It's probably due to you having multiple open references to the sqlite database. I'd start by closing your PreparedStatement in a finally block inside your while loop.

PreparedStatement pst__ = null;
try{   
  pst__ = connectionUsers.prepareStatement(query3);
  pst__.setString(1, x);
  pst__.setString(2, x1);
  pst__.setString(3, x2); 
  ts2 = pst__.executeQuery();
}finally{
  if(pst__ != null) {
    pst__.close();
  }
}

You should also close the database connection at the end of everything.

Also it is a bad practice to use multiple connections when connecting to SQLite. See

http://touchlabblog.tumblr.com/post/24474398246/android-sqlite-locking

Set your poolsize maxactive to 1 and try out.

Upvotes: 1

Related Questions