Return-Path: X-Original-To: apmail-tomcat-users-archive@www.apache.org Delivered-To: apmail-tomcat-users-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 408C1F0D1 for ; Fri, 5 Apr 2013 16:45:28 +0000 (UTC) Received: (qmail 62617 invoked by uid 500); 5 Apr 2013 16:45:21 -0000 Delivered-To: apmail-tomcat-users-archive@tomcat.apache.org Received: (qmail 62535 invoked by uid 500); 5 Apr 2013 16:45:21 -0000 Mailing-List: contact users-help@tomcat.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Tomcat Users List" Delivered-To: mailing list users@tomcat.apache.org Received: (qmail 62482 invoked by uid 99); 5 Apr 2013 16:45:21 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 05 Apr 2013 16:45:21 +0000 X-ASF-Spam-Status: No, hits=-0.7 required=5.0 tests=RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of knst.kolinko@gmail.com designates 209.85.214.182 as permitted sender) Received: from [209.85.214.182] (HELO mail-ob0-f182.google.com) (209.85.214.182) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 05 Apr 2013 16:45:16 +0000 Received: by mail-ob0-f182.google.com with SMTP id ef5so3899684obb.13 for ; Fri, 05 Apr 2013 09:44:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type:content-transfer-encoding; bh=6j/iZoelWq+7X/0zVfk23Z3F6Trh/n5Nwqa4QK69n+w=; b=H2ll+Z2Le/sa9WAPp3phhJbZcwTWJxBV0hyS4C7Qav8k8H39UrfIdrHUpyjkDwnkN6 bfmEfLPmz/2OuGkUYViHYEy+iv/+3kiScWPPj60ADhtqJaUvs0ITUX7envwb3AMM7rWV +rkdwWXucN/eXRuOCcnjhTFx1Bwicq8z7gF1thfiUR2bs0XbW6xz2a2Ok99wn/KT0feF Xjhdpr+vbq3nbxrrN06V0lPPl0amuCyscHCkiiwi/RwLwJOAYHzsbVDOs266OWxWjC/h JwnmiBglYakLOjCxwuPK9X0d9QZBiy9gKjUx0q7VjtTXWm6Tgje3NnNgDqTRAafqHlDV mTmA== MIME-Version: 1.0 X-Received: by 10.60.144.135 with SMTP id sm7mr8882610oeb.16.1365180296106; Fri, 05 Apr 2013 09:44:56 -0700 (PDT) Received: by 10.76.153.38 with HTTP; Fri, 5 Apr 2013 09:44:55 -0700 (PDT) In-Reply-To: <515EEFC8.7090108@medifirst.fr> References: <515E9B5A.3060909@medifirst.fr> <515EEFC8.7090108@medifirst.fr> Date: Fri, 5 Apr 2013 20:44:55 +0400 Message-ID: Subject: Re: Tomcat7 - ajax and connection pool : "connection closed" despite the use of "validationQuery" and "testOnBorrow" From: Konstantin Kolinko To: Tomcat Users List Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Checked: Checked by ClamAV on apache.org 2013/4/5 Jean-Claude Haw-King-Chon : > Le 05/04/2013 13:09, Konstantin Kolinko a =E9crit : >> >> 2013/4/5 Jean-Claude Haw-King-Chon : >>> >>> Hi, >>> >>> I use the connection pool of tomcat7 and set a connection in a >>> ThreadLocal. >>> The context is a web application : the threadLocal is configured when t= he >>> http request is initialized. The jdbc connection is closed and removed >>> from >>> the threadLocal when the resquest is destroyed. >>> >> 1. What exactly do you mean by "initialized" and "destroyed"? >> >>> If I'm not mistaken, each http request is treated by one thread by >>> tomcat. >>> So, I have one jdbc connection for each request ( =3D thread). >> >> 2. No. It depends on how you use it and at what points in time you look >> at it. >> >> If you look at it in a call chain, e.g. using try{ } finally block in >> a Filter, then yes, all calls inside of "try" belong to the same >> thread. >> >> If you return processing to Tomcat and Tomcat makes a different call >> into your code (e.g. using Async or Comet APIs), then that call can be >> using a different thread from a thread pool. >> >>> (...) >>> >>> My problem appears when I use Ajax. For 2 successive ajax request, I ha= ve >>> the error "Connection closed" : it's a random problem (the stacktrace i= s >>> not >>> the same for the 2 attemps and it's not depending of the thread executi= on >>> order). >>> >>> I know this issue : >>> >>> http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.= html#Random_Connection_Closed_Exceptions >>> >>> "These can occur when one request gets a db connection from the >>> connection >>> pool and closes it twice. When using a connection pool, closing the >>> connection just returns it to the pool for reuse by another request, it >>> doesn't close the connection. And Tomcat uses multiple threads to handl= e >>> concurrent requests" >>> >>> But, I'm sure, I always close the connection only once. >>> I use too the "validationQuery" and "testOnBorrow" parameters. >>> >>> Is it possible that the connection pool returns the same connection for= 2 >>> different threads? >>> >> You can check its hash code. >> >>> So I don't understand why my connection is closed during the treatment. >>> >> Best regards, >> Konstantin Kolinko >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org >> For additional commands, e-mail: users-help@tomcat.apache.org >> >> > > 1. What exactly do you mean by "initialized" and "destroyed"? > > Sorry, to be more precise, 2 approches has been tested : > > 1) The connection is initialized in the servlet by a method called by > "doGet" or "doPost". > But the connection is closed (and removed from the threadLocal) by a > ServletRequestListener in the override of the "requestDestroyed" method. > > 2) The connection is initialized in the servlet by a method called by > "doGet" or "doPost". The process is encapsuled in a try/catch/finally blo= ck. > The connection is closed (and removed from the threadLocal) in the finall= y > block. > > These 2 approches didn't work (random closed connection). > You should go with 2). > > > 2. No. It depends on how you use it and at what points in time you look = at > it. > > So for the 2 approches, I'm in a call chain. > > > > Is it possible that the connection pool returns the same connection for 2 > different threads? > You can check its hash code. > > Thank you for this tip. The thread name and the connection hashCode are n= ow > logged : I'm trying to understand the mechanism. > The first results show that the problem appears with connections preceden= tly > closed... > > My configuration : > > name=3D"jdbc/607" > auth=3D"Container" > type=3D"javax.sql.DataSource" > username=3D"xxxxx" > password=3D"xxxxx" > driverClassName=3D"com.mysql.jdbc.Driver" > url=3D"jdbc:mysql://localhost:3306/XXXXX" > removeAbandoned=3D"true" > removeAbandonedTimeout=3D"300" > testOnBorrow=3D"true" > validationQuery=3D"SELECT 1" > logAbandoned=3D"true" > /> > Tomcat 7.0.29 > Linux Fedora Core 12 > Do you have queries that run or are used for more than 5 minutes in a single request? If so, then your removeAbandonedTimeout is too short and "removeAbandoned" will close them. http://commons.apache.org/proper/commons-dbcp/configuration.html BTW, your pool size is 8. Best regards, Konstantin Kolinko --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org For additional commands, e-mail: users-help@tomcat.apache.org