You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Seetha Rao <sr...@vertizone.net> on 2006/07/20 20:34:34 UTC

Tomcat fails to refresh connections when mySQL server on linux is shutdown and restarted

Hi,

We are doing a test where mySQL server goes down when user is accessing a
webapplication.
We have setup tomcat with the required parameters for DBCP to handle such a
case.
It works fine in a set up where Tomcat and mySQL are on the same windows
machine.
But in a setup where Tomcat is on Windows machine connecting to mySQL
running on a linux machine, it does not work.

Here are the versions:

Tomcat 5.0
mySQL 4.1
Linux 2.6 Fedora

We have configured Tomcat with the following in our webapp xml file (I'm
pasting a part of it)

<parameter>
<name>validationQuery</name>
<value>select 1</value>
</parameter>

<parameter>
<name>testOnBorrow</name>
<value>true</value>
</parameter>

<parameter>
<name>testWhileIdle</name>
<value>true</value>
</parameter>

<parameter>
<name>timeBetweenEvictionRunsMillis</name>
<value>10000</value>
</parameter>

<parameter>
<name>minEvictableIdleTimeMillis</name>
<value>60000</value>
</parameter>

<parameter>
<name>url</name>
<value>jdbc:mysql://190.162.50.20:3306/xyz_office?autoReconnect=true</value>
</parameter>

Here is the test we are doing;
1. Enter the web application, do something which accesses the database
2. Stop mysql server and restart on linux
3. Try to do something in the web application
4. Tomcat gives the following exception

stdout.log

java.sql.SQLException: No operations allowed after statement closed.
This comes many many times, basically an overflow.

In the browser:

javax.servlet.ServletException: Servlet execution threw an exception

org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:670
)
	org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:637)
	org.apache.jsp.index_jsp._jspService(index_jsp.java:46)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:3
24)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:10
69)

org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProces
sor.java:455)

org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:279)
	org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
	org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

This comes many many times, basically an overflow.

root cause

java.lang.StackOverflowError

org.apache.coyote.tomcat5.CoyoteRequest.getAttribute(CoyoteRequest.java:909)

org.apache.coyote.tomcat5.CoyoteRequestFacade.getAttribute(CoyoteRequestFaca
de.java:214)

Could anyone please give us some hints?

Thanks
Seetha


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Tomcat fails to refresh connections when mySQL server on linux is shutdown and restarted

Posted by Seetha Rao <sr...@vertizone.net>.
Thanks David for the inputs. I will do the same. This might fix my original problem aslo, I will try.

-----Original Message-----
From: David Smith [mailto:dns4@cornell.edu]
Sent: Monday, July 24, 2006 5:00 PM
To: Tomcat Users List
Subject: Re: Tomcat fails to refresh connections when mySQL server on
linux is shutdown and restarted


Best practice is to store the DataSource, not the connections in your 
singelton class.  Then get a connection, perform your queries, and close 
the connection immediately.  The pool will take care of managing the 
connections including creating new ones when existing connections die 
which happens when the database server is restarted.

--David

Seetha Rao wrote:

