Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

java - How can I commit a Play! JPA transaction manually?

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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...