Set tomcat initial connection pool on startup - java

Im looking for information about how to enable connection pooling on tomcat startup. My setting right makes tomcat to initilize and add X number of connections to the pool upon the first request, but I would like tomcat to do this upon starting tomcat.
My setting in Server.xml is as follows:
<Context docBase=".../apache-tomcat-5.5.27/webapps/app" path="/app" reloadable="true"
source="org.eclipse.jst.j2ee.server:app">
<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" maxActive="100"
maxIdle="30" initialSize="10"
maxWait="10000" name="jdbc/app" password="pass" type="javax.sql.DataSource"
url="jdbc:mysql://ip:3306/dbschema" username="username"/>
As I mentioned earlier, setting initialSize to 10 creates 10 connections when the first connection is created, but I would like this functionality when starting tomcat.

Solved this and I will answer my own question in case someone else needs it.
Solved this by creating a servlet, overriding the init() method and had that servlet load on startup by modifying web.xml in tomcat. This works as long as initialSize is already set in the servlet.xml.
So, what actually happends is that tomcat loads this servlet on startup, and all this servlet does is to create a database connection. When this connection is created, tomcat will start the number of connections specified in initialSize

Related

Server Stop responding because of [Pool-Cleaner]:Tomcat Connection Pool but has failed to stop it. This is very likely to create a memory leak

Problem:
org.apache.catalina.loader.WebappClassLoader - The web application [/…] appears to have started a thread named [[Pool-Cleaner]:Tomcat Connection Pool[...] but has failed to stop it. This is very likely to create a memory leak.
I have few application deployed in a tomcat server(tomcat-7.0.23) with jdk1.6.0_45 and the server i register with apache webserver. After staring the server it is working fine until i get the above error for all the application separately all of a sudden and after that my tomcat giving 404 for all request but the tomcat process is still up and in browser it shows "FILE NOT FOUND".
we use Tomcat connection pool with following configuration:
<Resource
name="Project1"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
url="DatabaseName"
username="_username"
password="_Password"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
initialSize="40"
maxActive="300"
maxIdle="250"
minIdle="90"
maxWait="10000"
minEvictableIdleTimeMillis="300000"
timeBetweenEvictionRunsMillis="120000"
validationInterval="30000"
validationQuery="SELECT 1"
testOnBorrow="true"
logAbandoned="true"
removeAbandoned="true"
removeAbandonedTimeout="60"
abandonWhenPercentageFull="50"
jdbcInterceptors="ResetAbandonedTimer"
/>
And the configuration is in context.xml file.
That memory leak warning is correct. It refers to a known issue in Tomcat 7.0.x that was fixed a few weeks ago (r1744702) and will be included in 7.0.70 onwards.

Too few connections from Tomcat Application

I have a mysql database configured with a max_connections value of 150. I also have a Java 6 web application running in Tomcat 5.5 configured with the following setup:
<Resource name="jdbc/myDB"
type="javax.sql.DataSource"
driver="com.mysql.jdbc.Driver"
username="username"
password="password"
maxActive="100"
maxIdle="100"
maxWait="-1"
removeAbandoned="true"
removeAbandonedTimeout="300"
logAbandoned="true"
url="jdbc:mysql://localhost:3306/myDB?autoreconnect=true"
validationQuery="SELECT 1" />
This application is not using any 3rd party framework just basic java servlets. I have a bug in some code in the java app that is not properly releasing opened mysql connections from the pool. I am working on identifying and fixing these. But in the meantime I need to figure out why at most there is only 25 connections being allowed to mysql. After these 25 connections are used up, the application becomes unresponsive.
Can someone please help me figure out why both mysql and tomcat are configured for 100+ connections but it is only allowing 25 at a time?
Tomcat JDBC Connection Pool
What connection pool do you use?
Do you use the Tomcat JDBC Connection Pool, rather than the Apache Commons pool? It has properties to detect connection leaks or abandon connection that are open for a long time than the configured timeout.
MySQL's max_connections was set to 150 but the max_user_connections was set to 25 which was the limiting factor here. I removed this setting from my.cnf to restore it to the default value of unlimited.

Connection pool in tomcat 7

Here is my current config
<Resource
name="jdbc/data"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/TABLE_NAME"
username="USER_NAME"
password="PASSWORD"
initialSize="10"
maxActive="50"
suspectTimeout="120"
minIdle="10"
maxIdle="20"
maxWait="1000"
testOnBorrow="true"
timeBetweenEvictionRunsMillis="30000"
minEvictableIdleTimeMillis="60000"
validationQuery="SELECT 1 FROM DUAL"
validationInterval="40"
removeAbandoned="true"
removeAbandonedTimeout="100"
/>
This is in global context so multiple apps can use it.
I am little confused about parameters.need some details.
What I understand is
initalSize a number of connection created when a pool started.
maxActive maximum 50 connections can active at a time.
minIdle 10 connections remain Idle when connection is not used else are closed after maxwait
maxIdle 20 connections can be store as idle.
But When I start tomcat server I can see a 30 IDLE connections which remains forever.Why this happens? Am I missing something ? According to my understanding about connection pool there should only 10 connections should created and can stay in IDLE mode. Is there any specific changes that I have to do with mysql my.cnf
When you say...
This is in global context so multiple apps can use it.
What specifically do you mean? Is it in $CATALINA_BASE/conf/server.xml in the GlobalNamingResources block or in $CATALINA_BASE/conf/context.xml?
Defining a Resource tag in the GlobalNamingResources block of $CATALINA_BASE/conf/server.xml will cause only one resource to be created across the entire server. This can then be shared to applications deployed on your system by adding a ResourceLink tag to the Context configuration.
Defining a Resource in $CATALINA_BASE/conf/context.xml will define the resource once for each application deployed to your Tomcat instance. Thus if you have three applications deployed, you'll end up with three separate resources. This is a guess, but probably why you are seeing 30 connections to your database server.

Tomcat Connection Pool configuration: DataSource type and "Too many connection" error

I'm using the tomcat connection pool via JNDI resources.
In the context.xml:
<Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource"
username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
maxActive="1000" maxIdle="100" maxWait="10000"
url="jdbc:mysql://localhost:3306/mydatabase"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" />
In web.xml:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mydb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
From the java classes in which I need db connections, I do this lookup:
Context initContext = new InitialContext();
DataSource ds = (DataSource)initContext.lookup("java:/comp/env/jdbc/mydb");
My first doubt is the DataSource type. Is it the same using javax.sql.DataSource or org.apache.tomcat.jdbc.pool.DataSource?
Moreover, sometimes I obtain a "Too many connections" error.
I've read many stackoverflow question/answers about this, but I don't succeed in understanding where the problem could be.
I have followed the tomcat docs, and I close properly result sets, statements and connection.
EDIT
My tomcat version is 7.0.26. So there should be a bug (see link suggested by informatik01 user)
If you put the JDBC resource in the $CATALINA_HOME/conf/context.xml it loads the resource for every single webapp you have deployed. (Which can mean a huge number of connections) If you move that resource to META-INF/context.xml of your webapp it will only load when that specific webapp is deployed. http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html
It could also be that you have way too many maxActive and maxIdle.
javax.sql.DataSource is an interface and org.apache.tomcat.jdbc.pool.DataSource is a class. I am not sure if tomcat permits us to directly instantiate org.apache.tomcat.jdbc.pool.DataSource. If yes, you can use any of these.
The connection related error could be due to
maxActive="1000" maxIdle="100" maxWait="10000" in your tomcat configuration file.
Set it to maxActive="10" maxIdle="10" maxWait="10" - 10 number of active connections, 10 number of idle connections with a maximum 10 seconds wait time.
You may need to increase the max connection on mysql , the default max is 151.
Make sure you don't have a resource leak: Example java.sql.Connection not getting closed

DBCP Tomcat connection pool leak

<Resource name="myConn" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:#10.10.10.10.:1521:mydb"
username="username" password="password" maxActive="500" maxIdle="50"
maxWait="-1" removeAbandoned="true" removeAbandonedTimeout="60" logAbandoned="true" accessToUnderlyingConnectionAllowed="true"
/>
I am trying to find out areas of the application where connections are NOT being closed. I added the removeAbandoned and logAbandoned clauses in my context file but if i check v$session on oracle it is still showing the same number of connections active even after 60 seconds. Is there something wrong in the configuration above?
I would set maxActive to smaller value like 50 and then check if the configuration is working correctly.
According to the docs the connections pool must running low to execute the check for abandoned connections:
When available db connections run low
DBCP will recover and recycle any
abandoned dB connections it finds.
I would also changed the removeAbandonedTimeout to 20 so that you won't have to wait to long to check if the detector is working fine.

Categories

Resources