1
votes

HI: I have a multi thread Java database application, we have to create a customized database pooling. The reason is that some of our preparedstatement has to be cached in the connection. We have our primitive solution based on one free hashmap, the other locked hashmap. With hashmap, we have to use synchronized method, then our throughput is influenced. The choice is to use concurrentHashMap so that synchronization part would be as small as possible. Are there some sample codes? Or shall I download apache DBCP source codes to read?

2
Where does requirement to cache prepared statements is coming from? How long do you need to cache them?Olaf
sample code for a thread pool or sample code for using a concurrent hash-map. P.S are you using the hash map just to see whether a connection is free or not?? wont a FIFO queue be a much better altenative.. afterall there cant be so many connections as to burden your server memory wiseOsama Javed
I did a simple class for maintaining a simple connection pool if you want i can upload it in a couple of hours (when I get home ) though I dont know whether that is what you are looking forOsama Javed
To Osama Javed: How could I get your sample codes? It would be great to see some examples.user84592

2 Answers

1
votes

Using synchronized will cost you about 1-2 microseconds. If this is critical to you, you shouldn't be using JDBC. IMHO. Just accessing a service over a TCP connection is likely to cost 100 micro-seconds and many JDBC databases have a latency of 1-10 milli-seconds.

I suspect that a few milliseconds per query/update is fine for you in which case using synchronization is unlikely to matter.

Depending on how many threads you have, you can have a thread local connection for each thread. This reduces the overhead as much as possible.

0
votes

You have a multi-threaded application, but do many threads use the same database connection at the same time ? As the PreparedStatement cache will be done at the Connection level, if you connections are used by a single Thread at a time (which I think should be the case), you do not need synchronization.