From: Sumit Pandya <sumit.pandya@elitecore.com>
To: 'Emanuel Freitas' <ejsfreitas@gmail.com>
To: pgsql-jdbc@postgresql.org
Subject: Re: Exception in DriverManager.getConnection creates multiple connections in SYN_SEND state
Date: Mon, 1 Nov 2010 14:41:51 +0530
Message-ID: <000301cb79a4$d1e7fbf0$3902a8c0@elitecore.com> (raw)
In-Reply-To: <AANLkTik0rTQ6HhbrY66qpaU+E0jsL9Ra7Lt=vR3FvZja@mail.gmail.com>
References: <AANLkTik0rTQ6HhbrY66qpaU+E0jsL9Ra7Lt=vR3FvZja@mail.gmail.com>
Dear Emanuel,
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:
private Connection initConn() {
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException cnfe) {
System.err.println("Error on Driver");
return null;
}
try {
Connection db =
DriverManager.getConnection("jdbc:postgresql://192.168.1.2:5432/test?loginTi
meout=3", "test", "1234");
return db;
} catch (SQLException sqle) {
System.err.println("Error on
Connection");
return null;
}
}
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?
Thank You!
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: sumit.pandya@elitecore.com, ejsfreitas@gmail.com
Subject: Re: Exception in DriverManager.getConnection creates multiple connections in SYN_SEND state
In-Reply-To: <000301cb79a4$d1e7fbf0$3902a8c0@elitecore.com>
References: <AANLkTik0rTQ6HhbrY66qpaU+E0jsL9Ra7Lt=vR3FvZja@mail.gmail.com> <000301cb79a4$d1e7fbf0$3902a8c0@elitecore.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox