user1395446
user1395446

Reputation: 31

OpenShift app can't connect to MySQL : JDBCConnectionException: Could not open connection

I generated a .war file for my SpringMVC + Maven + Hibernate + MySQL app which was working perfectly fine on localhost and local MySQL database. The way I configure the database is through a WebAppConfig.java file which looks at an application.properties file and retrieves the appropriate information.

Then I created an OpenShift account and deployed that .war file. I added MySQL and PHPMyAdmin cartridges so I can maintain a database. When I try to retrieve information or push to the database through my application I receive this error.

HTTP Status 500 - Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Could not open connection

message Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Could not open connection

exception org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Could not open connection org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:948) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838) javax.servlet.http.HttpServlet.service(HttpServlet.java:641) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812) javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

I already added the appropriate information for my database in my properties file so I don't think that is the issue.

application.properties

#DB
db.driver = com.mysql.jdbc.Driver
db.url = jdbc:mysql://{OPENSHIFT_MYSQL_DB_HOST}:{OPENSHIFT_MYSQL_DB_PORT}/springmvc
db.username = {OPENSHIFT_MYSQL_DB_USERNAME}
db.password = {OPENSHIFT_MYSQL_DB_PASSWORD}

#Hibernate
hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.show_sql = true
entitymanager.packages.to.scan = org.example.app.model
hibernate.cache.provider_class = org.hibernate.cache.NoCacheProvider

Note: In my actual code I have the actual OPENSHIFT_MYSQL_DB_HOST and OPENSHIFT_MYSQL_DB_PORT values not those placeholders!

Upvotes: 1

Views: 6302

Answers (5)

Warner
Warner

Reputation: 231

I don't understand why openshift force us to use their environment variables instead of using "localhost:3306" and the actual values for username/password. This is making us very inconvenient. Also, adding this line of code jdbc:mysql://${OPENSHIFT_DB_HOST}:${OPENSHIFT_DB_PORT}/${OPENSHIFT_APP_NAME}

in my application-context.xml gets a compilation error since spring doesn't recognize these values.

Upvotes: 0

Tim Spann
Tim Spann

Reputation: 503

Missing the $ in the variable names, you can also run it locally very easily to make sure it's just the mysql variables and not a coding error.

Have you checked PHPMyAdmin to make sure MYSQL is up has the database and tables you expect and validate all your sql.

Does WebAppConfig have the proper Spring annotations? Does it build fully with no errors? Do your unit tests work? Do you have all the maven dependencies and versions established?

This has worked for me using OpenStack on all their available java server types.

Upvotes: 0

user1395446
user1395446

Reputation: 31

I forgot to actually answer this question.

I just want to clarify once again that using the 'OPENSHIFT' variables rather than putting the ACUTAL values in the application.properties fixed the issue.

db.url = jdbc:mysql://${OPENSHIFT_DB_HOST}:${OPENSHIFT_DB_PORT}/${OPENSHIFT_APP_NAME}

db.username = ${OPENSHIFT_MYSQL_DB_USERNAME}

db.password = ${OPENSHIFT_MYSQL_DB_PASSWORD}

Upvotes: 1

Nam Duong
Nam Duong

Reputation: 851

Thanks for posting to our forums as well:
https://www.openshift.com/forums/openshift/openshift-app-cant-connect-to-mysql-jdbcconnectionexception-could-not-open

Looks like you'll want to use:
db.username = {OPENSHIFT_MYSQL_DB_USERNAME} db.password = {OPENSHIFT_MYSQL_DB_PASSWORD}

instead of: db.username = root db.password = pass

Upvotes: 0

Sumana Mehta
Sumana Mehta

Reputation: 2663

Make sure mysql cartridge is up and running; if need be try restarting it. Otherwise, please post your properties file. Also please read the following threads, it may be of help:

https://www.openshift.com/forums/openshift/hibernate-mysql-connection-failing https://www.openshift.com/forums/openshift/mysql-db-stops-responding-after-some-time

Upvotes: 0

Related Questions