hibernate commit:在日志中显示已提交,但实际上没有
请看一下这段代码。 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 ;|Hibernate:插入候选者(comment、creation_date、currentCompany、current_ctc、cv、 cv_name、dob、email_id、email_id_verified、expected_ctc、first_name、last_name、位置、手机、notice_period、密码、性别、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 ;|交易关闭:|#]
Please have a look at this code. Persist works for a table called Candidate and it does not for another table called VerifyLink, whereas, hibernate shows that it is being persisted in the log file.
Please have a look at the log below, In first case it shows the query, "insert into Candidate", but in second case, it does not show the query. what is wrong ?
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();
}
}
When I commit something for the first time
[#|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;|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: insert into 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) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|#]
[#|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;|transaction close :|#]
[#|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;|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;|transaction close :|#]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 userTransaction.begin() 之后添加了 em.joinTransaction() ,如下所示,这似乎已经解决了问题。现在可以了。我得到了提示
I added em.joinTransaction() after userTransaction.begin() as shown below and this seems to have solved the problem. It works now. I got the hint from