@Override public Spell findByName(String name) { Transaction tx = null; Spell result = null; try (Session session = getSessionFactory().openSession()) { tx = session.beginTransaction(); Criteria criteria = session.createCriteria(persistentClass); criteria.add(Restrictions.eq("name", name)); result = (Spell) criteria.uniqueResult(); tx.commit(); } catch (Exception e) { if (tx != null) { tx.rollback(); } throw e; } return result; } }
public void create(T entity) { Session session=getSessionFactory().getCurrentSession(); Transaction trans=session.beginTransaction(); session.save(entity); trans.commit(); }
/** * Performs a call in a transaction * * @param call the call * @param <T> the type of the returned result * @return the result of the call */ public <T> T inTransaction(Callable<T> call) { final Session session = sessionFactory.getCurrentSession(); final Transaction transaction = session.beginTransaction(); try { final T result = call.call(); transaction.commit(); return result; } catch (final Exception e) { transaction.rollback(); if (e instanceof RuntimeException) { throw (RuntimeException) e; } throw new RuntimeException(e); } }
public Session getThreadSession(boolean create) { Session session = threadSessions.get(); if(session == null && create) { if(logger.isDebugEnabled()) { logger.debug("Creating thread-local session for {}", Thread.currentThread()); } session = sessionFactory.openSession(); session.beginTransaction(); threadSessions.set(session); } return session; }
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(); 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 newSession = (entityInterceptor != null ? obtainSessionFactory().withOptions().interceptor(entityInterceptor).openSession() : obtainSessionFactory().openSession()); if (logger.isDebugEnabled()) { logger.debug("Opened new Session [" + newSession + "] for Hibernate transaction"); hibTx = session.beginTransaction(); try { if (session != null && session.getTransaction().getStatus() == TransactionStatus.ACTIVE) { session.getTransaction().rollback();
//this is a update method **public boolean updateImage(Image image) { Session session = template.getSessionFactory().openSession(); Transaction transaction = session.beginTransaction(); session.saveOrUpdate(image); session.flush(); transaction.commit(); session.close(); return true; }**
@Before public void setup(){ factory = mock(SessionFactory.class); session = mock(Session.class); when(factory.openSession()).thenReturn(session); when(session.beginTransaction()).thenReturn(mock(Transaction.class)); when(session.createQuery(Mockito.anyString())).thenReturn(mock(Query.class)); }
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(); }
@Override protected Result check() throws Exception { return timeBoundHealthCheck.check(() -> { try (Session session = sessionFactory.openSession()) { final Transaction txn = session.beginTransaction(); try { session.createNativeQuery(validationQuery).list(); txn.commit(); } catch (Exception e) { if (txn.getStatus().canRollback()) { txn.rollback(); } throw e; } } return Result.healthy(); }); } }
public Session getThreadSession(boolean create) { Session session = threadSessions.get(); if(session == null && create) { if(logger.isDebugEnabled()) { logger.debug("Creating thread-local session for {}", Thread.currentThread()); } session = sessionFactory.openSession(); session.beginTransaction(); threadSessions.set(session); } return session; }
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();
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();
Session newSession = (entityInterceptor != null ? obtainSessionFactory().withOptions().interceptor(entityInterceptor).openSession() : obtainSessionFactory().openSession()); if (logger.isDebugEnabled()) { logger.debug("Opened new Session [" + newSession + "] for Hibernate transaction"); hibTx = session.beginTransaction(); try { if (session != null && session.getTransaction().getStatus() == TransactionStatus.ACTIVE) { session.getTransaction().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
@Test public void testSimpleCaseStatementFixture() { Session s = openSession(); Transaction t = s.beginTransaction(); s.createQuery( "select case p.name when 'Steve' then 'x' else 'y' end from Person p" ) .list(); t.commit(); 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; try { tx = sess.beginTransaction(); //do some work ... tx.commit(); } catch (Exception e) { if (tx!=null) tx.rollback(); throw e; } finally { sess.close(); }
Transaction txn = null; try { session = factorySupplier.get().openSession(); function.beforeTransactionCompletion(); txn = session.beginTransaction(); txn.commit(); txn.rollback(); if ( txn != null && txn.isActive() ) { try { txn.rollback(); function.afterTransactionCompletion(); if ( session != null ) { session.close();