Mysql – web application can’t connect to database

jdbcMySQLtomcatubuntu-10.04

I have a web application which accesses MySQL using Hibernate as a JPA provider. Though all is well on my development rig, once I deploy the WAR to my server I get the following error:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Cannot open connection

The dev machine runs Ubuntu 10.10 and the server 10.04; both are using the provided MySQL package and Apache's (not Ubuntu's) tomcat6. I have more than triple-checked that the database is running, the credentials are correct and the driver is available.

I have this Resource definition in tomcat's context.xml:

<Resource
    name="jdbc/phenome_repository"
    auth="Container"
    type="javax.sql.DataSource"
    username="correctUserName"
    password="correctPassword"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost/correctDatabaseName"    
/>

Since the web application works locally, I believe its configuration to be correct (it has a resource-ref in its web.xml). As for MySQL, it is configured to listen on 127.0.0.1 and the "correctUser" above has all the necessary privileges (on localhost and 127.0.0.1).

Does anyone know any less usual causes of "Cannot open connection"?

Full stacktrace

Best Answer

In my case it turns out another admin had denied ALL: ALL in hosts.deny and hosts.allow was empty. Placing mysqld: 127.0.0.1 in hosts.allow fixed the problem.

For reference, here are all the potential causes of errors like the one I described:

  • localhost may not connect as per hosts.allow and hosts.deny
  • JDBC driver is not available to tomcat
  • The database is configured to disallow network connections
  • The database is not running

In each of these cases the full stacktrace will be subtly different. A missing driver will be more apparent in the stacktrace than the other causes listed here.

Please suggest any items missing from the above list.