java - 使用 hibernate : psql: could not connect to server: Connection refused

标签 java hibernate postgresql

对于我的问题,我刚刚发现了这个问题 [ Postgresql : Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections 但这对我没有太大帮助。 我尝试了他们建议的所有方法。

我用

  • 装有 Ubuntu 12.04 LTS 服务器的虚拟机
  • PostgreSQL 9.3
  • Win7 客户端
  • eclipse 开普勒

我还使用 mySQL 进行测试,一切正常(因此可以连接到 VM 等)

我的日志:

Jul 07, 2015 12:49:35 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
Jul 07, 2015 12:49:35 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.10.Final}
Jul 07, 2015 12:49:35 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jul 07, 2015 12:49:35 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jul 07, 2015 12:49:35 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: de/motza/init/hibernatePSQL.cfg.xml
Jul 07, 2015 12:49:35 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: de/motza/init/hibernatePSQL.cfg.xml
Jul 07, 2015 12:49:35 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Jul 07, 2015 12:49:35 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Jul 07, 2015 12:49:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Jul 07, 2015 12:49:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://10.158.251.251:5432/history_log]
Jul 07, 2015 12:49:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=postgres, password=****}
Jul 07, 2015 12:49:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Jul 07, 2015 12:49:35 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)

我的错误:

Initial SessionFactory creation failed.
org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:132)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:118)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:140)
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:58)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:75)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
    at de.motza.init.HibernateUtil.getSessionFactoryPSQL(HibernateUtil.java:35)
    at de.motza.test.History_Log.testPSQL(History_Log.java:124)
    at de.motza.test.History_Log.main(History_Log.java:99)
Caused by: org.postgresql.util.PSQLException: Connection to 10.158.251.251:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:239)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
    at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:127)
    at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)
    at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:21)
    at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:41)
    at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
    at org.postgresql.Driver.makeConnection(Driver.java:414)
    at org.postgresql.Driver.connect(Driver.java:282)
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:55)
    ... 15 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at org.postgresql.core.PGStream.<init>(PGStream.java:61)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:121)
    ... 24 more

我的 hibernate 配置

<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url">jdbc:postgresql://10.158.251.251:5432/history_log</property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">postgres</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!--  create -> Drop the existing tables and create new one -->
        <!--  create-drop -> Drop the existing tables and create new one, when sessionFactory closes, the table will be dropped -->
        <!--  update -> updates table values, but saves data (not in production) -->
        <!--  validate -> validate existing table and not update or change database, throws exception -->
        <!--  for production leave this method out -->
        <property name="hbm2ddl.auto">create</property>

        <!-- Mention here all the model classes along with their package name -->
        <mapping class="de.motza.test.LogEntry"/>
        <mapping class="de.motza.test.Parameter"/>


    </session-factory>
</hibernate-configuration>

创建 session 工厂的 Java 代码:

if (sessionFactoryPSQL == null || sessionFactoryPSQL.isClosed()) {
            Configuration conf = new Configuration().configure("de/motza/init/hibernatePSQL.cfg.xml");
            serviceRegistry = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
            try {
                sessionFactoryPSQL = conf.buildSessionFactory(serviceRegistry);
            } catch (Exception e) {
                System.err.println("Initial SessionFactory creation failed.");
                e.printStackTrace();
                System.exit(0);
            }
        }
        return sessionFactoryPSQL;

我还使用了 sudo netstat -taupen | grep postgres

tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      105        13970       3013/postgres
tcp6       0      0 ::1:5432                :::*                    LISTEN      105        13969       3013/postgres
udp6       0      0 ::1:37902               ::1:37902               ESTABLISHED 105        13981       3013/postgres

我的 pg_hba.conf 看起来像这样:

# Database administrative login by Unix domain socket
local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
host    all             all             0.0.0.0/0               md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
host    all             all             0.0.0.0/0               md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            md5
#host    replication     postgres        ::1/128                 md5

我也尝试了不同的端口,但它仍然是相同的错误。

这是我学士论文的一部分,我不知道该怎么做了...... 希望有人能帮助我

最佳答案

Postgres 正在监听 127.0.0.1:5432,但您正在尝试连接到 10.158.251.251:5432。即使这些 IP 地址解析到同一台机器,它们在邮局主管的眼中也不会相等(第一个是本地连接,第二个不是)。

也将其他 IP 添加到您的 pg_hba.conf。

关于java - 使用 hibernate : psql: could not connect to server: Connection refused,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31266913/

相关文章:

java - hibernate 映射中的另一个重复列

postgresql - 当发生死锁时,postgres 日志文件中的 "tuple (0,79)"是什么意思?

java - 如何在Android中将ArrayList存储到sharedPreferences中

java - CompletableFuture.runAsync 未完成执行

java - 通过 socket 包裹

java - JPA 标准 Updatequery 并未真正更新任何内容

Spring、Hibernate、MySQL压力测试

sql - Postgres - 'time without time zone' 范围和排除约束

仅在 2 :00 UTC regardless of Daylight Savings Time 处检索带时间戳(带时区)的行的 SQL 查询

java - Android - 多次调用 DownloadManager/BroadcastReceiver