Java Hibernate+PostgreSQL 抛出 JDBCConnectionException:无法打开连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3071054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:12:25  来源:igfitidea点击:

Hibernate+PostgreSQL throws JDBCConnectionException: Cannot open connection

javahibernatepostgresqljsfjdbc

提问by Omer Salmanoglu

I write an Test Java APP and it works right BUt this Web app throws an exception like that with to same cfg.xml file

我编写了一个测试 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>

When I press "Save" button I get the following exception:

当我按下“保存”按钮时,出现以下异常:

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)

Blockquote

块引用

采纳答案by matt b

What's the question here?

这里有什么问题?

The exception is pretty explicit:

异常非常明确:

org.hibernate.exception.JDBCConnectionException: Cannot open connection

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

Make sure you can connect to the database you are connected to, that the configuration is correct, that you do not have network issues preventing the connection, etc.

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

回答by Jeff

I didn't look at your config file, but I'm assuming it configured at least reasonably well since the stack trace indicates that it is trying to connect to a postgres database at a localhost JDBC url.

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

It looks like the big problem is that it can't find the postgres JDBC driver:

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

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)

根本原因

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)

Make sure the JAR file is in your classpath. You can download it here, if you don't already have it: http://jdbc.postgresql.org/

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

回答by BalusC

When encountering an exception, it's worth to check the bottommost root cause in the stacktrace. In this case it's the following:

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

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

This just means that either the URL is wrong or that the desired driver is not present in the current runtime classpath. Since the URL looks fine as per the PostgreSQL JDBC documentation, it'll be that the driver is missing in the current runtime classpath.

这仅意味着 URL 错误或当前运行时类路径中不存在所需的驱动程序。由于根据PostgreSQL JDBC 文档,URL 看起来不错,因此当前运行时类路径中缺少驱动程序。

So, to fix this problem, you need put the JDBC driver JAR file in the webapp's runtime classpath. The /WEB-INF/libis one of the folders which is covered by the webapp's default runtime classpath. Just drop the PostgreSQL JDBC driver JAR file in there and redeploy.

因此,要解决此问题,您需要将 JDBC 驱动程序 JAR 文件放在 web 应用程序的运行时类路径中。该/WEB-INF/lib是由web应用程序的默认运行时类路径覆盖的一个文件夹。只需将 PostgreSQL JDBC 驱动程序 JAR 文件放在那里并重新部署。

回答by user55926

Verify that you have the jar for your database driver in classpath. Verify if the driver class name is correct. Verify if the connection.url is correct. I got the same error. Only difference being, I am using Oracle 11g. I had ojdbc14.jar in classpath , driver class name was correct but connection.url property was wrong. The correct format in case of Oracle 11g is

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

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

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