我在外部服務器上有兩臺虛擬機。 VM1是Tomcat服務器,VM2是MySQL服務器。我在tomcat服務器上有一個web應用程序,我可以通過在我的家用計算機的瀏覽器中輸入ip來訪問該應用程序,以便它可以正常工作。無法獲得外部MySQL服務器以使用Java Web應用程序+ Hibernate。 (無法構建EntityManagerFactory)
例如,如果IP到VM1是111.22.33.44,那麼我就可以從任何地方輸入這一點,並能夠訪問網頁:http://111.22.33.44:8080/WebApplication1
但在那之後,我嘗試在我的web應用程序登錄,我得到一個異常:
javax.persistence.PersistenceException:[PersistenceUnit:WebApplication1PU]無法建立的EntityManagerFactory
我只能假設,這已經是與我的外部數據庫服務器。
的persistence.xml(XXXX爲VM2的IP):
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="WebApplication1PU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>bo.User</class>
<class>bo.LogMessage</class>
<class>bo.PrivateMessage</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.password" value="root"/>
<property name="hibernate.connection.url" value="jdbc:mysql://x.x.x.x:3306/community"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
</properties>
</persistence-unit>
</persistence>
我試圖做修復它:對VM1 IP +用戶名MySQL服務器上
- 授予權限。
- 嗯,這是我現在所能記得的,我會編輯這個。
我也可以在堆棧跟蹤中編輯,如果有幫助的話。
另外:兩個虛擬機有完全不同的IP地址。我讀了一些關於將RESOURCE_LOCAL更改爲JTA的方法,是否可以解決任何問題?
編輯1 traceroute給了2行(跳?) 我評論了my.conf文件中的綁定127.0.0.1行。仍然不起作用。 嘗試在Web應用程序的主頁上登錄時出現異常。 (堆棧跟蹤):
javax.faces.el.EvaluationException: javax.persistence.PersistenceException: [PersistenceUnit: WebApplication1PU] Unable to build EntityManagerFactory
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:409)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:185)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:679)
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: WebApplication1PU] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
at db.UserDB.getUserByUsername(UserDB.java:49)
at bo.UserHandler.UserLogin(UserHandler.java:30)
at ui.UserBean.login(UserBean.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.apache.el.parser.AstValue.invoke(AstValue.java:262)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 24 more
Caused by: org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
at org.hibernate.dialect.DialectFactory.determineDialect(DialectFactory.java:57)
at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:39)
at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:426)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:128)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
... 38 more
編輯2 netstat的-anltp |對VM2(MySQL服務器)
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN -
tcp6 0 0 :::22 :::* LISTEN -
tcp6 0 0 ::1:631 :::* LISTEN -
編輯3 netstat的-anltp grep的 「聽」 | grep的「聽」上VM2(MySQL服務器) 現在聽的外部連接到MySQL(要做到這一點請參見本指南的第4步:link
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN -
tcp6 0 0 :::22 :::* LISTEN -
tcp6 0 0 ::1:631 :::* LISTEN -
我很抱歉,如果這很難理解,我只是很累這個東西整天工作。 – 2012-01-12 20:18:46
您是否能夠使用其他數據庫運行應用程序? Hibernate無法構建EntityManagerFactory可能意味着很多事情:錯誤的類映射,無法連接,防火牆阻止端口等... – clmarquart 2012-01-12 20:47:10
如果您確定已授予「VM1 ip + username」的mysql訪問權限, VM2上的mysql偵聽端口3306並接受非本地主機連接? – 2012-01-12 20:48:57