Jdbc and multithreading..

Not sure if this is the right place to post this thread…

Anyway-atm our server is using only one connection towards database…since we use nio, we only have one main server thread+one CleanupThread which does misc stuff like logging server heartbeat and cleaning users if their connections died(dial up disconnect etc).The latter gave us many troubles…
I dont want to get into thread pooling, i just want to share this 1 connection among those 2 threads.
Basicaly my question is this:do i get references to the same statement by calling connection.createStatement or do i get multiple statements-in documentation it says that it returns a new default Statement object…

Or is the approach with sharing connection bad?Dont have time to look/read a lot now :S…Opinions are very appreiciated.

Ty

[quote]do i get references to the same statement by calling connection.createStatement or do i get multiple statements-in documentation it says that it returns a new default Statement object…
[/quote]
different statement objects.

HOWEVER: Your approach is not very good. Please try to use a connection pool for your database connections. Right now you only have 2 thread -> BUT: You will have more in the future. Also, you cannot do transactions over a shared connection.

At least use 2 connections - one for each thread! Sharing the connection will give you database access errors … trust me

first impression: I’d go database pooling.

secund one is that I can’t really opt for niether since the systems size and function is unclear.

Ok, tnx for the feedback…
I guess that i ll try using a connection pool…c3po?

It takes 5 minutes to write your own connection pool, theres nothing magical about it,

Just use two connections. Your performance would be fine if you had 200 connections, so its really nothing to worry about,.

And if you don’t want to make everything from scratch or study working source code:
DBPool http://jakarta.apache.org/commons/dbcp/
Generic pool http://jakarta.apache.org/commons/pool/

You can use dbcp or take pool package and create few own classes and its ready.