final Session session = HibernateUtil.getSession(); try { final Transaction transaction = session.beginTransaction(); try { // The real work is here transaction.commit(); } catch (Exception ex) { // Log the exception here transaction.rollback(); throw ex; } } finally { HibernatilUtil.closeSession(); }
public void create(T entity) { Session session=getSessionFactory().getCurrentSession(); Transaction trans=session.beginTransaction(); session.save(entity); trans.commit(); }
Session sess = factory.openSession(); Transaction tx; try { tx = sess.beginTransaction(); //do some work ... tx.commit(); } catch (Exception e) { if (tx!=null) tx.rollback(); throw e; } finally { sess.close(); }
Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); User userA = (User)session.get(User.class, 1101); transaction.commit(); session.close(); // Since session is closed, userA is detached. session = sessionFactory.openSession(); transaction = session.beginTransaction(); User userB = (User)session.get(User.class, 1101); //Now here, userB represents the same persistent row as userA. //When an attempt to reattach userA occurs, an exception is thrown session.update(userA); transaction.commit(); session.close();
Session session = factory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); ... tx.commit(); } catch (Exception e) { if (tx!=null) tx.rollback(); }finally { session.close(); }
Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); User userA = (User)session.get(User.class, 1101); transaction.commit(); session.close(); //userA is now detached as session is closed. session = sessionFactory.openSession(); transaction = session.beginTransaction(); User userB = (User)session.get(User.class, 1101); User userC = (User)session.merge(userA); if (userB == userC) { System.out.println("Reattched user is equal"); } transaction.commit(); session.close();
public void addUser(User user) { try{ Session session = sessionFactory.getCurrentSession(); Transaction trans = session.beginTransaction(); session.save(user); trans.commit(); }catch(Exception e){ trans.rollback(); } }
Session session = factory.getCurrentSession(); Transaction tx = session.beginTransaction(); Product p = new Product(); session.persist(p); session.flush(); System.out.println(session.isOpen()); // prints true p.setName("foo"); session.merge(p); tx.commit(); System.out.println(session.isOpen()); // prints false
try { Session session = factory.openSession(); Transaction tx = session.beginTransaction(); ... tx.commit(); session.close(); } catch (SomeException e) { tx.rollback(); ... }
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i<888888; i++ ) { TableA record = new TableA(); record.setXXXX(); session.save(record) if ( i % 50 == 0 ) { //50, same as the JDBC batch size //flush a batch of inserts and release memory: session.flush(); session.clear(); } } tx.commit(); session.close();
@Override public void close() { if (this.count.getAndDecrement() == 0) { try { switch (this.tx.getStatus()) { case Status.STATUS_ACTIVE: { if (this.active) { try { this.tx.commit(); break; } catch (RollbackException e) { throw new IllegalStateException(e); } catch (HeuristicMixedException | HeuristicRollbackException e) { throw new CacheException(e); } } // Otherwise fall through } case Status.STATUS_MARKED_ROLLBACK: { this.tx.rollback(); break; } } } catch (SystemException e) { throw new CacheException(e); } } }
Session s = sessionfactory.openSession(); Transaction tx = null; try { tx = s.beginTransaction(); // Your data manipulation here tx.commit(); } catch (Exception e) { if (tx != null) { tx.rollback();} } finally { s.close(); }
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i<100000; i++ ) { Customer customer = new Customer(.....); session.save(customer); if ( i % 20 == 0 ) { //20, same as the JDBC batch size //flush a batch of inserts and release memory: session.flush(); session.clear(); } } tx.commit(); session.close();
Session sess = factory.openSession(); Transaction tx = null; try { tx = sess.beginTransaction(); //do some work, point 3 tx.commit(); } catch (Exception e) { if (tx!=null) tx.rollback(); throw e; } finally { sess.close(); }
Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i<100000; i++ ) { Customer customer = new Customer(.....); session.save(customer); if ( i % 20 == 0 ) { //20, same as the JDBC batch size //flush a batch of inserts and release memory: session.flush(); session.clear(); } } tx.commit(); session.close();
Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); try { // do something transaction.commit(); } catch (Exception ex) { // on error, revert changes made on DB if (transaction != null) { transaction.rollback(); } } finally { session.close(); }