Hibernate(mysql)为同一查询返回不同的结果

时间:2014-08-01 17:59:00

标签: java mysql hibernate jdbc hql

我现在有点绝望,因为我现在坐在这个问题上大约2天了:/

我想查询用户列表,如下所示:

Query query = session.createQuery("FROM User as us WHERE us.allowedOnWebInterface = 1");
List webusers = query.list(); 
logger.info("Found " + webusers.size() + " users."); 
// i repeat the same query with jdbc via session.doWork and also log the result count
// do other stuff like load the webusers list into a var to have it available in the jsp

当我在本地启动我的webapp时,我总是显示正确的用户..当我将webapp的war文件部署到带有jetty和mysql数据库的ubuntu服务器时BUUT,然后应用程序首先向我显示正确的用户但是当我编辑或添加用户然后应用程序混淆。 如果我现在刷新用户列表页面,我有时会看到我应该看到的所有用户,但有时我只看到一个子集。当我编辑一些用户时,我也会得到旧结果。

看一下日志,我可以看到实际上同一个查询获得了不同的结果,只需刷新页面即可。 这是我的日志记录输出:

qtp569116871-17 2014-08-01 19:40:25,672 76978 INFO  query: FROM User as us WHERE us.allowedOnWebInterface = 1
qtp569116871-17 2014-08-01 19:40:25,684 76990 INFO  Found 6 users.
qtp569116871-17 2014-08-01 19:40:25,686 76992 INFO  with jdbc 5 results
qtp569116871-20 2014-08-01 19:40:33,419 84725 INFO  query: FROM User as us WHERE us.allowedOnWebInterface = 1
qtp569116871-20 2014-08-01 19:40:33,425 84731 INFO  Found 5 users.
qtp569116871-20 2014-08-01 19:40:33,434 84740 INFO  with jdbc 5 results
qtp569116871-17 2014-08-01 19:40:34,411 85717 INFO  query: FROM User as us WHERE us.allowedOnWebInterface = 1
qtp569116871-17 2014-08-01 19:40:34,420 85726 INFO  Found 5 users.
qtp569116871-17 2014-08-01 19:40:34,422 85728 INFO  with jdbc 6 results
qtp569116871-18 2014-08-01 19:40:35,114 86420 INFO  query: FROM User as us WHERE us.allowedOnWebInterface = 1

我还启用了mysql日志记录并检查了生成的mysql查询。即使返回不同的结果,它们也是一样的。 也许重要的是要注意我使用BoneCPConnectionProvider。

如果有人能帮我解决这个问题会很好。

更新:

原来我的问题确实是由BoneCP引起的。在我的hibernate.cfg.xml中注释掉所有的BoneCp之后,它最终没有来自数据库的奇怪的查询答案。

<!-- this is connection provider -->
<!--        <property name="connection.provider_class">com.jolbox.bonecp.provider.BoneCPConnectionProvider</property> -->

<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/mywebapp</property>
<property name="connection.username">hibernate</property>
<property name="connection.password">hibernate</property>

<!-- encoding -->
<property name="connection.CharSet">utf8</property>
<property name="connection.characterEncoding">utf8</property>
<property name="connection.useUnicode">true</property>

<!-- configure the bonecp conection provider -->
<!--        <property name="bonecp.partitionCount">3</property> -->
<!--        <property name="bonecp.maxConnectionsPerPartition">15</property> -->
<!--        <property name="bonecp.minConnectionsPerPartition">2</property> -->
<!--        <property name="bonecp.acquireIncrement">3</property> -->

1 个答案:

答案 0 :(得分:0)

这当然很奇怪,一定非常令人沮丧。我会尝试缩小问题的范围:

  1. 使用log4jdbc打开jdbc日志记录。看看应用程序端的查询/结果是否与您通过mysql日志看到的内容有任何不同。

  2. 由于只有6/5个用户,因此请编写一个额外的日志语句来迭代用户并打印其ID。是否始终是相同的用户?该用户记录的用途是什么?

  3. 也许您的缓存已经配置好了?关闭二级缓存:

    hibernate.cache.use_second_level_cache=false
    
  4. 另外,请确保您的查询未被缓存。

        query.setCacheable(false);
    
相关问题