You should try with JDBC Connection Pooling. There are great tutorials are available. Using Connection Pool generally improves performance by 3x.
From: Emanuel Freitas [mailto:ejsfreitas@gmail.com] Sent: Friday, October 29, 2010 7:52 PM To: pgsql-jdbc@postgresql.org Subject: Exception in DriverManager.getConnection creates multiple connections in SYN_SEND state
Hello,
I'm running a server that receives approximately 300 requests per second. For each request I need to do some operations in a database. So, when I receive a request i check if there is any active connection to the database and if not i create one. The code for create the connection is something like this:
If the database host is accessible the getConnection method works and I return the connection, on the other hand, if for some reason the host is not accessible I get a SQLException after 3 seconds (loginTimeout). That's exactly what i want to do.
The problem is, for each time it executes the "getConnection" method one connection to the database is created and if the host is not accessible the connection stays in SYN_SENT state for alot of time...
As I receive alot of requests per second these connections are created until it reaches the max connections allowed by the SO (linux).
Can you tell me what I'm doing wrong or what can I do to prevent this?