当应该清除所有内容时,为什么会向我发送未关闭的连接错误? [英] Why is it sending me an unclosed connection error when everything should be clean?

查看:165
本文介绍了当应该清除所有内容时,为什么会向我发送未关闭的连接错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的一对一映射.当我第一次尝试使用外键提取信息时,我收到一条错误消息:ERROR:检测到连接泄漏:关闭池jdbc:mysql://localhost:3306/novabaza时,有1个未关闭的连接? useSSL = false& serverTimezone = UTC 线程主"中的异常java.lang.NullPointerException 在oto_otm_mtm.Blogic.main(Blogic.java:46).第二次我运行该程序时,它总是运行顺利.

I have a basic one to one mapping. When i try to extract information with the foreign key the first time i run the program i get an error message:ERROR: Connection leak detected: there are 1 unclosed connections upon shutting down pool jdbc:mysql://localhost:3306/novabaza?useSSL=false&serverTimezone=UTC Exception in thread "main" java.lang.NullPointerException at oto_otm_mtm.Blogic.main(Blogic.java:46). Second time i run the progam it always goes smoothly.

试图关闭该会话,然后开始一个新会话,因为我认为问题是我正试图从一个不存在的库中检索数据.什么都没改变.

Tried to close the session then begin a new one because i thought that the problem is that I'm trying to retrieve data from a base that doesn't exist. Nothing changed.

         try {

    session.beginTransaction();
    session.save(student);
    session.save(laptop);
    session.getTransaction().commit();
            session.close();


    session = sf.getCurrentSession();
    session.beginTransaction();

    Student myStudent = session.get(Student.class, 2);
    lpa = myStudent.getLaptop(); /*Eclipse says that the problem is 
                                       here though i dont understand why.*/

    System.out.println(lpa.getVrsta());

    session.close();

    } finally {
        sf.close();
    }

推荐答案

您正在关闭会话,但没有关闭已建立的数据库连接.因此,连接泄漏.请关闭数据库连接.这样可以解决问题.

You are closing the session but you are not closing the established database connection. Hence the connection leak. Please close the database connection. This will resolve the issue.

在上面的代码中,要使您的所有语句都包含在单个事务中.

A titbit is to in your above code, have all your statements within a single transaction.

这篇关于当应该清除所有内容时,为什么会向我发送未关闭的连接错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