繁体   English   中英

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

[英]Hibernate (mysql) returns different results for same query

我现在有点绝望,因为我现在正坐在这个问题上大约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文件部署到带有码头和mysql数据库的ubuntu服务器上时,该应用程序会首先向我显示正确的用户,但是当我编辑或添加用户,则应用程序感到困惑。 如果现在刷新用户列表页面,我有时会看到所有我应该看到的用户,但有时我只会看到一个子集。 当我编辑一些用户时,我也会得到旧的结果。

查看日志,我可以看到实际上相同的查询获得不同的结果,只是通过刷新页面即可。 这是我的日志输出:

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. 使用log4jdbc打开jdbc日志记录。 查看应用程序方面的查询/结果是否与通过mysql日志看到的内容有所不同。

  2. 由于只有6/5个用户,因此请编写一条额外的日志语句,以遍历用户并打印其ID。 总是缺少相同的用户吗? 该用户记录如何处理?

  3. 也许您的缓存配置不正确? 关闭二级缓存:

     hibernate.cache.use_second_level_cache=false 

另外,请确保您的查询没有被缓存。

    query.setCacheable(false);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM