EntityManagerFactory 关闭,休眠

Rob*_*scu 5 java web-services hibernate jpa entitymanager

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

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

Web Service 类如下所示:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,车辆列表正在使用该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;
}
Run Code Online (Sandbox Code Playgroud)

这是负责处理会话等的 HibernateUtilities 类:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Mac*_*ski 6

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

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

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

//close the SessionFactory
HibernateUtilities.getSessionFactory().close();
Run Code Online (Sandbox Code Playgroud)

删除这条线,你就可以多次使用工厂了。