EntityManagerFactory 关闭,Hibernate

EntityManagerFactory is closed, Hibernate

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

Web 服务运行良好,returns JSON 返回给调用者。但是,它只工作一次。如果您尝试刷新或发出新请求,我会收到 EntityManagerFactory is closed 错误。

Web 服务 class 如下所示:

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() 方法填充车辆列表。

该方法如下所示:

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 class:

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 is closed 错误。此外,我将不得不以实时方式一次又一次地获取此列表。这对 Hibernate 可行吗?要以实时方式(例如,每 2 秒左右)从数据库中获取项目列表?我知道这取决于项目的数量等等,但我是从技术角度问的——据我了解,打开和关闭会话需要很长时间——我可以在同一个会话中一遍又一遍地这样做吗如果是,怎么做?

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

您必须 flush/commit 事务并关闭会话,因为您正在使用工厂的 openSession() 方法。

但我认为您不需要关闭 SessionFactory 本身

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

去掉这一行,你就可以多次使用工厂了。