org.hibernate.Transaction.rollback()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(9.0k)|赞(0)|评价(0)|浏览(152)

本文整理了Java中org.hibernate.Transaction.rollback()方法的一些代码示例,展示了Transaction.rollback()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Transaction.rollback()方法的具体详情如下:
包路径:org.hibernate.Transaction
类名称:Transaction
方法名:rollback

Transaction.rollback介绍

[英]Rollback this transaction. Either rolls back the underlying transaction or ensures it cannot later commit (depending on the actual underlying strategy).
[中]回滚此事务。回滚基础事务或确保它以后不能提交(取决于实际的基础策略)。

代码示例

代码示例来源:origin: iluwatar/java-design-patterns

@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;
 }
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

public static void finalizeTransaction(Transaction transaction, boolean isError) {
  if (isError) {
    try {
      transaction.rollback();
    } catch (Exception e) {
      LOG.error("Rolling back caused exception. Logging and continuing.", e);
    }
  } else {
    transaction.commit();
  }
}

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: dropwizard/dropwizard

/**
 * 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);
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@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;
    }
  }
}

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: dropwizard/dropwizard

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

代码示例来源:origin: hibernate/hibernate-orm

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[]

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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;

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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[]

代码示例来源:origin: hibernate/hibernate-orm

@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;
    }
  }
}

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

@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;
    }
  }
}

代码示例来源:origin: hibernate/hibernate-orm

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

代码示例来源:origin: hibernate/hibernate-orm

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

相关文章