Beispiel für Hibernate-Transaktion

Beispiel für ein Transaktionshandle im Ruhezustand

In Hibernate ist die Transaktionsverwaltung Standard. Denken Sie jedoch daran, dass alle von Hibernate ausgelösten AusnahmenFATALind. Sie müssen die Transaktion zurücksetzen und die aktuelle Sitzung sofort schließen.

Hier ist eine Transaktionsvorlage für den Ruhezustand:

        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();
            }
        }