Getting [SQLITE_BUSY] database file is locked with select statements

前端 未结 8 2022
广开言路
广开言路 2020-11-28 13:18

If I run multiple threads against my web app I get:

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


        
相关标签:
8条回答
  • 2020-11-28 13:45

    Try @Transactional(readonly=true) for those methods that only do reads. Maybe that works for you.

    0 讨论(0)
  • 2020-11-28 13:50

    Everytime you establish a connection make sure to close it after the work is done, It worked for me like if you are using

    Connection con = null;
    PreparedStatement pst = con.prepareStatement("...query... "); 
    /*
     do some stuff 
    */
    pst.executeQuery();
    pst.close();
    con.close();
    
    0 讨论(0)
  • 2020-11-28 13:59

    Note also that this may happen if you accidentally forget to close your connection:

    Connection connection;
    try {
      Statement statement = connection.createStatement();
      ResultSet resultSet = statement.executeQuery(QUERY);
      if (resultSet.next()) { /* do something */ }
    catch (SQLException e) { /* handle exception */ }
    finally {
      if (connection != null) {
        try {
          connection.close(); // <-- This is important
        } catch (SQLException e) {
          /* handle exception */
        }
      }
    }
    

    While the first database connection may work well once the server is started, subsequent queries may not, depending on how the connection pool is configured.

    0 讨论(0)
  • 2020-11-28 14:01

    After some googling I found that 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.

    0 讨论(0)
  • 2020-11-28 14:04

    I experienced the same problem, even though all connections, resulsets and statements were closed, I still had the error. The problem for me was using the DB browser plugin in Intellij to visualize and manage tables. Disconnecting the database from this tool solved the problem. So make sure that no external tool is connecting to the database and locking tables.

    0 讨论(0)
  • 2020-11-28 14:06

    There should be only ONE connection with your application. you can use this to ensure.

    public class SqliteHelper {
    private static Connection c = null;
    public static Connection getConn() throws Exception {
        if(c == null){
        Class.forName("org.sqlite.JDBC");
        c = DriverManager.getConnection("jdbc:sqlite:D:/test.db");
        }
        return c;
        }
    }
    
    0 讨论(0)
提交回复
热议问题