【问题标题】:Hibernate+PostgreSQL throws JDBCConnectionException: Cannot open connectionHibernate+PostgreSQL 抛出 JDBCConnectionException:无法打开连接
【发布时间】:2011-03-05 12:06:23
【问题描述】:

我编写了一个测试 Java 应用程序,它可以正常工作,但是这个 Web 应用程序会在同一个 cfg.xml 文件中引发类似的异常

<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)

块引用

【问题讨论】:

    标签: java hibernate postgresql jsf jdbc


    【解决方案1】:

    这里有什么问题?

    异常非常明确:

    org.hibernate.exception.JDBCConnectionException:无法打开连接

    确保您可以连接到您所连接的数据库、配置正确、没有阻止连接的网络问题等。

    【讨论】:

    • 但是我像这样配置hibernate cfg.xml; org.postgresql.Driver1234 jdbc:postgresql://localhost/postgrespostgrespublicorg.hibernate.dialect.PostgreSQLDialect
    • 好的,你有一个在默认端口的 localhost 上运行的 postgres 数据库,它是否有一个名为 postgres 的数据库,它是否有一个名为 postgres 的用户,其密码名为 postgres?跨度>
    • 是 有一个名为 postgres 的数据库,使用默认端口 此数据库有一个名为 postgres 的用户,其 pass = '1234'
    • 这个答案有点粗鲁,不是吗?
    【解决方案2】:

    我没有查看您的配置文件,但我假设它至少配置得相当好,因为堆栈跟踪表明它正在尝试通过 localhost JDBC url 连接到 postgres 数据库。

    看起来最大的问题是它找不到 postgres JDBC 驱动程序:

    根本原因

    java.sql.SQLException: 找不到适合 jdbc 的驱动程序:postgresql://localhost/postgres java.sql.DriverManager.getConnection(Unknown Source) java.sql.DriverManager.getConnection(Unknown Source) org.hibernate.connection .DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)

    确保 JAR 文件位于您的类路径中。如果您还没有它,您可以在这里下载它: http://jdbc.postgresql.org/

    【讨论】:

    • 我编写了一个测试 Java 应用程序,它工作正常,但我认为这个 Web 应用程序有问题。而且我控制JDBC驱动是对的
    【解决方案3】:

    当遇到异常时,值得在堆栈跟踪中检查最底层的根本原因。在这种情况下,它是以下内容:

    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 文件放入其中并重新部署即可。

    【讨论】:

    • 驱动路径已经是 /WEB-INF/lib 但同样的问题还在继续
    • 显然使用了不同的类加载器。尝试在Tomcat/conf/catalina.properties 中放入Tomcat/lib 或将JAR 文件的根路径添加到shared.loader
    【解决方案4】:

    确认您的类路径中有数据库驱动程序的 jar。 验证驱动程序类名称是否正确。 验证 connection.url 是否正确。 我得到了同样的错误。唯一不同的是,我使用的是 Oracle 11g。我在 classpath 中有 ojdbc14.jar ,驱动程序类名是正确的,但 connection.url 属性是错误的。 Oracle 11g 的正确格式是

    jdbc:oracle:thin:@localhost:1521:global_dsn_name

    【讨论】:

      猜你喜欢
      • 2015-09-21
      • 1970-01-01
      • 2011-03-13
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-02
      相关资源
      最近更新 更多