1

I'm trying to implement a Java Swing GUI App with a game. The game is to count how many times a button is clicked in 5 seconds.

I'm using sqlite4java in one of my Java GUI projects. It is pretty minimalistic therefore supports only single-threaded apps.

I want to execute some SQL query after the 5 seconds is up. I have a Thread started in my button's onclick listener whose run() method is implemented like the following:

run() {
 timeLeft = 5;
 score = 0;
 while(timeLeft>0)
      Thread.sleep(100);
      timeLeft -= 0.1;
      update left time on GUI;
  }
  // time is up
  execute some SQLite INSERT query here;
}

And since sqlite4java is single-thread supported, it throws an exception:

SQLite error:com.almworks.sqlite4java.SQLiteException:
[-98] DB[1] confined(Thread[main,5,]) used (Thread[Thread-3,6,main]) 

How can I possibly execute after thread is finished (outside the thread)? It is throwing an exception because the callee thread and the thread in which database is instantiated (Main thread) are not the same.

How can I make main thread signaled (and handle this signal in main thread) after this thread terminates?

All I want to achieve is to execute query to add user's score to high scores list. That's not an homework, I'm trying to develop a proof-of-concept application for my own ORM framework.

2 Answers 2

9

As @corlettk advises, you need to have a separate thread for database operations. sqlite4java comes with SQLiteQueue, which does that for you. There's a tutorial and javadoc with example.

Make sure you wrap ALL database operations with SQLiteJob(s) and pass them to the queue.

Hope this helps! Igor

Sign up to request clarification or add additional context in comments.

Comments

0

Ahmet,

I guess you'll have to do ALL your "database stuff" (including dis/connect) on ONE thread, which should probably be dedicated to this purpose; maybe even "hidden" behind a request-queue.

Good luck with that. Can you "just" swap RDBMS's instead?

Cheers. Keith.

2 Comments

@Keith, actually I'm trying to find a way to communicate between threads. sqlite4java does not allow to execute on other threads. I might implement a request queue and process it using .wait() and .notifyAll(). But looking for more elegant solution. Thanks.
@Ahmit, code.google.com/p/sqlite4java says: "[An] application may open several connections to the same database from different threads." Which to me means you could open the database on dedicated 'DB-service thread' and then 'hide' that thread behind a threadsafe request queue. Each request would be a Future, to allow you to read back results, and check for errors. It's still pretty kludgy (delayed error detection) but it could work, I guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.