hibernate commit:显示在日志中提交,但实际上并没有

时间:2011-07-25 07:30:47

标签: mysql hibernate jpa transactions hibernate-entitymanager

请查看此代码。 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 =线程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; | status: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; | Hibernate:插入Candidate(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; | committed:|#]

[#| 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 =线程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; | status: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; | committed:|#]

[#| 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; |事务关闭:|#]

1 个答案:

答案 0 :(得分:0)

我在userTransaction.begin()之后添加了em.joinTransaction(),如下所示,这似乎解决了这个问题。它现在有效。我从

得到了提示
if (Status.STATUS_ACTIVE != status){
                count.incrementAndGet();
                userTransaction.begin();
                em.joinTransaction();
            }