MYSQL 8 Hours Time out Problem - mysql

I am using MYSQL 5.0 and Tomcat 5.5.
After 8 hours , MYSQL closes by default closes all idle connections and so I am getting an SQL Exception .Any solution to this problem

Which connection pool are you using? Most allow a heartbeat command to be executed periodically to keep connections from idling out.
EDIT: Looking at the DBCP configuration page, I think you should set an appropriate (cheap) validationQuery. You could then either tell DBCP to test this when potentially evicting idle connections (testWhileIdle) or tell it to test just before returning the connection (testOnBorrow). The first would (hopefully!) keep your idle connections alive; the second wouldn't, but wouldn't return you broken connections (i.e. if would create a new one if necessary).

Do you mean that you keep the connections alive while they are idle? I am no expert on mySql, but in the databases I have been working with it's considered good practice to keep the connection open only while it's needed, and then close it.

I don't know how to increase the timeout but I guess there should be a setting.. Why don't you create the connection when you need it, and close it after using it other than depending on a created connection?

With MySQL, by default an open connection (often waiting in a connection pool) will be closed after 8 hours of inactivity. You can change this time from 8 hours by configuring the wait_timeout variable either in your my.cnf or on our mysql command line
See:
http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_wait_timeout
http://dev.mysql.com/doc/refman/5.0/en/gone-away.html

This question has been asked a few years ago, but I feel that it's good to have the correct answer. i had the same issue, to solve I had to play with the configuration a little bit. here is what works for me. Go on https://people.apache.org/~fhanik/jdbc-pool/jdbc-pool.html for more info. The code below took care of everything. There is way to optimize the values but I will take care of these later.
<Resource name="jdbc/database"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="username" password="password"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql:///"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
testWhileIdle="true"
testOnBorrow="true"
testOnReturn="false"
validationQuery="SELECT 1"
validationInterval="30000"`enter code here`
timeBetweenEvictionRunsMillis="30000"
minIdle="10" `enter code here`
initialSize="10"
removeAbandonedTimeout="60"
removeAbandoned="true"
logAbandoned="true"
minEvictableIdleTimeMillis="30000"
jmxEnabled="true"
/>

Related

How to configure Tomcat to Not use Database Connection Pooling

How can you configure Tomcat to Not use Database Connection Pooling ?
I want to do this because I have an application that makes very few calls to the mysql database, typically less than 10 in a 24 hour period and some calls fail on retrieving the connection. It appears that Mysql is sometimes giving up on the connection because it has been idle for so long without telling the database pooler.
Worth noting that I have another application that makes thousands of requests to the exact same database and this never fails, so I am convinced the issue is related to the rarity of calls to the database. Mysql is probably part of the problem but updating to Postgres would be a larger task than I want to commit to right now.
So I tried configuring context.xml to testWhileIdle but it is stil failing
<Context path="/store" privileged="true">
<Resource name="jdbc/myapp" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="100" maxWait="10000" testWhileIdle="true"
username="usrnm" password="pwd" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/myapp"/>
</Context>
So now I want to disable pooling so its get a new connection every time but I cant work out how to do it, Ive looked at
https://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html#Plain_Ol%27_Java
but everything seems to be centered out database pooling.
Attempted Solution
So I tried out suggestion in answer and will see what happens over next few days
<Context path="/store" privileged="true">
<Resource name="jdbc/myapp" auth="Container" type="javax.sql.DataSource"
maxActive="10" maxIdle="10"
testWhileIdle="true" validationQuery="select 1" validationQueryTimeout="5"
username="usrnm" password="pwd"
driverClassName="com.mysql.jdbc.Driver" testOnBorrow="true"
url="jdbc:mysql://localhost:3306/myapp"/>
</Context>
Nothing should be preventing you from making your own connections and using them. But, I think you are missing configuration to make the connection validation work properly. When a connection is tested and found to be bad it should be replaced by a new one prior to the pool giving you the connection. I think you are missing validationQuery which would be utilized when testing the connection. Check out the docs at https://tomcat.apache.org/tomcat-8.0-doc/jdbc-pool.html and look at supplying the necessary values for the validation to work. Try testOnBorrow and validationQuery so that each time you go for a connection it will validation it prior to returning. Also, you appear to be setting up way too many connections based on your frequency of usage. Tuning that should help avoid keeping unnecessary connections open.

Connection to database lost after several hours in Grails war

