Posting a request throws Connection pool error in java - java

I'm getting connection pool issue when I try to post to the Database
here what I'm using for context xml file
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
maxActive="10"
initialSize="0"
maxAge="1800"
testWhileIdle="true"
testOnBorrow="true"
testOnReturn="false"
validationQuery="SELECT 1"
validationInterval="30000"
timeBetweenEvictionRunsMillis="30000"
removeAbandonedTimeout="60"
removeAbandoned="true"
logAbandoned="true"
minEvictableIdleTimeMillis="18000"
jmxEnabled="true"
fairQueue="true"
jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;
org.apache.tomcat.jdbc.pool.interceptor.ResetAbandonedTimer;
org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
the Error:
Caused by: org.apache.tomcat.jdbc.pool.PoolExhaustedException: [ajp-nio-0.0.0.0-8009-exec-3] Timeout: Pool empty. Unable to fetch a connection in 30 seconds, none available[size:10; busy:10; idle:0; lastwait:30000].
at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:710) ~[tomcat-jdbc.jar:?]
at org.apache.tomcat.jdbc.pool.ConnectionPool.getConnection(ConnectionPool.java:198) ~[tomcat-jdbc.jar:?]
at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:132) ~[tomcat-jdbc.jar:?]
at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:157) ~[spring-jdbc-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:115) ~[spring-jdbc-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:78) ~[spring-jdbc-5.1.6.RELEASE.jar:5.1.6.RELEASE]
I have this working working before, then suddenly it stops working, Is there something I'm missing to add or update in my config?

There can be different reasons why your connectionpool is exausted. If you are using a framework that connects to your mysql database check the recommended pool configuration.
Also make sure to close your connections properly.
The try-with-resources Statement: try with resources
a reasonable config could look like this:
<Resource type="javax.sql.DataSource"
...
initialSize="10"
maxActive="100"
maxIdle="50"
minIdle="10"
...
/>

Related

java.sql.SQLException: Connection has already been closed

We are getting java.sql.SQLException: Connection has already been closed. exception intermittently while performing a transaction. We are using tomcat 7.X and below is the configuration.
<Context docBase="ROOT" reloadable="true" antiJARLocking="true">
<Resource
name="jdbc/DS"
auth="Container"
type="javax.sql.DataSource"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://XXXXXXX"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
username="XXXXXX"
password="XXXXXX"
maxActive="20"
maxIdle="3"
minIdle="3"
maxWait="10000"
removeAbandoned="true"/>
</Context>
Probably we are missing some configuration or property here that is causing the issue.
Please suggest a way fix this issue or help to find out the root cause.
Following configuration worked for me
<Context context="ROOT" debug="0" reloadable="false" useHttpOnly="true" cacheMaxSize="40960" cacheTTL="60000" cachingAllowed="true" antiJARLocking="true">
<Resource name="XYZ" auth="Container"
description="Exchange DB Connection"
dataSourceClassName="org.postgresql.ds.PGSimpleDataSource"
dataSource.serverName="XXXXX"
dataSource.databaseName="XXXX"
dataSource.portNumber="XXXX"
dataSource.user="xyz"
dataSource.password="xyz"
maximumPoolSize="20"
minimumIdle="5"
connectionTimeout="300000"
factory="com.zaxxer.hikari.HikariJNDIFactory"
registerMbeans="true"
type="javax.sql.DataSource" />
The key value here is connectionTimeout.
The factory which you are currently using has a default timeout, after that it forces session to close.
The connection timeout value above worked for me , for your application scenarios you'll have to experiment a bit to get the right value.
add below value:
removeAbandonedTimeout="600"

JDBC connection pooling with Struts 2

I am using connection pooling in Struts2 application but after some request, I am not getting any result even though I am closing the connection in our application. Please give me proper solution.
Context.xml
<Resource name="jdbc/abbas"
auth="Container"
type="javax.sql.DataSource"
username="myuser"
password="pass#123"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://192.168.14.65/abbas"
maxActive="15"
maxIdle="3"/>

Tomcat need to restart after database restart

