简体   繁体   English

mysql视图在事务内部没有刷新(Hibernate / Spring)

[英]mysql view not refreshing inside a transaction ( Hibernate/Spring)

I am using Spring for DI and Hibernate for data access on a mySQL database. 我使用Spring for DI和Hibernate在mySQL数据库上进行数据访问。 I have code inside a transaction that inserts a record in a table and then executes a view that queries that table and performs some aggregate calculations. 我在一个事务中有代码,该事务在表中插入记录,然后执行查询该表并执行一些聚合计算的视图。 The problem I see is that the record I just inserted during the same transaction, is not included in the view's calculated values. 我看到的问题是我刚刚在同一个事务中插入的记录未包含在视图的计算值中。 I run the same view in mySQL workbench and the inserted value is included in the view. 我在mySQL工作台中运行相同的视图,插入的值包含在视图中。 Does anyone know what is causing this? 有谁知道是什么原因造成的?

Ultimately, I had to call entityManager.refresh(Object entity) to refresh the entity for the view record I wanted updated. 最终,我不得不调用entityManager.refresh(Object entity)来刷新我想要更新的视图记录的实体。 I think the problem resides in the fact that Hibernate cannot recognize that the view needs to be updated since it does not know that it is dependent (at the database level) upon the original entity that was updated. 我认为问题在于Hibernate无法识别视图需要更新的事实,因为它不知道它是否依赖于(在数据库级别)更新的原始实体。 I presume that Hibernate is caching the records from the view and does not know that they need to get updated, even after a flush(). 我认为Hibernate正在从视图中缓存记录,并且即使在flush()之后也不知道它们需要更新。

Hibernate sees the original table and the view as completely unrelated, when in reality the view is dependent upon the table and should be made "dirty" whenever the table changes. Hibernate认为原始表和视图完全不相关,而实际上视图依赖于表,并且应该在表更改时变为“脏”。 I don't know how to get Hibernate to recognize that. 我不知道如何让Hibernate认识到这一点。

Most probably you haven't flushed changes to the database yet. 很可能你还没有刷新数据库的更改。 Hibernate does not know that there is connection between table you inserted rows and view that you are reading later. Hibernate不知道您插入行的表与您稍后阅读的视图之间存在连接。 Flush entitymanager (or session or template) before querying view . 在查询视图之前刷新实体管理器(或会话或模板)。

This is most probably caused by the default isolation level of MySQL which is REPEATABLE READ . 这很可能是由MySQL的默认隔离级别引起的,它是REPEATABLE READ

This means that your transaction in MySQL Workbench won't see changes until you end that transaction. 这意味着在您结束该事务之前,MySQL Workbench中的事务将不会看到更改。 Running a SELECT counts as a transaction. 运行SELECT计为事务。

You should see the changes in MySQL Workbench once you issue a commit (or rollback) there. 一旦在那里发出提交(或回滚),您应该会看到MySQL Workbench中的更改。

You probably want to change either the default isolation level for your installation to READ COMMITTED or change the isolation level of your session in MySQL Workbench to READ COMMITTED 您可能希望将安装的默认隔离级别更改为READ COMMITTED或者将MySQL Workbench中的会话隔离级别更改为READ COMMITTED

Details on how you do that can be found in the manual. 有关如何执行此操作的详细信息,请参阅手册。

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

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