there is something need to be careful about Hibernate XA transaction.
- UserTransaction utx = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
- Session session1 = null;
- Session session2 = null;
- try {
- utx.begin(); // !!! must begin transaction before open session. order is important
- session1 = auctionDatabase.openSession();
- session2 = billingDatabase.openSession();
- concludeAuction(session1);
- billAuction(session2);
- session1.flush(); // !!! must flush, otherwise can not persistent it
- session2.flush();
- utx.commit();
- } catch (RuntimeException ex) {
- try {
- utx.rollback();
- } catch (RuntimeException rbEx) {
- log.error("Couldn't roll back transaction", rbEx);
- }
- throw ex;
- } finally {
- session1.close();
- session2.close();
- }