I have a webapp deployed in Tomcat 7. There I cave configured my database pool as given below.
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
<Resource type="javax.sql.DataSource"
name="jdbc/TEST"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/testdb?zeroDateTimeBehavior=convertToNull"
username="test"
password="test"
initialSize="10"
maxActive="100"
maxIdle="50"
minIdle="10"/>
This configuration works fine.But I want to configure my database pool to reconnect data database server automatically when a database server down for sometime and started again.
Try to add
?autoReconnect=true
in your url.
I found an answer for this problem. Below configuration done the job.
<Resource type="javax.sql.DataSource"
name="jdbc/TEST"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/testdb?zeroDateTimeBehavior=convertToNull"
validationQuery="select 1"
validationInterval="30000"
testWhileIdle="true"
testOnBorrow="true"
testOnReturn="false"
username="test"
password="test"
initialSize="10"
maxActive="100"
maxIdle="50"
minIdle="10"/>

How to get PoolingDataSource from Tomcat's JNDI

If I deploy a web app to Tomcat, and have code like this:
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("jdbc/myDB");
How can I specify for this DataSource to be a PoolingDataSource? How do I configure the pool (GenericObjectPool) to inject the PoolingDataSource with?
Or, is this the default behavior of Tomcat's JNDI implementation? Thanks in advance!
Just configure the connection pool settings (maxActive, maxWait, maxIdle, ...).
Tomcat comes with the apache commons-dbcp library. It is repackaged as $CATALINA_HOME/lib/tomcat-dbcp.jar.
Take a look at the tomcat doc for details: http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html#Database_Connection_Pool_%28DBCP%29_Configurations
<Context>
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javatest"/>
</Context>
The JDBC Connection Pool org.apache.tomcat.jdbc.pool
is a replacement or an alternative to the commons-dbcp
connection pool.
Add to the server.xml file in the GlobalNamingResources section something like the next:
<Resource name="jdbc/TestDB"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
testWhileIdle="true"
testOnBorrow="true"
testOnReturn="false"
validationQuery="SELECT 1"
validationInterval="30000"
timeBetweenEvictionRunsMillis="30000"
maxActive="100"
minIdle="10"
maxWait="10000"
initialSize="10"
removeAbandonedTimeout="60"
removeAbandoned="true"
logAbandoned="true"
minEvictableIdleTimeMillis="30000"
jmxEnabled="true"
jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
username="root"
password="password"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mysql"/>
Add the JDBC driver JAR into the Tomcat lib directory.
Add in your context.xml:
<ResourceLink global="jdbc/TestDB" name="jdbc/TestDB"
type="javax.sql.DataSource"/>
See more in The Tomcat JDBC Connection Pool.

Mysql not reconnecting with JNDI Tomcat 6

I am using JNDI with Tomcat6 to manage Mysql connections, my Catalina/domain.com/ROOT.xml has:
<Resource name="jdbc/db" auth="Container" type="javax.sql.DataSource"
username="db1" password="somepass" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/db?autoReconnect=true" maxActive="15" maxIdle="3"
maxWait="5000" removeAbandoned="true" removeAbandonedTimeout="20" />
I though autoReconnect will do the job reconnecting to database but it does not, after about 8 hours of inactivity my app spits out lost connection to database errors. Any ideas?
Thanks, Fedor
Dont use autoReconnect. There are problems with it and it's been deprecated. For example, you could have a disconnect/reconnect event happen while a thread is using the connection. I would instead have your connection pool test connections with testOnBorrow before passing them to the app. Here is an example:
<Resource name="jdbc/db"
auth="Container"
type="javax.sql.DataSource"
username="db1"
password="somepass"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/db"
maxActive="15"
maxIdle="3"
maxWait="5000"
removeAbandoned="true"
removeAbandonedTimeout="20"
logAbandoned="true"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
validationQuery="select 1"
minEvictableIdleTimeMillis="3600000"
timeBetweenEvictionRunsMillis="1800000"
numTestsPerEvictionRun="10"
testWhileIdle="true"
testOnBorrow="true"
testOnReturn="false"
/>

Categories

Resources