If I run rẩy multiple threads against my trang web tiện ích I get:

java.sql.SQLException: [SQLITE_BUSY]  The database tệp tin is locked (database is locked)
    at org.sqlite.DB.newSQLException(DB.java:383)
    at org.sqlite.DB.newSQLException(DB.java:387)
    at org.sqlite.DB.execute(DB.java:339)
    at org.sqlite.PrepStmt.executeQuery(PrepStmt.java:75)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)

I bởi know that only one thread can write đồ sộ a sqlite database but I'm only reading from the database. So why bởi I get this error message ?

BTW: My connection pool looks lượt thích this:


    
    
    
    
    
    
    
    

The setup is: Java 1.6, Tomcat 7.0.34, Spring 3.2, Hibernate 3.6.9 and sqlite3 3.7.2

Regards Roger

asked Dec 15, 2012 at 9:24

1

For anyone who's having issues with it in WSL2:

Happened đồ sộ bầm when I was using WSL2 và Datagrip, even tho the database wasn't busy.

It turns out that Datagrip has tried đồ sộ connect đồ sộ the database tệp tin that existed inside WSL2 via Windows' sqlite3.

Moving the tệp tin from WSL2 đồ sộ a Windows tệp tin directory seems đồ sộ solve this issue

answered Jan 5, 2022 at 16:51

2

There should be only ONE connection with your application. you can use this đồ sộ 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;
    }
}

answered Jun 15, năm trước at 18:27

Note also that this may happen if you accidentally forget đồ sộ close your connection:

Connection connection;
try {
  Statement statement = connection.createStatement();
  ResultSet resultSet = statement.executeQuery(QUERY);
  if (resultSet.next()) { /* bởi 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.

trnelson

2,7432 gold badges25 silver badges41 bronze badges

answered Oct 21, năm trước at 20:12

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

Connection con cái = null;
PreparedStatement pst = con cái.prepareStatement("...query... "); 
/*
 bởi some stuff 
*/
pst.executeQuery();
pst.close();
con.close();

answered May 1, 2018 at 5:15

1

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

answered Nov 10, 2020 at 7:52

1

In my case, there are thread using sqlite connection in the background, which caused this error.

  1. close sqlitebrowser
  2. close electron tiện ích ( maybe need restart)
  3. re-run your program.

answered Apr 18, 2021 at 11:41

For bầm the problem was that I was opening too much Sessions So I made the session field in my DAO class static

answered Dec 7, 2018 at 16:49

Thanks from bowman han, I added a piece of code đồ sộ his solution and it worked for bầm.

private static Connection c = null;
public static Connection connect() throws Exception {

    if (c == null) {
        c = (Connection) DriverManager.getConnection(url);
    } else {
        c.close();
        c = (Connection) DriverManager.getConnection(url);
    }
    return c;
}

answered Nov 9, 2019 at 11:21

You have opened another application containing the database, Try đồ sộ close that application and run rẩy your program again. This worked for me

answered Aug 26, 2021 at 23:23

1

Always work with me

private static Connection con cái = null; 

public static Connection sqlite(){

        try {
            if (con != null) {
                con cái.close();
            }
            return   con cái =(Connection) DriverManager.getConnection(url);
        } catch (SQLException ex) {
            throw new RuntimeException( ex);
        }

    }

The most elegant and worked solution that I found here is:

move the database tệp tin đồ sộ a path accessible from both WSL2 and Windows.

Steps:

  1. Move the Database File đồ sộ a Windows Path: For example, place foo.db on your Windows Desktop.
  2. Create a Symbolic Link in WSL2: Use the ln -s command đồ sộ create a symlink in WSL2 pointing đồ sộ the Windows tệp tin. Note the use of the -s flag for creating a symbolic links.
  3. Accessing Windows Files in WSL2: Windows files are accessible in WSL2 through the /mnt/c/... path.

Command:

ln -s /mnt/c/Users/Tanut/Desktop/foo.db ./foo.db

I had this issue with SqlDelight, I was using nested transactions.

It turned out some DI code was creating multiple instances of the AndroidSqliteDriver, which were likely not correctly closed. I fixed it by making sure AndroidSqliteDriver was only instantiated once.

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

answered Dec 15, 2012 at 9:30

1