EntityManager.persist无法正常工作

时间:2014-05-02 14:26:30

标签: java-ee jpa persistence

我有一个测试servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EntityManager em = EMF.get().createEntityManager();
        em.getTransaction().begin()
        DBManager.getInstance().eraseDB();
        em.getTransaction().commit();
        em.getTransaction().begin();
        if(DBManager.getInstance().createUser("first", "second"))
            writer.print("done.");
        else
            writer.print("failed.");
        em.getTransaction().commit();

    }

方法eraseDB是:

@SuppressWarnings("unchecked")
    public void eraseDB(){
        EntityManager em = EMF.get().createEntityManager();
        Query q = em.createQuery("select u from User u");
        List<User> list = q.getResultList(); 
        for(User u : list){
            em.remove(u);   
        }       
    }

虽然方法createUser是:

public static synchronized boolean createUser(String username, String password) {
        EntityManager em = EMF.get().createEntityManager();
        Query q = em.createQuery("select u from User u");
        @SuppressWarnings("unchecked")
        List<User> list = q.getResultList();
        if(list.size() > 0)
            return false;
        User u = new User();
        u.setPassword(password);
        u.setUsername(username);
        em.persist(u);
        return true;
    }

在servlet调用之后,我从createUser方法获得了真,但数据存储区为空。

为什么persist无效?我做错了什么?
关于这种架构的任何其他建议都是值得欢迎的,谢谢。

1 个答案:

答案 0 :(得分:1)

根据Jb nizet的回答:
当您明确指定事务(使用em.getTransaction.begin/commit/rollback)时,如果第一个事务不是提交或回滚,则无法开始第二个事务。在您的代码中,您应该将事务保存到每个DbManager的方法中,例如:

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws    ServletException, IOException {
        EntityManager em = Emf.get().createEntityManager();
        DBManager.getInstance().eraseDB(em);
        if(DBManager.getInstance().createUser(em,"first", "second"))
            writer.print("done.");
        else
            writer.print("failed.");

    }

public void eraseDB(EntityManager em){
    Query q = em.createQuery("SELECT u FROM User u");
    List<User> list = q.getResultList();
    Try{
        em.getTransaction().begin(); 
        for(User u : list){
           em.remove(u);   
        }
        em.getTransaction().commit();
    }catch(Exception ex){
        em.getTransaction().rollback();
    }   
}

public static synchronized boolean createUser(EntityManager em, String username, String password) {
        Query q = em.createQuery("SELECT u FROM User u");
        List<User> list = q.getResultList();
        if(list.size() > 0)
            return false;
        User u = new User();
        u.setPassword(password);
        u.setUsername(username);
        Try{
            em.getTransaction().begin();
            em.persist(u);
            em.getTransaction().commit();
            return true;
        }catch(Exception ex){
            em.getTransaction().rollback();
            return false;
       }
    }