Search code examples
javamysqlmultithreadingasynchronousc3p0

Non-blocking MySQL updates with java?


For a multiplayer game I'm working on I'd like to record events to the mysql database without blocking the game update thread so that if the database is busy or a table is locked the game doesn't stop running while it waits for a write.

What's the best way to accomplish this?

I'm using c3p0 to manage the database connection pool. My best idea so far is to add query update strings to a synchronized list with an independent thread checking the list every 100ms and executing the queries it finds there.


Solution

  • My best idea so far is to add query update strings to a synchronized list with an independent thread checking the list every 100ms and executing the queries it finds there.

    This sounds good to me if you don't want to block your main thread, except that don't use synchronized list and 100ms polling, but use some BlockingQueue implementation instead. LinkedBlockingQueue should do the job, if you make it unbounded it will never block your main thread.