问题描述
我为我的 SpringMVC + Maven + Hibernate + MySQL 应用程序生成了一个 .war 文件,它在 localhost 和本地 MySQL 数据库上运行良好.我配置数据库的方式是通过一个 WebAppConfig.java 文件,该文件查看 application.properties 文件并检索适当的信息.
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.
然后我创建了一个 OpenShift 帐户并部署了该 .war 文件.我添加了 MySQL 和 PHPMyAdmin 墨盒,以便我可以维护一个数据库.当我尝试通过我的应用程序检索信息或推送到数据库时,我收到此错误.
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 状态 500 - 请求处理失败;嵌套异常是 org.springframework.transaction.CannotCreateTransactionException: 无法打开 Hibernate Session 进行事务;嵌套异常是 org.hibernate.exception.JDBCConnectionException: 无法打开连接
message 请求处理失败;嵌套异常是 org.springframework.transaction.CannotCreateTransactionException: 无法打开 Hibernate Session 进行事务;嵌套异常是 org.hibernate.exception.JDBCConnectionException: 无法打开连接
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
异常 org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 org.springframework.transaction.CannotCreateTransactionException: 无法打开 Hibernate Session 进行事务;嵌套异常是 org.hibernate.exception.JDBCConnectionException:无法打开连接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)
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
注意:在我的实际代码中,我有实际的 OPENSHIFT_MYSQL_DB_HOST 和 OPENSHIFT_MYSQL_DB_PORT 值,而不是那些占位符!
Note: In my actual code I have the actual OPENSHIFT_MYSQL_DB_HOST and OPENSHIFT_MYSQL_DB_PORT values not those placeholders!
推荐答案
我忘记回答这个问题了.
I forgot to actually answer this question.
我只想再次澄清,使用OPENSHIFT"变量而不是将 ACUTAL 值放在 application.properties 中解决了这个问题.
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.username = ${OPENSHIFT_MYSQL_DB_USERNAME}
db.password = ${OPENSHIFT_MYSQL_DB_PASSWORD}
db.password = ${OPENSHIFT_MYSQL_DB_PASSWORD}
这篇关于OpenShift 应用程序无法连接到 MySQL:JDBCConnectionException:无法打开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!