简体   繁体   English

实体经理,连接过多

[英]Entitymanager, too many connections

I create only one session factory for the whole progamm and create everytime i want to persist/update/query smth. 我只为整个程序创建一个会话工厂,并在每次我想持久/更新/查询时创建。 an new entity manager but i get always an to many connection error. 一个新的实体管理器,但我总是遇到很多连接错误。 Can anybody give me an adivce? 有人可以给我一个住所吗? In my point of view it cant be the best solution to increase the number of allowed connections in MySql. 以我的观点,这不是增加MySql中允许的连接数的最佳解决方案。 I used C3P0 for pooling. 我使用C3P0进行池化。

Try using a try-catch-finally template like this whenever calling the EntityManager . 每当调用EntityManager时,尝试使用这样的try-catch-finally模板。

EntityManager em = ... //However you get an em.
try {
    em.getTransaction().begin();

    // ...  Put your persistence code here.

    em.getTransaction().commit();
} catch (Exception ex) {
    em.getTransaction().rollback();
    throw ex;
} finally {
    em.close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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