>Hi Chris, thanks for the comments. I thought since we have singleton class and creating a connection object there, we will be reusing the same connection object for all database operations. May be my thinking is not correct. Do you suggest I just create the DataSource object in the private constructor and then get connection object from the datasource when doing a 
>database query/update opeartions? Yes, I will make sure I close the stmt and connection objects.
>
>-----
>private static DataSource ds = null;
>
>private DBManager() throws Exception
>{
>        Context init = new InitialContext();
>        Context ctx = (Context) init.lookup("java:comp/env");
>        ds = (DataSource) ctx.lookup("jdbc/jalasevaDB");
>        
>}
>
>And then, for example, in a method to do query, 
>
>myCon = ds.getConnection();
>stmt = myCon.createStatement();
>
>And then close the stmt and mycon.
>-------
>
>What would be the effect of calling 'ds.getConnection()' for every database operation?
>
>Thanks again for your guidance,
>Seetha
>
>
>-----Original Message-----
>From: Christopher Schultz [mailto:chris@christopherschultz.net]
>Sent: Sunday, July 23, 2006 8:04 PM
>To: srao@vertizone.net
>Cc: Tomcat Users List; timlucia@yahoo.com
>Subject: Re: Tomcat fails to refresh connections when mySQL server on
>linux is shutdown and restarted
>
>
>Seetha,
>
>  
>
>>To answer Tim's question, we are not explicitly closing connection
>>and statement objects as the context xml has these resource parameters.
>>
>>removeAbandoned="true"
>>removeAbandonedTimeout="60"
>>logAbandoned="true"
>>    
>>
>
>This probably means that you are leaking every single connection. :(
>
>  
>
>>Shouldn't DBCP take care of creating new connection if the connection
>>object is stale?
>>    
>>
>
>You will be creating new Connection objects all the time -- basically
>you'll never re-use a database connection, making the pool completely
>irrelevant; you may as well call DriverManager.getConnection each time
>you need to make a SQL query.
>
>Even if DBCP /does/ clean up after you, you /really/ need to call
>"close" on your statement, resultset, and connection objects in finally
>blocks in your code. If you don't, your code will probably not work
>if/when you switch to another connection pool, another app server,
>another database, etc.
>
>Most databases allocate lots of memory for each database connection on
>the server side, which means that every useless connection you have
>waiting around to be cleaned up by DBCP will be taking up memory on your
>database server that could be used to serve actual requests.
>
>Whether this solves your original problem or not, you definitely need to
>modify your code to close all of those objects.
>
>-chris
>
>
>
>
>---------------------------------------------------------------------
>To start a new topic, e-mail: users@tomcat.apache.org
>To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>For additional commands, e-mail: users-help@tomcat.apache.org
>
>  
>


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Tomcat fails to refresh connections when mySQL server on linux is shutdown and restarted

Posted by David Smith <dn...@cornell.edu>.
Best practice is to store the DataSource, not the connections in your 
singelton class.  Then get a connection, perform your queries, and close 
the connection immediately.  The pool will take care of managing the 
connections including creating new ones when existing connections die 
which happens when the database server is restarted.

--David

Seetha Rao wrote:

>Hi Chris, thanks for the comments. I thought since we have singleton class and creating a connection object there, we will be reusing the same connection object for all database operations. May be my thinking is not correct. Do you suggest I just create the DataSource object in the private constructor and then get connection object from the datasource when doing a 
>database query/update opeartions? Yes, I will make sure I close the stmt and connection objects.
>
>-----
>private static DataSource ds = null;
>
>private DBManager() throws Exception
>{
>        Context init = new InitialContext();
>        Context ctx = (Context) init.lookup("java:comp/env");
>        ds = (DataSource) ctx.lookup("jdbc/jalasevaDB");
>        
>}
>
>And then, for example, in a method to do query, 
>
>myCon = ds.getConnection();
>stmt = myCon.createStatement();
>
>And then close the stmt and mycon.
>-------
>
>What would be the effect of calling 'ds.getConnection()' for every database operation?
>
>Thanks again for your guidance,
>Seetha
>
>
>-----Original Message-----
>From: Christopher Schultz [mailto:chris@christopherschultz.net]
>Sent: Sunday, July 23, 2006 8:04 PM
>To: srao@vertizone.net
>Cc: Tomcat Users List; timlucia@yahoo.com
>Subject: Re: Tomcat fails to refresh connections when mySQL server on
>linux is shutdown and restarted
>
>
>Seetha,
>
>  
>
>>To answer Tim's question, we are not explicitly closing connection
>>and statement objects as the context xml has these resource parameters.
>>
>>removeAbandoned="true"
>>removeAbandonedTimeout="60"
>>logAbandoned="true"
>>    
>>
>
>This probably means that you are leaking every single connection. :(
>
>  
>
>>Shouldn't DBCP take care of creating new connection if the connection
>>object is stale?
>>    
>>
>
>You will be creating new Connection objects all the time -- basically
>you'll never re-use a database connection, making the pool completely
>irrelevant; you may as well call DriverManager.getConnection each time
>you need to make a SQL query.
>
>Even if DBCP /does/ clean up after you, you /really/ need to call
>"close" on your statement, resultset, and connection objects in finally
>blocks in your code. If you don't, your code will probably not work
>if/when you switch to another connection pool, another app server,
>another database, etc.
>
>Most databases allocate lots of memory for each database connection on
>the server side, which means that every useless connection you have
>waiting around to be cleaned up by DBCP will be taking up memory on your
>database server that could be used to serve actual requests.
>
>Whether this solves your original problem or not, you definitely need to
>modify your code to close all of those objects.
>
>-chris
>
>
>
>
>---------------------------------------------------------------------
>To start a new topic, e-mail: users@tomcat.apache.org
>To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>For additional commands, e-mail: users-help@tomcat.apache.org
>
>  
>


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Tomcat fails to refresh connections when mySQL server on linux is shutdown and restarted

Posted by Seetha Rao <sr...@vertizone.net>.
Hi Chris, thanks for the comments. I thought since we have singleton class and creating a connection object there, we will be reusing the same connection object for all database operations. May be my thinking is not correct. Do you suggest I just create the DataSource object in the private constructor and then get connection object from the datasource when doing a 
database query/update opeartions? Yes, I will make sure I close the stmt and connection objects.

-----
private static DataSource ds = null;

private DBManager() throws Exception
{
        Context init = new InitialContext();
        Context ctx = (Context) init.lookup("java:comp/env");
        ds = (DataSource) ctx.lookup("jdbc/jalasevaDB");
        
}

And then, for example, in a method to do query, 

myCon = ds.getConnection();
stmt = myCon.createStatement();

And then close the stmt and mycon.
-------

What would be the effect of calling 'ds.getConnection()' for every database operation?

Thanks again for your guidance,
Seetha


-----Original Message-----
From: Christopher Schultz [mailto:chris@christopherschultz.net]
Sent: Sunday, July 23, 2006 8:04 PM
To: srao@vertizone.net
Cc: Tomcat Users List; timlucia@yahoo.com
Subject: Re: Tomcat fails to refresh connections when mySQL server on
linux is shutdown and restarted


Seetha,

> To answer Tim's question, we are not explicitly closing connection
> and statement objects as the context xml has these resource parameters.
> 
> removeAbandoned="true"
> removeAbandonedTimeout="60"
> logAbandoned="true"

This probably means that you are leaking every single connection. :(

> Shouldn't DBCP take care of creating new connection if the connection
> object is stale?

You will be creating new Connection objects all the time -- basically
you'll never re-use a database connection, making the pool completely
irrelevant; you may as well call DriverManager.getConnection each time
you need to make a SQL query.

Even if DBCP /does/ clean up after you, you /really/ need to call
"close" on your statement, resultset, and connection objects in finally
blocks in your code. If you don't, your code will probably not work
if/when you switch to another connection pool, another app server,
another database, etc.

Most databases allocate lots of memory for each database connection on
the server side, which means that every useless connection you have
waiting around to be cleaned up by DBCP will be taking up memory on your
database server that could be used to serve actual requests.

Whether this solves your original problem or not, you definitely need to
modify your code to close all of those objects.

-chris




---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Tomcat fails to refresh connections when mySQL server on linux is shutdown and restarted

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Seetha,

> To answer Tim's question, we are not explicitly closing connection
> and statement objects as the context xml has these resource parameters.
> 
> removeAbandoned="true"
> removeAbandonedTimeout="60"
> logAbandoned="true"

This probably means that you are leaking every single connection. :(

> Shouldn't DBCP take care of creating new connection if the connection
> object is stale?

You will be creating new Connection objects all the time -- basically
you'll never re-use a database connection, making the pool completely
irrelevant; you may as well call DriverManager.getConnection each time
you need to make a SQL query.

Even if DBCP /does/ clean up after you, you /really/ need to call
"close" on your statement, resultset, and connection objects in finally
blocks in your code. If you don't, your code will probably not work
if/when you switch to another connection pool, another app server,
another database, etc.

Most databases allocate lots of memory for each database connection on
the server side, which means that every useless connection you have
waiting around to be cleaned up by DBCP will be taking up memory on your
database server that could be used to serve actual requests.

Whether this solves your original problem or not, you definitely need to
modify your code to close all of those objects.

-chris



RE: Tomcat fails to refresh connections when mySQL server on linux is shutdown and restarted

Posted by Seetha Rao <sr...@vertizone.net>.
Thanks Tim and Chris for the response. Sorry, it took a while for me to respond. We are not storing connections in the session. We have a singleton class called DBManager and in its private constructor we have JNDI lookup for the datasource and  create a connection object there.

  Context init = new InitialContext();
  Context ctx = (Context) init.lookup("java:comp/env");
  DataSource ds = (DataSource) ctx.lookup("jdbc/jalasevaDB");
  myCon = ds.getConnection();
  stmt = myCon.createStatement();

And other classes which require database access get this DBManager instance to do query,update etc.
To answer Tim's question, we are not explicitly closing connection and statement objects as the context xml has these resource parameters.

removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"

Shouldn't DBCP take care of creating new connection if the connection object is stale?

I have been pulled into do other related work, haven't got the time to explore this problem further. 
I will update once I get to work on this. Thanks very much for the response. Meanwhile I would 
welcome your suggestions.

Thanks
Seetha

-----Original Message-----
From: Christopher Schultz [mailto:chris@christopherschultz.net]
Sent: Friday, July 21, 2006 6:09 PM
To: Tomcat Users List
Cc: srao@vertizone.net
Subject: Re: Tomcat fails to refresh connections when mySQL server on
linux is shutdown and restarted


Seetha,

> Are you getting the connection anew after restarting the server?  It looks
> like you have this case:
> 
> T0	Connection x = Datasource.getConnection()
> T1	Do some stuff
> T2	Shutdown / restart MySQL
> T3	x.prepareStatement() or other stuff
> ... FAIL
> 
> The connection pool will only renew the connection on the
> Datasource.getConnection() (the validation query, SELECT 1, will only run on
> getConnection() -- in your case with test on borrow)

You could get this kind of thing happening if you were storing JDBC
Connections in the user's session. If the Connection is shut down and
serialized along with the session during the restart, then you will have
an invalid Connection in the session when Tomcat comes back up.

Do you do anything like this in your application?

-chris




---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Tomcat fails to refresh connections when mySQL server on linux is shutdown and restarted

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Seetha,

> Are you getting the connection anew after restarting the server?  It looks
> like you have this case:
> 
> T0	Connection x = Datasource.getConnection()
> T1	Do some stuff
> T2	Shutdown / restart MySQL
> T3	x.prepareStatement() or other stuff
> ... FAIL
> 
> The connection pool will only renew the connection on the
> Datasource.getConnection() (the validation query, SELECT 1, will only run on
> getConnection() -- in your case with test on borrow)

You could get this kind of thing happening if you were storing JDBC
Connections in the user's session. If the Connection is shut down and
serialized along with the session during the restart, then you will have
an invalid Connection in the session when Tomcat comes back up.

Do you do anything like this in your application?

-chris



RE: Tomcat fails to refresh connections when mySQL server on linux is shutdown and restarted

Posted by Tim Lucia <ti...@yahoo.com>.
Are you getting the connection anew after restarting the server?  It looks
like you have this case:

T0	Connection x = Datasource.getConnection()
T1	Do some stuff
T2	Shutdown / restart MySQL
T3	x.prepareStatement() or other stuff
... FAIL

The connection pool will only renew the connection on the
Datasource.getConnection() (the validation query, SELECT 1, will only run on
getConnection() -- in your case with test on borrow)


Tim


> -----Original Message-----
> From: Seetha Rao [mailto:srao@vertizone.net]
> Sent: Thursday, July 20, 2006 2:35 PM
> To: Tomcat Users List
> Subject: Tomcat fails to refresh connections when mySQL server on linux is
> shutdown and restarted
> 
> Hi,
> 
> We are doing a test where mySQL server goes down when user is accessing a
> webapplication.
> We have setup tomcat with the required parameters for DBCP to handle such
> a
> case.
> It works fine in a set up where Tomcat and mySQL are on the same windows
> machine.
> But in a setup where Tomcat is on Windows machine connecting to mySQL
> running on a linux machine, it does not work.
> 
> Here are the versions:
> 
> Tomcat 5.0
> mySQL 4.1
> Linux 2.6 Fedora
> 
> We have configured Tomcat with the following in our webapp xml file (I'm
> pasting a part of it)
> 
> <parameter>
> <name>validationQuery</name>
> <value>select 1</value>
> </parameter>
> 
> <parameter>
> <name>testOnBorrow</name>
> <value>true</value>
> </parameter>
> 
> <parameter>
> <name>testWhileIdle</name>
> <value>true</value>
> </parameter>
> 
> <parameter>
> <name>timeBetweenEvictionRunsMillis</name>
> <value>10000</value>
> </parameter>
> 
> <parameter>
> <name>minEvictableIdleTimeMillis</name>
> <value>60000</value>
> </parameter>
> 
> <parameter>
> <name>url</name>
> <value>jdbc:mysql://190.162.50.20:3306/xyz_office?autoReconnect=true</valu
> e>
> </parameter>
> 
> Here is the test we are doing;
> 1. Enter the web application, do something which accesses the database
> 2. Stop mysql server and restart on linux
> 3. Try to do something in the web application
> 4. Tomcat gives the following exception
> 
> stdout.log
> 
> java.sql.SQLException: No operations allowed after statement closed.
> This comes many many times, basically an overflow.
> 
> In the browser:
> 
> javax.servlet.ServletException: Servlet execution threw an exception
> 
> org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:6
> 70
> )
> 	org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.ja
> va:637)
> 	org.apache.jsp.index_jsp._jspService(index_jsp.java:46)
> 	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
> 	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
> 
> org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java
> :3
> 24)
> 	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:
> 292)
> 	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
> 	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
> 
> org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:
> 10
> 69)
> 
> org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProc
> es
> sor.java:455)
> 
> org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:27
> 9)
> 	org.apache.struts.action.ActionServlet.process(ActionServlet.java:14
> 82)
> 	org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525
> )
> 	javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
> 	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
> 
> This comes many many times, basically an overflow.
> 
> root cause
> 
> java.lang.StackOverflowError
> 
> org.apache.coyote.tomcat5.CoyoteRequest.getAttribute(CoyoteRequest.java:90
> 9)
> 
> org.apache.coyote.tomcat5.CoyoteRequestFacade.getAttribute(CoyoteRequestFa
> ca
> de.java:214)
> 
> Could anyone please give us some hints?
> 
> Thanks
> Seetha
> 
> 
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org