2010-06-18 78 views
2

我寫一個測試Java應用程序和它的作品的權利,但是當我點擊「保存這個網絡應用程序拋出這樣的異常,並以相同cfg.xml文件休眠+ PostgreSQL的拋出JDBCConnectionException:無法打開連接

<property name="hibernate.bytecode.use_reflection_optimizer">false</property> 
<property name="hibernate.connection.autocommit">true</property> 
<property name="hibernate.connection.release_mode">auto</property> 
<property name="hibernate.current_session_context_class">thread</property> 
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property> 
<property name="hibernate.connection.password">1234</property> 
<property name="hibernate.connection.url">jdbc:postgresql://localhost/postgres</property> 
<property name="hibernate.connection.username">postgres</property> 
<property name="hibernate.default_schema">public</property> 
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property> 

<property name="hibernate.ejb.discard_pc_on_close">false</property> 
<property name="hibernate.query.jpaql_strict_compliance">true</property> 
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> 
<property name="hibernate.transaction.flush_before_completion">false</property> 
<property name="hibernate.show_sql">false</property> 
<property name="hibernate.generate_statistics">false</property> 
<property name="hibernate.use_sql_comments">false</property> 
<property name="hibernate.connection.pool_size">100</property> 

「按鈕,我得到以下異常:

javax.servlet.ServletException: org.hibernate.exception.JDBCConnectionException: Cannot open connection 
javax.faces.webapp.FacesServlet.service(FacesServlet.java:325) 

root cause 

javax.faces.el.EvaluationException: org.hibernate.exception.JDBCConnectionException: Cannot open connection 
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102) 
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) 
javax.faces.component.UICommand.broadcast(UICommand.java:315) 
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775) 
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267) 
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82) 
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 
javax.faces.webapp.FacesServlet.service(FacesServlet.java:312) 

root cause 

org.hibernate.exception.JDBCConnectionException: Cannot open connection 
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:98) 
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) 
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52) 
org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449) 
org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167) 
org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142) 
org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85) 
org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1463) 
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
java.lang.reflect.Method.invoke(Unknown Source) 
org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:344) 
$Proxy108.beginTransaction(Unknown Source) 
com.yemex.beans.CompanyBean.saveOrUpdate(CompanyBean.java:52) 
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
java.lang.reflect.Method.invoke(Unknown Source) 
org.apache.el.parser.AstValue.invoke(AstValue.java:196) 
org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276) 
com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98) 
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) 
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) 
javax.faces.component.UICommand.broadcast(UICommand.java:315) 
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775) 
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267) 
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82) 
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 
javax.faces.webapp.FacesServlet.service(FacesServlet.java:312) 

root cause 

java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost/postgres 
java.sql.DriverManager.getConnection(Unknown Source) 
java.sql.DriverManager.getConnection(Unknown Source) 
org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133) 
org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446) 
org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167) 
org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142) 
org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85) 
org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1463) 
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
java.lang.reflect.Method.invoke(Unknown Source) 
org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:344) 
$Proxy108.beginTransaction(Unknown Source) 
com.yemex.beans.CompanyBean.saveOrUpdate(CompanyBean.java:52) 
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
java.lang.reflect.Method.invoke(Unknown Source) 
org.apache.el.parser.AstValue.invoke(AstValue.java:196) 
org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276) 
com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98) 
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) 
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) 
javax.faces.component.UICommand.broadcast(UICommand.java:315) 
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775) 
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267) 
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82) 
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 
javax.faces.webapp.FacesServlet.service(FacesServlet.java:312) 

塊引用

回答

0

有什麼問題嗎?

唯一的例外是很明確的:

org.hibernate.exception.JDBCConnectionException:無法打開連接

確保您可以連接到您連接到數據庫,該配置正確的,你沒有網絡問題,防止連接等

+0

但我配置休眠cfg.xml中llike那; \t <屬性名= 「hibernate.connection.driver_class」> org.postgresql.Driver \t <屬性名= 「hibernate.connection.password」> 1234 \t <屬性名= 「hibernate.connection.url」 > JDBC:在PostgreSQL://本地主機/ postgres的 \t <屬性名= 「hibernate.connection.username」> postgres的 \t <屬性名= 「hibernate.default_schema」>公共 <屬性名=「hibernate.dialect 「> org.hibernate.dialect.PostgreSQLDialect – 2010-06-18 15:43:56

+0

好吧,你有一個postgres數據庫在默認端口上運行在本地主機上,它有一個名爲postgres的數據庫嗎?它是否有一個名爲postgres的用戶名爲postgres? – 2010-06-18 15:56:32

+0

是有一個數據庫是名稱postgres和使用默認端口 這個數據庫有一個用戶名爲postgres和它的通行證='1234' – 2010-06-18 16:05:18

1

我沒看你的配置文件,但我假設它配置至少相當不錯,因爲堆棧跟蹤指示th它試圖連接到本地主機JDBC url的postgres數據庫。

它看起來像一個大問題是,它無法找到的Postgres JDBC驅動程序:

根源

值java.sql.SQLException:PostgreSQL的:找到了JDBC沒有合適的驅動程序/ /本地主機/ postgres的java.sql.DriverManager.getConnection(未知來源)java.sql.DriverManager.getConnection(未知來源)org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)

確保JAR文件在您的類路徑中。如果您還沒有它,可以在這裏下載: http://jdbc.postgresql.org/

+0

我寫了一個測試Java應用程序,它的工作原理正確,但我認爲這個Web應用程序有問題。我控制着JDBC驅動程序,它是正確的 – 2010-06-18 16:07:32

6

遇到異常時,檢查堆棧跟蹤中最底層的根本原因是值得的。在這種情況下,它是以下幾點:

java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost/postgres 

這只是意味着該URL是錯誤的,或者所需的驅動程序不存在當前運行時類路徑。由於根據PostgreSQL JDBC documentation,URL看起來很好,所以當前運行時類路徑中缺少驅動程序。

因此,要解決此問題,您需要將JDBC驅動程序JAR文件放入webapp的運行時類路徑中。 /WEB-INF/lib是webapp的默認運行時類路徑覆蓋的文件夾之一。只需將PostgreSQL JDBC驅動程序JAR文件放在那裏並重新部署即可。

+0

驅動程序路徑已經是/ WEB-INF/lib,但同樣的問題繼續 – 2010-06-18 17:18:28

+1

顯然已經使用了不同的類加載器。嘗試在'Tomcat/conf/catalina.properties'中放入'Tomcat/lib'或將JAR文件的根路徑添加到'shared.loader'。 – BalusC 2010-06-18 17:24:39

0

確認您在classpath中具有用於數據庫驅動程序的jar。 驗證驅動程序類名是否正確。 驗證connection.url是否正確。 我得到了同樣的錯誤。唯一不同的是,我正在使用Oracle 11g。我在類路徑中使用了ojdbc14.jar,驅動程序類名稱正確,但connection.url屬性錯誤。 在Oracle 11g中的情況下的正確的格式是

JDBC:預言:瘦:@localhost:1521:global_dsn_name

相關問題