JNDI Datasource Settings for Amazon Beanstalk with RDS

This post shows how you can configure Tomcat for JDBC Connection to MySQl through JNDI connection pooling.

This post assumes that you have already created a site on Amazon Beanstalk and you have an active EC2 environment running. Refer to the following posts if you want to setup a new BeanStalk Site.

Creating a new Amazon Elastic Beanstalk
Setup RDS Environment with BeanStalk

Step 1:>SSH into your EC2 Environment.

Step 2:Go to directory /usr/share/tomcat7/lib and download the mysql connector jar file.
wget http://central.maven.org/maven2/mysql/mysql-connector-java/5.1.35/mysql-connector-java-5.1.35.jar

 

Step 3: Open /usr/share/tomcat7/conf/context.xml file for editing.
sudo vi /usr/share/tomcat7/conf/context.xml

Edit the file to add your JDBC resource, Change the username, password, and connection url accordingly

<!--?xml version='1.0' encoding='utf-8'?-->
<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -->
<!-- The contents of this file will be loaded for each web application -->

    <!-- Default set of monitored resources -->
    WEB-INF/web.xml
    
    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    
    
    <!-- Uncomment this to enable Comet connection tacking (provides events on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->
    


Step 4: Restart your tomcat, it should pick connection to mysql from the context.xml file.

TroubleShooting

If after you restart your tomcat you receive below error

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Too many connections

Then edit your resources to lower down the number of maxActive connections in your context.xml.

tgugnani: Web Stuff Enthusiast.