Session session = factory.getCurrentSession();//Use getCurrentSession rather than openSession Transaction trans = session.beginTransaction(); trans.begin(); Session session2 = factory.getCurrentSession();//Same session will be returned. System.out.println(session2.isConnected()); trans.commit();
Transaction tx = em.getTransaction(); tx.begin(); try { update1(); update2(); } catch(Exception e) { failed = true } finally { if( !failed ) tx.commit(); else tx.rollbacl(); }
public class HibernateUtil { public static void inTransaction(TransactionCallback tc) { Transaction transaction = HibernateUtil.getSessionFactory().getCurrentSession().getTransaction(); if (!HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().isActive()) { transaction.begin(); try { tc.doInTransaction(); transaction.commit(); } catch (Exception e) { transaction.rollback(); } } } }
public abstract class BaseClass { public Object executeTrans(Template template) { // PersistenceManager pm = ...; Transaction tx = pm.currentTransaction(); try { tx.begin(); template.doTransaction(); tx.commit(); } finally { if (tx.isActive()) { tx.rollback(); } } } }
PersistenceManager pm = PMF.getInstance().getPersistenceManager(); Transaction tx = pm.currentTransaction(); try { tx.begin(); pm.makePersistent(t); tx.commit(); } catch (Exception e) { LOGGER.severe("Exception by creating a new entity " + e.getMessage()); tx.rollback(); } finally { pm.close(); }
PersistenceManager pm = PMF.get().getPersistenceManager(); Transaction txn = pm.currentTransaction(); txn.begin(); Recipe r = pm.getObjectById(Recipe.class, recKey); try { r.setImage(newImage); txn.commit(); } finally { pm.close(); }
Transaction t1 = Spring.createTransaction(); t1.begin(); try { //your first service method marked as @Transactional userService.saveUser(userDto); t1.commit(); } catch (Throwable t) { t1.rollback(); } finally { t1.close(); } Transaction t2 = Spring.createTransaction(); t2.begin(); try { //your second service method marked as @Transactional roleService.saveRole(roleDto); t2.commit(); } catch (Throwable t) { t2.rollback(); } finally { t2.close(); }
// PersistenceManager pm = ...; Transaction tx = pm.currentTransaction(); User user = userService.currentUser(); List<Account> accounts = new ArrayList<Account>(); try { tx.begin(); Query query = pm.newQuery("select from Customer " + "where user == userParam " + "parameters User userParam"); List<Customer> customers = (List<Customer>) query.execute(user); query = pm.newQuery("select from Account " + "where parent-pk == keyParam " + "parameters Key keyParam"); for (Customer customer : customers) { accounts.addAll((List<Account>) query.execute(customer.key)); } } finally { if (tx.isActive()) { tx.rollback(); } }
Transaction tx = pm.currentTransaction(); try { tx.begin(); pm.setDetachAllOnCommit(true); Parent parent = null;