EntityManagerFactory关闭,休眠 [英] EntityManagerFactory is closed, Hibernate

查看:466
本文介绍了EntityManagerFactory关闭,休眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近创建了一个Web服务,该服务使用Java中的静态方法从数据库中获取项目列表。

I have recently created a web service that uses a static method in Java to obtain a list of items from the database.

该Web服务可以完美工作并返回JSON回到呼叫者。但是,它只能运行一次。如果您尝试刷新或发出新请求,我会收到 EntityManagerFactory关闭错误。

The web service works perfectly and returns JSON back to the caller. However, it works only once. If you try to refresh or make a new request, I get a EntityManagerFactory is closed error.

这是Web Service类看起来像:

Here's how the Web Service class looks like:

public class WebService extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
    //obtain the list of vehicles from the database
        List<Vehicle> vehicles = ExecuteVehicle.getVehicleList();

        //create the Gson object and generate the Json
        Gson gson = new Gson();
        JsonElement element = gson.toJsonTree(vehicles, new TypeToken<List<Vehicle>>(){}.getType());

        //send the list of vehicles
        JsonArray jsonArray = element.getAsJsonArray();
        resp.setContentType("application/json");
        resp.getWriter().print(jsonArray);
    }
}

如您所见,车辆清单正在使用 ExecuteVehicle.getVehicleList()方法填充。

As you can see, the list of vehicles is being populated using the ExecuteVehicle.getVehicleList() method.

该方法的外观如下:

public static List<Vehicle> getVehicleList(){

    //open a Session
    Session session = HibernateUtilities.getSessionFactory().openSession();

    //start a transaction
    session.beginTransaction();

    //SELECT STATEMENT for the entire list of Vehicles
    Query<Vehicle> query = session.getNamedQuery("SelectAllVehicles"); //query name is declared in the mapping file
    List<Vehicle> vehicles = query.list();

    //commit the changes and end the transaction
    session.getTransaction().commit();

    //close the Session
    session.close();

    //close the SessionFactory
    HibernateUtilities.getSessionFactory().close();

    return vehicles;
}

这里是负责会话的HibernateUtilities类,依此类推:

Here's the HibernateUtilities class that takes care of the Session and so on:

public class HibernateUtilities {
    private static SessionFactory sessionFactory;
    private static StandardServiceRegistry standardServiceRegistry;

    static{
        try {
            //configure and build the service registry
            standardServiceRegistry = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();

            //create the metadata
            Metadata metadata = new MetadataSources(standardServiceRegistry).getMetadataBuilder().build();

            //build the SessionFactory
            sessionFactory = metadata.getSessionFactoryBuilder().build();
        } catch (HibernateException e) {
            //in case the SessionFactory cannot be built, then the stackTrace is displayed
            e.printStackTrace();        
        }
    }

    //method that returns the Hibernate session factory
    public static SessionFactory getSessionFactory(){
        return sessionFactory;
    }
}

我的问题是-如何避免 EntityManagerFactory已关闭错误。此外,我将不得不一次又一次地实时获取此列表。使用Hibernate可行吗?要以实时方式(例如,每2秒左右)从数据库中获取项目列表?我知道这取决于项目的数量,等等,但是我从技术的角度出发-从我的理解来看,开启和关闭本次会议需要很长时间-我可以在同一次会议中一遍又一遍地进行此操作吗?

The question I have is - how can I avoid the EntityManagerFactory is closed error. Furthermore, I will have to obtain this list again and again, in a real time manner. Is that feasible with Hibernate? To obtain a list of items from a database in a real-time manner (say, every 2 seconds or so)? I know this depends on the number of items and so on, but I'm asking from a technical standpoint - from what I understand, opening and closing the Session takes a long time - could I do this over and over again in the same Session and if so, how?

推荐答案

我会说你在那里做得太多。

I would say that you are doing too much there.

您必须使用工厂的 openSession()方法刷新/提交事务并关闭会话。

You have to flush/commit the transaction and close the session as you are using the openSession() method of the factory.

但是我不认为您需要关闭SessionFactory

But i dont think you need to close the SessionFactory itself

//close the SessionFactory
HibernateUtilities.getSessionFactory().close();

删除此行,您将可以多次使用工厂。

remove this line and you can would be able to use the factory many times.

这篇关于EntityManagerFactory关闭,休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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