有没有办法将两个查询作为Java实现的一部分来使用相同的数据集?
我正在开发 Java 服务(Hibernate),并且我正在按顺序调用计数查询和查询以获取相应的记录(本机查询)。在某些情况下,计数与查询检索数据所获取的实际记录不同。 我想确保两个查询都将使用相同的数据集。 对此有什么想法吗?
I am working on a Java Service (Hibernate) and I am calling sequentially a count query and a query to fetch the corresponding records (native queries). There are cases where the count is different than the actual records fetched by the query retrives the data.
I would like to secure that both queries are about to use the same dataset.
Any ideas on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想使用计数并不是一个好主意。
考虑记录上的主键代表什么...或者其他字段可能标识您需要的记录。
客户端上检索的数据集为您提供运行查询时数据库中的内容。
在事务尚未提交时锁定表或记录有一些危险的能力......但我不建议尝试它们。如果是关于多个服务/客户端或线程并行使用的 Db。我猜你有这样的系统,当你的查询运行时,计数会发生变化。
使用锁需要非常小心的处理,并且减慢和挂起其他线程非常危险
I guess it is quite not good idea to use counts.
think about what primary key on record stands for... or maybe other fields identify records you need.
Retrieved Dataset on client gives you what was in DB at time you ran your query.
There are some dangerous abilities to lock table or records while your transaction not commited yet... but I do not recommend to try them. if it is about Db used by multiple services/clients or threads in parallel. I guess you have such system as counts change while your queries run.
It needs very careful handling to use locks and really dangerous to slow and hang other threads