Refine search
@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; } }
private void retrieveAndCompare(Map<Integer, Boolean> dbexpected, Criterion spatialCriterion) { Session session = null; Transaction tx = null; try { session = openSession(); tx = session.beginTransaction(); Criteria criteria = session.createCriteria( JtsGeomEntity.class ); criteria.add( spatialCriterion ); compare( dbexpected, criteria.list() ); } finally { if ( tx != null ) { tx.rollback(); } if ( session != null ) { session.close(); } } }
@Test public void testTemporalKeyMap() throws Exception { Session s = openSession(); Transaction tx = s.beginTransaction(); Atmosphere atm = new Atmosphere(); atm.colorPerDate.put( new Date(1234567000), "red" ); s.persist( atm ); s.flush(); s.clear(); atm = (Atmosphere) s.get( Atmosphere.class, atm.id ); assertEquals( 1, atm.colorPerDate.size() ); final Date date = atm.colorPerDate.keySet().iterator().next(); final long diff = new Date( 1234567000 ).getTime() - date.getTime(); assertTrue( "24h diff max", diff >= 0 && diff < 24*60*60*1000 ); tx.rollback(); s.close(); }
/** * 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); } }
@Override protected void prepareTest() throws Exception { try (Session session = openSession()) { session.getTransaction().begin(); try { session.save( new MyEntity( 1L, "entity_1", new BigInteger( "3" ) ) ); session.save( new MyEntity( 2L, "entity_2", new BigInteger( "6" ) ) ); session.getTransaction().commit(); } catch (Exception e) { if ( session.getTransaction().isActive() ) { session.getTransaction().rollback(); } throw e; } } }
@Test public void testGenericGenerators() throws Exception { // Ensures that GenericGenerator annotations wrapped inside a GenericGenerators holder are bound correctly Session s = openSession(); Transaction tx = s.beginTransaction(); Monkey monkey = new Monkey(); s.persist( monkey ); s.flush(); assertNotNull( monkey.getId() ); tx.rollback(); s.close(); }
@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(); }); } }
private void doInSession(String tenant, Consumer<Session> function) { Session session = null; Transaction txn = null; try { session = sessionFactory .withOptions() .tenantIdentifier( tenant ) .openSession(); txn = session.getTransaction(); txn.begin(); function.accept(session); txn.commit(); } catch (Throwable e) { if ( txn != null ) txn.rollback(); throw e; } finally { if (session != null) { session.close(); } } } //end::multitenacy-hibernate-session-example[]
private <T> void doInSession(String hql, Map<Integer, T> result, Map<String, Object> params) { Session session = null; Transaction tx = null; try { session = openSession(); tx = session.beginTransaction(); Query query = session.createQuery( hql ); setParameters( params, query ); addQueryResults( result, query ); } finally { if ( tx != null ) { tx.rollback(); } if ( session != null ) { session.close(); } } }
public Object execute(boolean isSingleResult) throws Exception{ Session s = openSession(); Transaction t = s.beginTransaction(); Object result = null; try { result = getResults( s, isSingleResult ); t.commit(); } catch ( Exception ex ) { t.rollback(); throw ex; } finally { s.close(); } return result; } protected abstract Object getResults(Session s, boolean isSingleResult) throws Exception;
public final Serializable doInsideTransaction(Session s, java.util.function.Supplier<Serializable> sp) { s.getTransaction().begin(); try { final Serializable result = sp.get(); s.getTransaction().commit(); return result; } catch (Exception e) { if ( s.getTransaction().getStatus() == TransactionStatus.ACTIVE ) { s.getTransaction().rollback(); } throw e; } } }
@Test public void testLowAllocationSize() throws Exception { Session s; Transaction tx; s = openSession(); tx = s.beginTransaction(); int size = 4; BreakDance[] bds = new BreakDance[size]; for ( int i = 0; i < size; i++ ) { bds[i] = new BreakDance(); s.persist( bds[i] ); } s.flush(); for ( int i = 0; i < size; i++ ) { assertEquals( i + 1, bds[i].id.intValue() ); } tx.rollback(); s.close(); }
private void retrieveNullGeometry() { Transaction tx = null; Session session = null; try { session = openSession(); tx = session.beginTransaction(); Criteria criteria = session.createCriteria( GeomEntity.class ); List<GeomEntity> retrieved = criteria.list(); assertEquals( "Expected exactly one result", 1, retrieved.size() ); GeomEntity entity = retrieved.get( 0 ); assertNull( entity.getGeom() ); tx.commit(); } catch (Exception e) { if ( tx != null ) { tx.rollback(); } throw new RuntimeException( e ); } finally { if ( session != null ) { session.close(); } } } }
private void doInSession(String tenant, Consumer<Session> function, boolean useTenantTimeZone) { Session session = null; Transaction txn = null; try { SessionBuilder sessionBuilder = sessionFactory .withOptions() .tenantIdentifier( tenant ); if ( useTenantTimeZone ) { sessionBuilder.jdbcTimeZone( timeZoneTenantMap.get( tenant ) ); } session = sessionBuilder.openSession(); txn = session.getTransaction(); txn.begin(); function.accept( session ); txn.commit(); } catch (Throwable e) { if ( txn != null ) { txn.rollback(); } throw e; } finally { if ( session != null ) { session.close(); } } } //end::multitenacy-hibernate-timezone-configuration-session-example[]
@Test(expected = IllegalStateException.class) public void anIllegalStateExceptionShouldBeThrownWhenBeginTxIsCalledWithAnAlreadyActiveTX() throws Exception { try (Session s = openSession()) { tm.begin(); Transaction tx = null; try { // A call to begin() with an active Tx should cause an IllegalStateException tx = s.beginTransaction(); } catch (Exception e) { if ( tx != null && tx.isActive() ) { tx.rollback(); } throw e; } } catch (Exception e) { if ( tm.getStatus() == Status.STATUS_ACTIVE ) { tm.rollback(); } throw e; } } }
private void cleanUpTest(String pckg) { Session session = null; Transaction tx = null; try { session = openSession(); tx = session.beginTransaction(); String hql = String.format( "delete from %s", entityName(pckg) ); Query q = session.createQuery( hql ); q.executeUpdate(); tx.commit(); } catch (Exception e) { if ( tx != null ) { tx.rollback(); } } finally { if ( session != null ) { session.close(); } } }
@Override protected void prepareTest() throws Exception { try (Session session = openSession()) { session.getTransaction().begin(); try { session.save( new MyEntity( 1L, "entity_1" ) ); session.save( new MyEntity( 2L, "entity_2" ) ); session.getTransaction().commit(); } catch (Exception e) { if ( session.getTransaction().isActive() ) { session.getTransaction().rollback(); } throw e; } } }
@Test public void testEnumKeyType() throws Exception { Session s = openSession(); Transaction tx = s.beginTransaction(); Atmosphere atm = new Atmosphere(); atm.colorPerLevel.put( Atmosphere.Level.HIGH, "red" ); s.persist( atm ); s.flush(); s.clear(); atm = (Atmosphere) s.get( Atmosphere.class, atm.id ); assertEquals( 1, atm.colorPerLevel.size() ); assertEquals( "red", atm.colorPerLevel.get(Atmosphere.Level.HIGH) ); tx.rollback(); s.close(); }
Transaction tx; s = openSession(); tx = s.beginTransaction(); Sky sky = new Sky(); sky.id = Long.valueOf( 2 ); s.flush(); s.save( sameSky ); tx.commit(); fail( "unique constraints not respected" ); tx.rollback();