let's say I have a database connection in the class Database which offers the following public methods

public synchronized void open() {}
public synchronized void close() {}
public synchronized void addData(Object data) {}

My main program looks as follows:

Database databaseConnector = new Database();

for (Feed feed : feedList) {
    Timer timer = new Timer();
    FeedTimerTask timerTask = new FeedTimerTask(feed, databaseConnector, stopWords);
    // Run the task every five minutes.
    timer.scheduleAtFixedRate(timerTask, 0, 1000 * 60 * 5);

It is easy to see, that an unknown number of threads is repeated every 5 minutes. These threads itself read data from a feed, and try to save it to a database afterwards.

Since they are all using the same Database connection, it seems not clever to me to open() and close() the connection for every feed. Especially since one reader can close the connection while another is still trying to write something to this feed.

Instead I thought that I want something like this timeline:

(open) (write1) (write2) ... (writeN) (close) ... (wait 5 mins) (open) ... (close)

The idea is, that every 5 minutes exactly one connection is opened and after all the threads have written their data, is closes again. So far, I have the following solution.

I added private int openConnections; to the Database and modified the code as follows:

public synchronized void open() {
    if(openConnections++ == 0) {
        // Connect to the database, because we were not connected before

public synchronized void(close) {
    if(--openConnections == 0) {
        // Close to the connections because this was the last one

Now, in every Thread, I do the following:


With this strategy, it is always checked, if there is still a need for an open connection and in that case, the connection is not closed. And I don't plan to have more threads than MAX_INT. My question is: Is this a good idea? Are there better ways to achieve the same behavior? It works completely fine but I am wondering if there is another solution which I should prefer.

  • 6
    Database connection pooling exists for a reason (and you have indeed come across one of those reasons). However writing your own non-thread-safe bicycle is not a good idea. – Ordous Jun 20 '16 at 18:25

Use database connection pooling. I don't recommend writing your own. Here's an explanation on StackOverflow. You can use c3p0 or read up more on your own by searching for "connection pooling."

  • Thanks for the link. This raises the question when to close a database connection? Since I don't need the connection for 5 minutes I thought it would be a good idea to close it and open it 5 minutes later when all the threads fire. Should I instead leave it open the whole time? – Freddy Jun 20 '16 at 18:57
  • @Freddy likely you just keep a fixed number open at all times. You can finagle with min=X and max=Y, set them to autoclose after X minutes idle, etc. but I would aim for something simple. – djechlin Jun 20 '16 at 18:58
  • I agree, simple is better. In my case the behavior of the application is completely deterministic. It will always sleep for 5 mins, there is no user interaction whatsoever. Should I really keep the connections open? This seems a bit too complicated. For non-deterministic applications, I would agree like a server which has user requests which are non-predictable. – Freddy Jun 20 '16 at 19:03
  • 1
    @Freddy keeping the connections open is not complicated. If it's not expensive then probably just keep them open. You can put a timeout if you want. It shouldn't matter most of the time. – djechlin Jun 20 '16 at 19:06
  • According to (this)[stackoverflow.com/questions/8968125/mongodb-connection-pooling] question, the mongoDB driver does connection pooling for me. So it should be safe for me to close() the connection after I finish my task. Would you agree with that? Let's assume I actually want to close the connections after every run and I don't know the order of the threads. Is this then a reasonable idea? Because when I now call close and the other thread is in the middle of adding data, it fails. – Freddy Jun 20 '16 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.