0

请看一下这段代码。Persist 适用于名为 Candidate 的表,但不适用于另一个名为 VerifyLink 的表,而 hibernate 显示它正在持久化在日志文件中。请查看下面的日志,在第一种情况下它显示查询“插入候选”,但在第二种情况下,它不显示查询。怎么了 ?

protected final void doInTransaction(PersistenceActionWithoutResult action) throws EntityAccessorException {
    EntityManager em = emf.createEntityManager();
    try {
        int status = 0;
        status = userTransaction.getStatus();
        if (Status.STATUS_ACTIVE != status){
            count.incrementAndGet();
            userTransaction.begin();
        }
        action.execute(em);
        System.out.println(em);
        status = userTransaction.getStatus();
        System.out.println("status : " + status);
        if (Status.STATUS_ACTIVE == status) {
            if (0 == count.decrementAndGet()) {
                userTransaction.commit();
                System.out.println("committed : ");
            }
        }
    } catch (Exception e) {
        try {
            userTransaction.rollback();
            System.out.println("rolled back :");
            e.printStackTrace();
        } catch (Exception ex) {
            Logger.getLogger(AbstractEntityAccessor.class.getName()).log(Level.SEVERE, null, ex);
        }
        throw new EntityAccessorException(e);
    } finally {
        System.out.println("transaction close :");
        em.close();
    }
}

当我第一次提交某事时

[#|2011-07-25T12:38:40.020+0530|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=61;_ThreadName=Thread-1 ;|org.hibernate.ejb.EntityManagerImpl@15e3c52|#]

[#|2011-07-25T12:38:40.021+0530|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=61;_ThreadName=Thread-1 ;|状态:0|#]

[#|2011-07-25T12:38:40.093+0530|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=61;_ThreadName=Thread-1 ;|休眠:插入候选(comment、creation_date、currentCompany、current_ctc、cv、cv_name、dob、email_id、email_id_verified、expected_ctc、first_name、last_name、location、mobile、notice_period、password、sex、upadte_date、years_of_exp、cand_id)值( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|#]

[#|2011-07-25T12:38:40.164+0530|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=61;_ThreadName=Thread-1 ;|已提交:|#]

[#|2011-07-25T12:38:40.165+0530|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=61;_ThreadName=Thread-1 ;|交易结束:|#]

[#|2011-07-25T12:38:47.158+0530|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=61;_ThreadName=Thread-1 ;|org.hibernate.ejb.EntityManagerImpl@1fd91c4|#]

[#|2011-07-25T12:38:47.159+0530|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=61;_ThreadName=Thread-1 ;|状态:0|#]

[#|2011-07-25T12:38:47.159+0530|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=61;_ThreadName=Thread-1 ;|已提交:|#]

[#|2011-07-25T12:38:47.159+0530|INFO|glassfish3.0.1|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=61;_ThreadName=Thread-1 ;|交易结束:|#]

4

1 回答 1

0

我在 userTransaction.begin() 之后添加了 em.joinTransaction() ,如下所示,这似乎解决了问题。现在可以了。我得到了提示

if (Status.STATUS_ACTIVE != status){
                count.incrementAndGet();
                userTransaction.begin();
                em.joinTransaction();
            }
于 2011-07-25T17:49:26.403 回答