Android.database.sqllite.SQLiteDatabaseLocked Exception:database is locked (code 5): ,while compiling:PRAGMA journal_mode

3.1k views Asked by At

I use SQLite for a Gluon Mobile App in my Android. When I access the database for the first time it works without problem, but the another time I have a Exception:

"Android.database.sqllite.SQLiteDatabaseLocked Exception:database is locked (code 5)

The code to access the database is:

if (Platform.isAndroid()) {
  Class.forName("org.sqldroid.SQLDroidDriver");

  try {
    dir = Services.get(StorageService.class)
               .map(s -> s.getPrivateStorage().get())
               .orElseThrow(() -> new IOException("Error: PrivateStorage not available"));
    File db = new File(dir, DB_NAME);
    DBUtils.copyDatabase("/databases/", dir.getAbsolutePath(),   
DB_NAME);
    dbUrl = dbUrl + db.getAbsolutePath();
    System.out.println(dbUrl);
    c = DriverManager.getConnection(dbUrl);
  } catch (IOException ex) {

    Alert alert = new Alert(AlertType.CONFIRMATION, ex.getClass().getName());
    alert.setContentText(ex.getMessage());
    alert.showAndWait();
  }

  c.setAutoCommit(false);
  System.out.println("Opened database successfully");

  stmt = c.createStatement();
  ResultSet rs = stmt.executeQuery("SELECT * FROM COMPANYn;");

  while (rs.next()) {
    ObservableList<String> items = FXCollections.observableArrayList();
    String name1 = rs.getString("name");
    // System.out.println(name1);

    items.addAll(name1);

    ET.getItems().addAll(items);

    ET.getSelectionModel().select(0);

  }
  rs.close();
  stmt.close();
  c.close();

Somebody can help me please. Thank for help.

1

There are 1 answers

0
superbeef150 On

I was receiving this same error in the same situation (connecting to sqlite, using Gluon and running on Android). Worked fine on the desktop, but got the same error as in your title for Android.

Interestingly, I found that my 2nd SELECT statement rather than my 1st was the one throwing the error. The 2nd statement was in a separate method, after closing the statement, resultset, and connection from the 1st one.

I eventually found that not closing my sqlite connections seemed to resolve this issue for me. This seemed odd since I'm used to closing them at the end of a transaction, method, etc, but it seems this is not abnormal in sqlite.

See here for some related info on the topic:

Android: Cannot perform this operation because the connection pool has been closed