简体   繁体   English

默认@Transactional 如何在低级别工作?

[英]How does default @Transactional work in the low level?

Does a propagation-Required default @Transactional collects all queries and executes them at the end of the method altogether or does it open a db transaction and executes BEGIN, every query as it finds it and when transaction finishes executes COMMIT?需要传播的默认@Transactional 是否收集所有查询并在方法结束时完全执行它们,或者它是否打开数据库事务并执行 BEGIN,每个查询在它找到时以及事务完成时执行 COMMIT?

Is this what is referred as Logical vs Physical transactions?这就是所谓的逻辑与物理事务吗?

I am wondering that because I am using a @Transactional tests that executes GET endpoint + DELETE endpoont + GET endpoint with READ_UNCOMMITED, behavior manages to work well, but I see no trace of delete queries in the logs, only selects.我想知道,因为我正在使用执行 GET 端点 + DELETE 端点 + 带有 READ_UNCOMMITED 的 GET 端点的 @Transactional 测试,行为设法运行良好,但我在日志中看不到删除查询的痕迹,只有选择。

I would have expected I see all the queries issued and then a rollback, but I have the feeling that the transaction is just modifying the managed entities of the persistance context and just tries to save by the end of the test...我原以为我会看到所有发出的查询,然后回滚,但我感觉事务只是修改持久性上下文的托管实体,并试图在测试结束时保存...

If I should be seeing all the delete queries as the repository.removes() are executed then it might be that for some reason hibernate is only logging queries out of a readonly=false transaction如果我应该在执行 repository.removes() 时看到所有删除查询,那么可能是由于某种原因,hibernate 仅从 readonly=false 事务中记录查询

Maybe this answer helps you: JPA flush vs commit也许这个答案对你有帮助: JPA 刷新 vs 提交

If there is an active transaction, JPA/Hibernate will execute the flush method when transaction is committed.如果有活动事务,JPA/Hibernate 会在事务提交时执行flush 方法。 Meanwhile, all changes applied to entities are collected in the Unit of Work .同时,应用于实体的所有更改都收集在工作单元中

In flush() the changes to the data are reflected in database after encountering flush, but it is still in transaction.flush() MUST be enclosed in a transaction context and you don't have to do it explicitly unless needed (in rare cases), when EntityTransaction.commit() does that for you.在flush() 中,在遇到flush 后,对数据的更改会反映在数据库中,但它仍在事务中。flush() 必须包含在事务上下文中,除非需要,否则您不必显式执行此操作(在极少数情况下) ),当 EntityTransaction.commit() 为您执行此操作时。

You can change this behavior changing the flush strategy.您可以更改此行为以更改刷新策略。

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

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