EntityManagerFactory已关闭,Hibernate

时间:2017-02-06 10:46:19

标签: java hibernate web-services 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);
    }
}

如您所见,正在使用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类,负责处理Session等等:

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秒左右)从数据库中获取项目列表?我知道这取决于项目的数量等等,但我从技术的角度来问 - 从我的理解,开启和关闭会议需要很长时间 - 我可以在同一个会话中一遍又一遍地这样做如果是的话,怎么样?

1 个答案:

答案 0 :(得分:3)

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

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

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

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

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