Note that there are some explanatory texts on larger screens.

plurals
  1. POJava single worker thread for SQL update statements
    primarykey
    data
    text
    <p>I'm working on a Java-based server in which I will have multiple threads (one thread for each connected user + some extra). There will be some database connection involved, so I was thinking that each time the server makes a <em>SELECT</em> query to the database it will start a new thread for this, to prevent blocking from the current thread. I'm planning on using a connection pool for this and I think I know how to do that. (I've looked into <a href="http://www.mchange.com/projects/c3p0/index.html" rel="nofollow">C3P0</a>) However, there will be a lot of <em>UPDATE</em> statements involved also, but it's not important that these are ran directly, it's ok with a delay here. And since there might be a lot of <em>UPDATE</em> statements, I'm thinking of having a single worker thread for all <em>UPDATE</em> statements. As I see it, this will have the advantage of being able to re-use <code>PreparedStatement</code>-objects</p> <p><strong>The question:</strong> How can I, from the other threads, tell the <em>UPDATE</em>-worker thread to run some statements? I know about multithreading and how to make threads "talk" to each other using synchronized blocks, but with the database involved it suddenly feels more complex. I have read that prepared statements and connections should not be shared between threads.</p> <p><strong>The idea I have right now on how to solve it:</strong> (doesn't feel like a good solution)</p> <p>Use a <code>LinkedBlockingQueue</code> (or another kind of Queue) of a custom class with information about which kind of UPDATE statement to call and which parameters to send it. And then the worker thread will read from this queue when it's notified (which it will be when something is added to the queue) and there it will run the appropriate method which will use the appropriate prepared statement, set the params, and call it.</p> <p><strong>Edit:</strong> A bad think that I see myself with this approach is that the params might be ints, String, double, or whatever. How to store them in the custom class? Doesn't feel good to store them all as String.</p> <p>Am I on the right track here or is there a better way to solve this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload