user2156803
user2156803

Reputation: 43

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.

Upvotes: 0

Views: 915

Answers (1)

coderLMN
coderLMN

Reputation: 3076

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.

Upvotes: 1

Related Questions