Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 由于此结果集已关闭,无法打开连接_Java_Spring_Postgresql_Hibernate_Jpa - Fatal编程技术网

Java 由于此结果集已关闭,无法打开连接

Java 由于此结果集已关闭,无法打开连接,java,spring,postgresql,hibernate,jpa,Java,Spring,Postgresql,Hibernate,Jpa,我正在尝试使用Maven Spring MVC JPA和Postgres制作一个web应用程序。但我仍然无法在数据库中持久化对象。我在启动事务时出错: org.hibernate.exception.GenericJDBCException: Could not open connection caused by org.postgresql.util.PSQLException: This ResultSet is closed. 这是密码 @Controller public class

我正在尝试使用Maven Spring MVC JPA和Postgres制作一个web应用程序。但我仍然无法在数据库中持久化对象。我在启动事务时出错:

org.hibernate.exception.GenericJDBCException: Could not open connection
caused by org.postgresql.util.PSQLException: This ResultSet is closed.
这是密码

@Controller
public class ExamController {

@GetMapping("/hello")
public String hello(Model model) {

    // EntityManager entityManager = 
EntityManagerUtil.getEntityManager();

    try {
        EntityManager entityManager = 
            Persistence.createEntityManagerFactory("examinatorpu").createEntityManager();

        entityManager.getTransaction().begin();
        entityManager.persist(new Exam());
        entityManager.getTransaction().commit();
        entityManager.close();
        System.out.println("_exam_ok_");

    } catch (Exception e) {
        // entityManager.getTransaction().rollback();
        e.printStackTrace();
        System.out.println("_exam_ko_");
    }

    model.addAttribute("examId", "1");

    return "welcome";
    }
}
这是我的实体

@Entity
@Table(name = "EXAM")
public class Exam {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "EXAM_ID")
    private long id;

    @Column(name = "TITLE") 
    private String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

}
这是我的persistence.xml

<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="examinatorpu"
    transaction-type="RESOURCE_LOCAL">
    <class>examinator.entity.Exam</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" /> <!-- DB Driver -->
        <property name="javax.persistence.jdbc.url"
            value="jdbc:postgresql://localhost:5432/examinatordb" />
        <property name="javax.persistence.jdbc.user" value="postgres" /> <!-- DB User -->
        <property name="javax.persistence.jdbc.password" value="admin" /> <!-- DB Password -->
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" /> <!-- DB Dialect -->
        <property name="hibernate.hbm2ddl.auto" value="update" /> <!-- create / create-drop / update -->
        <property name="hibernate.show_sql" value="true" /> <!-- Show SQL in console -->
        <property name="hibernate.format_sql" value="true" /> <!-- Show SQL formatted -->
        <!-- <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false" 
            /> -->

    </properties>
</persistence-unit>

我刚刚将JDBC的版本更改为最新版本,现在它可以正常工作了

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.4-1200-jdbc41</version>
</dependency>

org.postgresql
postgresql
9.4-1200-jdbc41

正如Judy1989所说,您需要更新postgres jdbc驱动程序

这个

<dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4-1200-jdbc41</version>
    </dependency>

org.postgresql
postgresql
9.4-1200-jdbc41

他也为我工作。谢谢大家

您使用的是哪个版本的PostgreSQL和哪个版本的PostgreSQL JDBC驱动程序?这是我执行select version()时得到的结果;在pgadmin:PostgreSQL 10.0 on x86_64-apple-darwin上,由i686-apple-darwin11-llvm-gcc-4.2(gcc)4.2.1(基于apple Inc.build 5658)(llvm build 2336.11.00)编译,64位,在maven上:PostgreSQL PostgreSQL 9.0-801.jdbc4 9.4不是最新版本,最新版本是42.1.4请参见
<dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4-1200-jdbc41</version>
    </dependency>