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

最后都变了- 提交于 2020-01-03 04:34:49

问题


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!


回答1:


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}




回答2:


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




回答3:


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




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/17204482/openshift-app-cant-connect-to-mysql-jdbcconnectionexception-could-not-open-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!