Hibernateトランザクションハンドルの例
Hibernateでは、トランザクション管理は非常に標準的です。Hibernateによってスローされる例外はFATALであることに注意してください。トランザクションをロールバックして、現在のセッションをすぐに閉じる必要があります。
Hibernateトランザクションテンプレートは次のとおりです。
Session session = null; Transaction tx = null; try{ session = HibernateUtil.getSessionFactory().openSession(); tx = session.beginTransaction(); tx.setTimeout(5); //doSomething(session); tx.commit(); }catch(RuntimeException e){ try{ tx.rollback(); }catch(RuntimeException rbe){ log.error("Couldn’t roll back transaction", rbe); } throw e; }finally{ if(session!=null){ session.close(); } }