Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. public <R> R doInTransaction(final Function<EntityManager, R> whatToDo) {
  2. final EntityManager em = emf.createEntityManager();
  3. final R result;
  4. try {
  5. try {
  6. em.getTransaction().begin();
  7. result = whatToDo.apply(em);
  8. em.getTransaction().commit();
  9. } finally {
  10. if (em.getTransaction().isActive()) {
  11. em.getTransaction().rollback();
  12. }
  13. }
  14. } finally {
  15. em.close();
  16. }
  17. return result;
  18. }
  19.  
  20. em.unwrap(Session.class).doWork(connection -> {
  21. // stored procedures are called here directly over JDBC
  22. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement