6

I am doing a project in wicket How to solve the problem. I came across such a message: WicketMessage: Can't instantiate page using constructor public itucs.blg361.g03.HomePage()

Root cause:

java.lang.UnsupportedOperationException: [SQLITE_BUSY] The database file is locked (database is locked) at itucs.blg361.g03.CategoryEvents.CategoryEventCollection.getCategoryEvents(CategoryEventCollection.java:41)

 public List<CategoryEvent> getCategoryEvents() {
    List<CategoryEvent> categoryEvents = new 
            LinkedList<CategoryEvent>();
    try {
        String query = "SELECT id, name, group_id"
                + " FROM event_category";
        Statement statement =  this.db.createStatement();
        ResultSet result = statement.executeQuery(query);
        while (result.next()) {
            int id = result.getInt("id");
            String name = result.getString("name");
            int group_id = result.getInt("group_id");
            categoryEvents.add(new CategoryEvent(id, name, group_id));
        }
    } catch (SQLException ex) {
        throw new UnsupportedOperationException(ex.getMessage());
    }
        return categoryEvents;  
}

at itucs.blg361.g03.HomePage.(HomePage.java:71)

        categories = categoryCollection.getCategoryEvents();

at java.lang.reflect.Constructor.newInstance(Constructor.java:525)

bert
  • 7,566
  • 3
  • 31
  • 47
Burak Dağlı
  • 512
  • 2
  • 10
  • 33
  • Well, the Exception says it all: the database is locked. This has nothing to do wicket, but relates entirely to your database. Also, I would suggest to use proper Exceptions. UnsupportedOperationException() sematicly wrong here. – bert Dec 19 '11 at 10:56
  • I am using this way: catch(Exception e) { e.printStackTrace(); Instead of this one: catch (SQLException ex) { throw new UnsupportedOperationException(ex.getMessage()); } Now it seems ok. Any other idea? – Burak Dağlı Dec 19 '11 at 15:47

2 Answers2

16

Sqlite allows only one writer to the whole database at a time and, unless you selected "WAL" journal mode, no reader while writing. Moreover unless you explicitly ask it to wait, it simply returns the SQLITE_BUSY status for any attempt to access the database while conflicting operation is running.

You can tell sqlite to wait for the database to become available for a specified amount of time. The C-level API is sqlite3_busy_timeout; I never used sqlite from Java though, so I don't know where to find it there.

Valmond
  • 2,897
  • 8
  • 29
  • 49
Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
7

(...) tell sqlite to wait for the database to become available for specified amount of time.

In order to do it from Java, run the following statement just like a simple SQL statement:

pragma busy_timeout=30000; -- Busy timeout set to 30000 milliseconds
Stephan
  • 41,764
  • 65
  • 238
  • 329