2

I signed up for a new AWS free account, and I am trying to to connect the Tomcat to the RDS database through JNDI Pool connection. The war deployed on the Beanstalk is the default one (which does not have any database connection or queries)

Here is how my context.xml looks like

<Context>

<WatchedResource>WEB-INF/web.xml</WatchedResource>

<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<Manager pathname="" />

<!-- Uncomment this to enable Comet connection tacking (provides events
     on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
<Resource name="jdbc/web"
      auth="Container"
      type="javax.sql.DataSource"
      factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
      testWhileIdle="true"
      testOnBorrow="true"
      testOnReturn="false"
      validationQuery="SELECT 1"
      timeBetweenEvictionRunsMillis="30000"
      maxActive="10"
      maxIdle="5"
      minIdle="1"
      removeAbandonedTimeout="60"
      removeAbandoned="false"
      logAbandoned="true"
      minEvictableIdleTimeMillis="30000"
      closeMethod="close"
      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://aamhszwo5p1rrw.cf7vbk9ataaz.us-west-2.rds.amazonaws.com:3306/broadleaf"/>
<Resource name="jdbc/storage"
          auth="Container"
          type="javax.sql.DataSource"
          factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
          testWhileIdle="true"
          testOnBorrow="true"
          testOnReturn="false"
          validationQuery="SELECT 1"
          timeBetweenEvictionRunsMillis="30000"
          maxActive="10"
          maxIdle="5"
          minIdle="1"
          removeAbandonedTimeout="60"
          removeAbandoned="false"
          logAbandoned="true"
          minEvictableIdleTimeMillis="30000"
          closeMethod="close"
          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://aamhszwo5p1rrw.cf7vbk9ataaz.us-west-2.rds.amazonaws.com:3306/broadleaf"/>
<Resource name="jdbc/secure"
          auth="Container"
          type="javax.sql.DataSource"
          factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
          testWhileIdle="true"
          testOnBorrow="true"
          testOnReturn="false"
          validationQuery="SELECT 1"
          timeBetweenEvictionRunsMillis="30000"
          closeMethod="close"
          maxActive="10"
          maxIdle="5"
          minIdle="1"
          removeAbandonedTimeout="60"
          removeAbandoned="false"
          logAbandoned="true"
          minEvictableIdleTimeMillis="30000"
          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://aamhszwo5p1rrw.cf7vbk9ataaz.us-west-2.rds.amazonaws.com:3306/broadleaf"/>
<Resource name="jdbc/event"
          auth="Container"
          type="javax.sql.DataSource"
          factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
          testWhileIdle="true"
          testOnBorrow="true"
          testOnReturn="false"
          validationQuery="SELECT 1"
          timeBetweenEvictionRunsMillis="30000"
          closeMethod="close"
          maxActive="10"
          maxIdle="5"
          minIdle="1"
          removeAbandonedTimeout="60"
          removeAbandoned="false"
          logAbandoned="true"
          minEvictableIdleTimeMillis="30000"
          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://aamhszwo5p1rrw.cf7vbk9ataaz.us-west-2.rds.amazonaws.com:3306/broadleaf"/>

</Context>

When I restart my tomcat I receive the following exception.

SEVERE: Unable to create initial connections of pool.
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Too many connections
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.Util.getInstance(Util.java:381)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:910)
    at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3923)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1273)

I am not sure how the connections are getting maxed out without having my webapp fire any query or even connect to database

Tushar
  • 1,166
  • 4
  • 14
  • 31
  • 1
    You have 4 pools all connect to same database. Maybe you're over quota there for connections to that DB? – Jan Dec 17 '15 at 08:04
  • @Jan I see around 20 connections in the mysql all in sleep state after I start my Tomcat. What can be done? – Tushar Dec 17 '15 at 08:16
  • So 2 pools did manage to come up but failure on one of the other two. Try reducing maxActive to 3 for starters - just to check if that's the root of your evil. **OR** Just go down to *one* pool and use this all over your app - it's all connected to the same db with same credentials anyway? – Jan Dec 17 '15 at 08:17
  • Yes, all pointing to same database with same credentials. I will try reducing the maxActive , Thanks a lot ! – Tushar Dec 17 '15 at 08:21
  • I'll wait for your check :-) – Jan Dec 17 '15 at 08:24
  • @Jan It works !! Thanks, was wondering if it will have any performance effects on the application – Tushar Dec 17 '15 at 08:25
  • 1
    Then allow me the pleasure of adding this as answer :-) If you'll be so kind to accept it. – Jan Dec 17 '15 at 08:26

1 Answers1

3

You're facing a quota of allowed connections into your AWS database. By setting up 4 datasources with each 10 connections into that database, you exceed that limit.

By reducing the maxActive to something lower (say... 3?) you should be good to go (for now)

As for performance considerations: This will work fine as long as none of your pools gets too active.

In the long term I'd recommend one or more of those:

  • get a larger connections quota on your db in AWS
  • get more users with different permissions to reflect your desired usage
  • reduce number of pools - if they're on the same db doing similar stuff, you might be better of with one pool with all your quota assigned. So you can assign connections where they're needed most at that time.
Jan
  • 13,738
  • 3
  • 30
  • 55