I'm having a rare issue with a war I deployed, and it's connection to the database server (MySQL), here's the thing:
I made the tomcat config like this:
<Resource name="jdbc/gimnasioBackend" auth="Container" type="javax.sql.DataSour$
maxTotal="100" maxIdle="30" maxWaitMillis="10000"
username=["username"] password=["password"] driverClassName="com$
url="jdbc:mysql://localhost:3306/[databaseName]"/>
And is connecting just fine, but the thing is that every day I come to check the server status, and also test the pages in it, I can't login. If I restart the server (which is not recommended, judging what I've red), I can login (using spring security) again, but not for long... The situation is complicated, mainly because it's already in production, and is holding a backend for an app..
Thanks in advance!
Note: There's more than one web app in this tomcat, all of them working fine.
I had a similar issue with grails DataSource and with tomcat resource configuration connecting to an Oracle database and I resolved adding
validationQuery="SELECT 1 FROM DUAL"
validationQueryTimeout=3
validationInterval=15000
If the problem is due to connection against database lost I suppose you can try adding those properties. Validation query for mysql should be select 1 instead of select 1 from dual

Java web application running on Tomcat unable to execute SQL queries with DB Connection pooling

I've come across similar questions many times here on Stack Overflow, however, I haven't been able to get it right.
I'm trying to use Database connection pooling in Tomcat 6 + MySQL ( on AWS RDS )
These are the parameters I have configured.
( I'm closing the connection from my Java code also )
<Resource name="jdbc/awsDB" auth="Container" type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
initialSize="10"
testWhileIdle="true"
maxActive="30"
maxAge="3600"
maxIdle="5"
maxWait="3000"
removeAbandoned="true"
logAbandoned="false"
validationQuery="SELECT 1"
removeAbandonedTimeout="60"
timeBetweenEvictionRunsMillis="15000"
username="sbose78" password="XXXXX"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://xxxxrds.amazonaws.com:3306/health?autoReconnect=true" />
The application works great for the first few database queries and after that the database connection hangs indefinitely.
From the connection logs I've noticed that the connection pooling works good , however, Even when there are 10+ connections in SLEEP state, a new database query takes indefinite time to execute.
I end up re-starting the server in most cases.
What configuration mistake am I doing?
Thanks a lot in advance!
Check whether your query is using all the indexed columns in the where clause.If you are sure about the connection pool, then most probaably the problem will be with query itself.

Grails / Tomcat: Database connections get stuck in sleep state

I have a Grails application running under Tomcat 7 and am having problems managing MySQL connections.
The problem is that each new request to the application (i.e., a page load) is creating a new MySQL connection, and these connections are not closing. Instead, they stay in a SLEEP state until the MySQL server finally refuses to accept more connections. Therefore, simply by reloading individual pages on the site at a fast rate, one can create numerous database connections.
So it seems that my connection pool isn't closing the connections with MySQL fast enough. There are a number of configuration settings for the connection pool, but I'm not sure what needs to be tuned to avoid this problem.
Here is the configuration from my context.xml file:
<Resource name="jdbc/Production" auth="Container" type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
minEvictableIdleTimeMillis="1800000"
timeBetweenEvictionRunsMillis="1800000"
numTestsPerEvictionRun="3"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"
testOnBorrow="true"
testWhileIdle="true"
testOnReturn="true"
validationQuery="SELECT 1"
username=""
password=""
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/Production"
/>
Thanks very much for any suggestions.
You did not define a connection pool.
Add following code to the context.xml(it seems to be a JNDI data source):
pooled = "true"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
If you haven't put JDBC Pool in to your dependency configuration, add following to the plugins closure in BuildConfig.groovy:
compile ":jdbc-pool:1.0.9.3"
You may use other connection pools, but JDBC Pool will be my recommendation.

Tomcat connectionpool mysql configuration to avoid timeout of idle connections

I'm using tomcat 7 and the tomcat jdbc connection pool to dish out mysql connections.
During night times we don't have any activity so all connections become idle for longer than 8 hours and are dropped by mysql. (mysql's wait_timeout default).
We use the following pool configuration:
<Resource name="jdbc/dbName"
auth="Container"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
type="javax.sql.DataSource"
maxActive="50"
maxIdle="30"
maxWait="5000"
driverClassName="com.mysql.jdbc.Driver"
validationQuery="SELECT 1"
testOnBorrow="true"
testWhileIdle="true"
timeBetweenEvictionRunsMillis="10000"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"
username="xxx"
password="xxx"
url="jdbc:mysql://host:3306/xxx"/>
I was expecting the EvictionPolicy to remove idle connections way before they ever get closed by MySql. Somehow after one day we get the following exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.Connection was implicitly closed by the driver.
I guess this problem should be something the jdbc connection pool can fix, but there are many configuration properties and I haven't used this pool before. Anybody got a good set of properties to configure the pool to not dish out closed connections?
Kind regards,
Albert
Solved it. Turned out it wasn't a pooling problem after all. We were using squeryl and lift together which isn't a happy combi (just yet). Connections got closed before returned to the pool.
Ditching lift's DB connection management in favor of squeryl's solved it.