Java entitymanager.close() 不关闭与数据库的连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19107408/
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-12 14:09:04  来源:igfitidea点击:

entitymanager.close() not closing connections to database

javahibernatejpac3p0

提问by Sumit Gupta

I'm developing a quiz system and I'm new to JPA & Hibernate. I've used hibernate 4.2.3 and I've used c3p0 connection pooling. Code works fine but each EntityManager creates a connection which is never closed. And once the max number of connections are reached then application can't access database. I'm using MySQL 5.6.10, when I see connections in workbench I never see connections being destroyed. And application is not reusing the connections.

我正在开发一个测验系统,我是 JPA 和 Hibernate 的新手。我使用了 hibernate 4.2.3 并且使用了 c3p0 连接池。代码工作正常,但每个 EntityManager 创建一个永不关闭的连接。一旦达到最大连接数,应用程序将无法访问数据库。我正在使用 MySQL 5.6.10,当我在工作台中看到连接时,我从未看到连接被破坏。并且应用程序没有重用连接。

My guess is that connections are not returned to connection pool. I dunno how as i have written "manager.close()".

我的猜测是连接不会返回到连接池。我不知道如何写“manager.close()”。

The same happens with hibernate internal connection pooling. (In case i remove c3p0.)

休眠内部连接池也会发生同样的情况。(如果我删除了 c3p0。)

Here are the properties of "persistence.xml"

这是“persistence.xml”的属性

<property name="hibernate.connection.provider_class" value="org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider" />
<property name="hibernate.c3p0.max_size" value="10" />
<property name="hibernate.c3p0.min_size" value="2" />
<property name="hibernate.c3p0.acquire_increment" value="1" />
<property name="hibernate.c3p0.idle_test_period" value="5000" />
<property name="hibernate.c3p0.max_statements" value="20" />
<property name="hibernate.c3p0.timeout" value="500" />

Here is how i access EntityManagerFactory

这是我访问 EntityManagerFactory 的方式

public class EntityMangFactory {

    private static EntityManagerFactory emf=null;

    private static void initEntityManagerFactory()
    {
        emf=Persistence.createEntityManagerFactory("com.oes.jpa");  //persistence-unit-name//

    }

    public static EntityManagerFactory getEntityManagerFactory()
    {
        if(emf==null){
            initEntityManagerFactory();
        }
        return emf;
    }


}

Here is how i access the database.

这是我访问数据库的方式。

public static List<MarksDTO> getMarks(int id){

        EntityManagerFactory factory= EntityMangFactory.getEntityManagerFactory();
        EntityManager manager= factory.createEntityManager();
        manager.getTransaction().begin();
        TypedQuery<MarksDTO> q= manager.createQuery("select new com.examsystem.DTO.MarksDTO(m.courseId,m.score,m.setNo,m.courseName) from MarksBean as m where TraineeID=:TraineeID",MarksDTO.class);
        q.setParameter("TraineeID", id);
        List<MarksDTO> ls=q.getResultList();


        manager.close();

        return ls;
    }

Please point me to where I'm wrong.

请指出我错的地方。

Thanks in advance.

提前致谢。

回答by Sumit Gupta

I wasn't committing each transaction. Hence the problem.

我没有提交每笔交易。因此问题来了。