Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id ED9FC1337B3E for ; Mon, 1 Nov 2010 08:54:29 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 49500-01 for ; Mon, 1 Nov 2010 11:54:22 +0000 (UTC) Received: from news.hub.org (news.hub.org [200.46.204.72]) by mail.postgresql.org (Postfix) with ESMTP id 8E5881337B47 for ; Mon, 1 Nov 2010 08:54:17 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.14.4/8.14.4) with ESMTP id oA1BsChh050011 for ; Mon, 1 Nov 2010 08:54:12 -0300 (ADT) (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.14.4/8.14.4/Submit) id oA1BPZUq038400 for pgsql-jdbc@postgresql.org; Mon, 1 Nov 2010 08:25:35 -0300 (ADT) (envelope-from news) From: Lew X-Newsgroups: pgsql.interfaces.jdbc Subject: Re: Exception in DriverManager.getConnection creates multiple connections in SYN_SEND state Date: Mon, 01 Nov 2010 07:25:49 -0400 Organization: albasani.net Lines: 50 Message-ID: References: <000301cb79a4$d1e7fbf0$3902a8c0@elitecore.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit In-Reply-To: <000301cb79a4$d1e7fbf0$3902a8c0@elitecore.com> Cancel-Lock: sha1:rq8ERl8uNbsLHg7QpgjFT93TDGg= User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.14) Gecko/20101006 Thunderbird/3.0.9 Injection-Info: news.albasani.net; logging-data="t8OhI89Y/MD7AoaVANkUK1PbItAWyWQdEBi2+UG6eJryDJ/tq/kpevur9wYl6kyy16OqpsaLg+rtqSHNczODqjx0ja9kkPvILCuiqjNSVrEj1pBwYQu+Kzei9nQbJnwk"; mail-complaints-to="abuse@albasani.net" To: pgsql-jdbc@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.009 tagged_above=-10 required=5 tests=BAYES_40=-0.001, NORMAL_HTTP_TO_IP=0.001, T_RP_MATCHES_RCVD=-0.01, WEIRD_PORT=0.001 X-Spam-Level: X-Archive-Number: 201011/2 X-Sequence-Number: 21123 Emanuel Freitas wrote: > 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 [sic] check if there is any active connection to > the database and if not i [sic] create one. The code for create the connection > is something like this: > > private Connection initConn() { > > try { > Class.forName("org.postgresql.Driver"); You only need to load the driver class once. Load it in a static initializer block. > } catch (ClassNotFoundException cnfe) { > System.err.println("Error on Driver"); Consider using a logging framework like log4j or java.util.logging. > return null; Consider stopping the application for this particular exception. > } > > try { > > Connection db = > DriverManager.getConnection("jdbc:postgresql://192.168.1.2:5432/test?loginTimeout=3 > http://192.168.1.2:5432/test?loginTimeout=3", "test", "1234"); Consider externalizing the connection details. Try persistence.xml, context.xml or an application server's setup script/screen to create a data source. > return db; > > } catch (SQLException sqle) { > System.err.println("Error on Connection"); > return null; You might want to log more details (again, using a logger) to troubleshoot this scenario. One candidate detail is the SQLState. > } > } -- Lew