【问题标题】:in hibernate database does not response在休眠数据库中没有响应
【发布时间】:2014-06-29 11:31:48
【问题描述】:

我有使用hibernate 访问数据库的Web 应用程序。 我在 DAO 类中有一个更新方法,当我运行这个方法时,我在控制台中没有错误但数据库没有响应,而且在运行该类之后我无法使用 sqlDeveloper 检索表。 这在运行类后显示在 Eclipse 控制台中:

May 12, 2014 11:29:57 AM org.hibernate.engine.internal.StatisticalLoggingSessionEventListener end
INFO: Session Metrics {
    13975 nanoseconds spent acquiring 1 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    40413377 nanoseconds spent preparing 1 JDBC statements;
    1130672 nanoseconds spent executing 1 JDBC statements;
    0 nanoseconds spent executing 0 JDBC batches;
    0 nanoseconds spent performing 0 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    0 nanoseconds spent performing 0 L2C misses;
    0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
    0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
}

这里是更新方法体:

session = hu.getSessionFactory().openSession();         
Query q= session.createQuery("update MessageQueue set   returnStatus=:returnStatus where id=:msg_id");
System.out.println("QQQQQQQQQQQQQQQQQQQQQQUERY ");
q.setParameter("msg_id", id);
System.out.println("1111111111111111111111111111 param "+id);
q.setParameter("returnStatus", returnStatus);
System.out.println("222222222222222222222 param "+returnStatus);
System.out.println(q.getQueryString());
int res=q.executeUpdate();
session.close();

数据库是oracle。

【问题讨论】:

  • 当您说“数据库没有响应”时,您到底是什么意思?应用程序是否挂起?更新似乎成功但没有在数据库内进行任何更改?当您说“无法检索表”时,您的意思是查询该表时出错还是您期望的数据不存在?
  • 我的意思是我没有从数据库中得到任何响应!我尝试更新的记录被我的用户锁定,我也无法使用 sql developer 等任何工具检索它们。

标签: java oracle hibernate jakarta-ee jdbc


【解决方案1】:

从您的 cmets 看来,数据没有被提交。尝试在更新之前创建一个事务并在之后提交它:

session = hu.getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();   // add this line
Query q= session.createQuery("update MessageQueue set   returnStatus=:returnStatus where id=:msg_id");
System.out.println("QQQQQQQQQQQQQQQQQQQQQQUERY ");
q.setParameter("msg_id", id);
System.out.println("1111111111111111111111111111 param "+id);
q.setParameter("returnStatus", returnStatus);
System.out.println("222222222222222222222 param "+returnStatus);
System.out.println(q.getQueryString());
int res=q.executeUpdate();
transaction.commit();                                   // add this line too
session.close();

如果抛出任何异常,在catch 块中回滚事务也是一个好主意。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 2023-04-09
    • 2013-11-07
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    相关资源
    最近更新 更多