Search code examples
mysqlhibernateamazon-ec2tomcat7rds

Spring war file deployed on ec2 showing hibernate persistence failure connecting to rds


Our company has an app that has been hosted on media temple, we since are moving to amazon ec2. I've been through all tutorials, and got apache, php, mysql, java, and tomcat7 running just fine. Also, I have been able to connect to the rds instance with mysql workbench.

However, when I upload the war file to the tomcat manager I get an error that states it was unable to connect to database.

Here is the top of the error in the catalina.out file

-----------------SNIPPET FROM LOG FILE----------------------------------------

17:28:52.311 [http-bio-8080-exec-1] DEBUG o.s.b.f.s.DisposableBeanAdapter - Invoking destroy method 'close' on bean with name 'dataSource'
17:28:52.311 [http-bio-8080-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Retrieved dependent beans for bean '(inner bean)#73e12051': [org.springframework.se
curity.access.method.DelegatingMethodSecurityMetadataSource#0]
17:28:52.311 [http-bio-8080-exec-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Retrieved dependent beans for bean '(inner bean)#3b35dc09': [(inner bean)#73e12051]
17:28:52.312 [http-bio-8080-exec-1] **ERROR o.s.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in file [/var/lib/tomcat7/webapps/clearbox/WEB
-INF/classes/META-INF/spring/applicationContext.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit
: persistenceUnit] Unable to build EntityManagerFactory**
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553) ~[spring-beans-
4.0.3.RELEASE.jar:4.0.3.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) ~[spring-beans-4.0
.3.RELEASE.jar:4.0.3.RELEASE]

.... AND SO ON.....

SEVERE: The web application [/clearbox] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.

**------------END SNIPPET FROM LOG FILE-----------------------------**

Here is the code from the applicationContext.xml it attempts to run

**--------------SNIPPET FROM applicationContect.xml----------------**

    <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
        <property name="driverClassName" value="${database.driverClassName}"/>
        <property name="url" value="${database.url}"/>
        <property name="username" value="${database.username}"/>
        <property name="password" value="${database.password}"/>
        <property name="testOnBorrow" value="true"/>
        <property name="testOnReturn" value="true"/>
        <property name="testWhileIdle" value="true"/>
        <property name="timeBetweenEvictionRunsMillis" value="1800000"/>
        <property name="numTestsPerEvictionRun" value="3"/>
        <property name="minEvictableIdleTimeMillis" value="1800000"/>
        <property name="validationQuery" value="SELECT 1"/>
    </bean>

    <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

---------------END SNIPPET ------------------------------------------

Here is the database.properties that holds the info for applicationContext.xml

-------------------SNIPPET FROM database.properties----------------

database.driverClassName=com.mysql.jdbc.Driver
database.url=jdbc\:mysql\://localhost\:3306/clearboxdb
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
database.username=MYUSERNAMEHERE
database.password=MYPASSWORDHERE

---------------------END SNIPPET---------------------------------------

The rds has a security group which has the following for Inbound: MYSQL TCP 3306 0.0.0.0/0

Everything works currently on my local tomcat7, and our existing live app with tomcat7. So I'm not sure where the error is.

  • Is it at the ec2/rds security groups level?
  • Is it at the hibernate / spring persistence level?
  • Is it at the mysql user level?

Can anyone please help, or point me to an existing post with the answer. I've surfed around stackoverflow for a couple days now, and decided to ask this question.

A tip that may help is, before we started with ec2, a new developer was getting the same error when deploying the war locally. We found a solution, in that for his system only mysql username: root with no password caused the war file to work.

So, now I'm seeing this error again, but I am not able to set a root with no password on the live system.


Solution

  • Your database url is set to localhost, if you are using RDS this is not the case, instead you should replace it with the hostname provided in the rds console (something like dbname.somerandomchars.rds.eu-west... Sorry I dont have an example to hand so this is a guess from memory it may be slightly different order). You will also need to make sure you set the db user name and password to the RDS values.

    One other thing if your database is accessible from workbench, does this mean you selected "publicly accessible" if so you must change your security group, allowing all traffic is very insecure, if it absolutely must be public you should use security groups to narrow down to just a limited set.