英文:
Schedule Hibernate to save at specific times, and not sync until told to
问题
由于我无法控制代码库的限制,重要的是 SQL 数据只在与其他所有内容同时保存,这样即使发生崩溃,所有数据仍然在保存时保持同步,不会出现不一致的情况。
我想能够使用 Hibernate 或其他 ORM,但我担心无法控制何时将数据保存到数据库,可能会导致不一致情况。
是否有一种方法来控制这个?在每个线程上存储一个会话并在计划保存时调用 save() 是否会起作用?我读过一些帖子说,在数据库中具有表示的对象在修改时会被保存。
谢谢。
英文:
Due to codebase limitations out of my control, it's important that SQL data is only saved at the same time as everything else, so that even if a crash was to occur, all data would still be in sync with each other at the time it was saved, and no inconsistencies would arise.
I'd like to be able to use Hibernate or another ORM, but I'm concerned that I won't be able to control when data is saved to the DB, and that inconsistencies may arise.
Is there a way to control this? Would storing a session per each thread and calling save() on all when a save is scheduled work for this? I've read some posts that say Objects that have a representation in the DB are saved when modified.
Thank you.
答案1
得分: 0
我需要的是一个Stateless Session,这允许您修改对象,而不会自动保存更改,而不需要干扰flush模式。
此外,Hibernate只会在特定会话处于打开状态时自动跟踪更改,而这在任何情况下都不应该在长时间内使用。关闭会话将导致对象变为已附加状态,并且可以使用新会话定期保存它。
希望这对将来的某人有所帮助!
英文:
What I needed was a Stateless Session this allows you to modify objects without changes being automatically saved without messing with flush modev
Plus, hibernate only automatically tracks changes while the specific session is open, which should never be used over any period of time anyway. Closing the session will cause the object to become attached, and a new session can be used to save it periodically.
Hope this helps someone in future!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论