我如何才能提交 Play! JPA 手动事务?
通常,玩! 在请求成功完成后提交事务。 在 Play 中手动提交交易的正确方法是什么?
void addPerson() {
Person p = new Person("John", "Doe");
p.save();
// TODO - commit the transaction
// Now p should have an ID
assert p.id != null;
usePersonIdForSomethingNasty(p.id);
}
Usually, Play! commits the transaction after a request completes successfully.
What is the correct way to commit a transaction manually in Play?
void addPerson() {
Person p = new Person("John", "Doe");
p.save();
// TODO - commit the transaction
// Now p should have an ID
assert p.id != null;
usePersonIdForSomethingNasty(p.id);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过调用 JPA.em() 来获取 Hibernate EntityManager。然后,您可以从那里访问事务 (JPA.em().getTransaction())。
如果您打算自己管理事务,则需要禁用 Play! 的事务处理(您可以在方法或控制器上使用 @NoTransaction 注释来执行此操作)。否则,玩吧!无论如何,都会尝试在请求结束时提交事务,如果您自己已经这样做了,那将导致异常。
You can get the Hibernate EntityManager by calling JPA.em(). Then, from there, you have access to the transaction (JPA.em().getTransaction()).
If you intend to manage the transaction yourself, you will want to disable Play!'s transaction handling (there is a @NoTransaction annotation you can use on the method or controller to do that). Otherwise, Play! will try to commit the transaction at the end of the request anyway, and if you have already done that yourself, that will cause an exception.
你不需要做任何事情。请求完成且没有任何异常后,将为您提交交易。
只需确保在事务结束时对要保留的所有实体调用“保存”即可。
You don't need to do anything. After the request finishes without any exception, the transaction will be commited for you.
Just ensure to call "save" on all entities you want to persist at the end of transaction.