简体   繁体   中英

Hibernate + PostgreSQL throws an Exception: org.hibernate.exception.GenericJDBCException: Cannot open connection

i write a java maven project for restful webservice using jersey + hibernate and having this errror as follow

    javax.servlet.ServletException: 
    org.hibernate.exception.GenericJDBCException: Cannot open connection

org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:419)

    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:
    381)

org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)

    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:
    221)


pom.xml File:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmln

s:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    http://maven.apache.org/maven-v4_0_0.xsd">



<modelVersion>4.0.0</modelVersion>


<groupId>org.asad.login</groupId>

<artifactId>login</artifactId>

<packaging>war</packaging>

<version>0.0.1-SNAPSHOT</version>

<name>login</name>

<build>

    <finalName>login</finalName>

    <plugins>

        <plugin>

            <groupId>org.apache.maven.plugins</groupId>

            <artifactId>maven-compiler-plugin</artifactId>

            <version>2.5.1</version>

            <inherited>true</inherited>

            <configuration>

                <source>1.7</source>


                <target>1.7</target>

            </configuration>

        </plugin>

    </plugins>

</build>

<dependencyManagement>

    <dependencies>

        <dependency>

            <groupId>org.glassfish.jersey</groupId>

            <artifactId>jersey-bom</artifactId>

            <version>${jersey.version}</version>

            <type>pom</type>


            <scope>import</scope>

        </dependency>

    </dependencies>

</dependencyManagement>

<dependencies>

    <dependency>

        <groupId>org.glassfish.jersey.containers</groupId>

        <artifactId>jersey-container-servlet-core</artifactId>


        <!-- use the following artifactId if you don't need servlet 2.x 

compatibility -->

        <!-- artifactId>jersey-container-servlet</artifactId -->

    </dependency>



 <dependency>

javax.servlet

javax.servlet-api

3.1.0

provided

    </dependency>

<dependency>

<groupId>org.hibernate</groupId>

<artifactId>hibernate-envers</artifactId>

<version>3.6.4.Final</version>

</dependency>


<dependency>

<groupId>org.hibernate</groupId>

<artifactId>hibernate-validator</artifactId>

<version>4.2.0.Final</version>

<dependency>

<groupId>postgresql</groupId>

<artifactId>postgresql</artifactId>

<version>9.1-901-1.jdbc4</version>

</dependencies>


<properties>

    <jersey.version>2.16</jersey.version>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>

hibernate.cfg.xml File:

<?xml version='1.0' encoding='utf-8'?>

    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<session-factory>


    <!-- Database connection settings -->

    <property name="connection.driver_class">org.postgresql.Drive</property>

    <property 

name="connection.url">jdbc:postgresql://localhost:5432/logindetail

    <property name="connection.username">postgres</property>

    <property name="connection.password">project</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>

    <!-- Drop and re-create the database schema on startup -->

    <property name="hbm2ddl.auto">update</property>


    <property                name="connection.driver_class">org.postgresql.Driver</property>

    <!-- Names the annotated entity class -->

    <mapping class="org.asad.dto.DatabaseClass"/>

</session-factory>

Class for getting data from table:

    public class LoginService{

SessionFactory sessionFactory = null;

    Session session = null;


    public String getDatabaseUser(){
        DatabaseClass user = null;

        sessionFactory = new Configuration().configure().buildSessionFactory();

        session = sessionFactory.openSession();

        session.beginTransaction();

        user = (DatabaseClass)session.get(DatabaseClass.class, 1);

        String name = user.getName();

        session.getTransaction().commit();

        session.close();

        return name;

    }

}

anyone can help me to eliminate this error i will b thankful.......... :)

Use try catch block for getting the appropriate exception

public String getDatabaseUser(){
        DatabaseClass user = null;
try{
        sessionFactory = new Configuration().configure().buildSessionFactory();

        session = sessionFactory.openSession();

        session.beginTransaction();

        user = (DatabaseClass)session.get(DatabaseClass.class, 1);

        String name = user.getName();

        session.getTransaction().commit();
        session.close();
    }catch(Exception e){
       e.printStackTrace();
     }
    return name;
        }

<property name="hibernate.connection.url"> jdbc:postgresql://localhost:5432/logindetail</property>

Here use only

<property name="hibernate.connection.url"> jdbc:postgresql://localhost:5432/login</property>

You have provided there may be problem due to incorrect package path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM