示例#1
0
  @Override
  public boolean storeUser(User u) throws Exception {
    dLog.info("Entering method storeUser | User ID: " + u.getId());
    boolean result = false;
    Session session = null;

    try {
      // ensure we were passed a valid object before attempting to write
      if (u.validate()) {
        session = getSession();
        Transaction tranx = session.beginTransaction();
        session.save(u);
        tranx.commit();
        result = tranx.wasCommitted();
        dLog.info("Was committed: " + result);
      }
    } catch (Exception e) {
      dLog.error("Exception in storeUser", e);
    } finally {
      // ensure that session is close regardless of the errors in try/catch
      if (session != null) {
        session.close();
      }
    }

    return result;
  }
示例#2
0
  @Override
  public void delete(Group group) throws TransactionFailException {
    Session session = null;
    Transaction tx = null;

    try {
      session = sessionFactory.openSession();
      tx = session.beginTransaction();

      session.delete(group);

      tx.commit();

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (!tx.wasCommitted()) {
        tx.rollback();

        session.close();

        throw new TransactionFailException();
      }

      session.close();
    }
  }
示例#3
0
  @Override
  public void save(Group group) throws TransactionFailException {
    Session session = null;
    Transaction tx = null;

    try {
      session = sessionFactory.openSession();
      tx = session.beginTransaction();

      if (group.getId() == null) {
        session.persist(group);
      } else {
        session.saveOrUpdate(group);
      }

      tx.commit();

    } catch (Exception e) {
      e.printStackTrace();
    } finally {

      if (!tx.wasCommitted()) {
        tx.rollback();

        session.close();
        throw new TransactionFailException();
      }

      session.close();
    }
  }
  public HashSet<Integer> getEstablishmentIds() {
    Session session = null;
    Transaction transaction = null;
    Criteria cr = null;

    HashSet<Integer> results = new HashSet<Integer>();
    List<Establishment> establishmentResults;

    try {
      session = sessionFactory.getCurrentSession();

      transaction = session.beginTransaction();

      cr = session.createCriteria(Establishment.class);

      establishmentResults = cr.list();

      for (Establishment establishment : establishmentResults) {
        results.add(establishment.getIdEstablishment());
      }

      if (!transaction.wasCommitted()) transaction.commit();
    } catch (Exception e) {
      System.out.println(e.getMessage());
      if (transaction != null) {
        transaction.rollback();
      }
      e.printStackTrace();

    } finally {
      if (session != null && session.isOpen()) session.close();
    }

    return results;
  }
示例#5
0
 public void afterTransactionCompletion(Transaction tx) {
   if (tx.wasCommitted()) {
     System.out.println("Creations: " + creates + ", Updates: " + updates + "Loads: " + loads);
   }
   updates = 0;
   creates = 0;
   loads = 0;
 }
 public static void commitTransaction() throws SQLException {
   Transaction tx = threadTransaction.get();
   try {
     if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) tx.commit();
     threadTransaction.set(null);
   } catch (HibernateException ex) {
     rollbackTransaction();
     throw new SQLException(ex);
   }
 }
示例#7
0
 @After
 @Override
 public void tearDown() {
   if (tx != null && !tx.wasCommitted()) {
     tx.commit();
   }
   if (session != null && session.isOpen()) {
     session.close();
   }
 }
示例#8
0
 public static void rollbackTransaction() {
   Transaction tx = threadTransaction.get();
   threadTransaction.set(null);
   try {
     if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
       tx.rollback();
     }
   } finally {
     closeSession();
   }
 }
示例#9
0
 /** Rollback the database transaction. */
 public static void rollbackTransaction() {
   Transaction tx = (Transaction) threadTransaction.get();
   try {
     threadTransaction.set(null);
     if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
       log.debug("Tyring to rollback database transaction of this thread.");
       tx.rollback();
     }
   } finally {
     closeSession();
   }
 }
示例#10
0
 /** Commit the database transaction. */
 public static void commitTransaction() {
   Transaction tx = (Transaction) threadTransaction.get();
   try {
     if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
       log.debug("Committing database transaction of this thread.");
       tx.commit();
     }
     threadTransaction.set(null);
   } catch (HibernateException e) {
     rollbackTransaction();
     throw e;
   }
 }
示例#11
0
 /**
  * Rollbacks the currently opened transaction.
  *
  * @throws PersistencyException
  */
 public static void rollbackTransaction() throws PersistencyException {
   threadRollback.set("");
   Transaction tx = (Transaction) threadTransaction.get();
   threadTransaction.set(null);
   try {
     if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
       tx.rollback();
     }
   } catch (HibernateException e) {
     throw new PersistencyException(e, PersistencyException.FATAL);
   } finally {
     closeSession();
   }
 }
  public static void rollbackTransaction() throws SQLException {

    Transaction tx = threadTransaction.get();
    try {
      threadTransaction.set(null);
      if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
        tx.rollback();
      }
    } catch (HibernateException ex) {
      throw new SQLException(ex);
    } finally {
      closeSession();
    }
  }
示例#13
0
 public void addAlbumGenre(Genre genre, Album album) throws Exception {
   Transaction trans = session.beginTransaction();
   try {
     GenreAlbum newGenreAlbum = new GenreAlbum(genre, album);
     session.save(newGenreAlbum);
     if (!trans.wasCommitted()) {
       trans.commit();
     }
     session.flush();
   } catch (Exception e) {
     if (trans != null) {
       trans.rollback();
     }
     throw e;
   }
 }
 public boolean delete(Event event) {
   session = HibernateUtil.getSessionFactory().openSession();
   try {
     transaction = session.beginTransaction();
     try {
       session.delete(event);
       if (!transaction.wasCommitted()) {
         transaction.commit();
       }
       return true;
     } catch (Exception e) {
       transaction.rollback();
       throw e;
     }
   } finally {
     session.close();
   }
 }
  public List<DefaultDictionaryRepresntation> getEstablishmentsDictionary() {
    Session session = null;
    Transaction transaction = null;
    Criteria cr = null;

    List<Establishment> establishmentResults = null;
    ArrayList<DefaultDictionaryRepresntation> establishmentDictionaryResults =
        new ArrayList<DefaultDictionaryRepresntation>();

    try {
      ApplicationContext context = ApplicationContextProvider.getApplicationContext();
      session = sessionFactory.getCurrentSession();

      transaction = session.beginTransaction();

      cr = session.createCriteria(Establishment.class);

      establishmentResults = cr.list();

      DefaultDictionaryRepresntation temp;

      for (Establishment result : establishmentResults) {
        temp = (DefaultDictionaryRepresntation) context.getBean("defaultDictionaryRepresntation");
        temp.setId(result.getIdEstablishment());
        temp.setName(result.getName());
        establishmentDictionaryResults.add(temp);
      }

      if (!transaction.wasCommitted()) transaction.commit();
    } catch (Exception e) {
      System.out.println(e.getMessage());
      if (transaction != null) {
        transaction.rollback();
      }
      e.printStackTrace();

    } finally {
      if (session != null && session.isOpen()) session.close();
    }

    return establishmentDictionaryResults;
  }
 public Event update(Event event) {
   session = HibernateUtil.getSessionFactory().openSession();
   try {
     transaction = session.beginTransaction();
     try {
       session.update(event);
       if (!transaction.wasCommitted()) {
         transaction.commit();
       }
       return event;
     } catch (HibernateException e) {
       e.printStackTrace();
       transaction.rollback();
     }
   } catch (Exception e) {
     transaction.rollback();
   } finally {
     session.close();
   }
   return null;
 }
  private String handleStatus(HttpServletRequest request) {
    String statusString = request.getParameter(Rest.REQ_PARAM_STATUS);
    int gameId = getGameId(request);

    log.info(String.format("Received %s message from game: %s", statusString, gameId));

    Session session = HibernateUtil.getSession();
    Transaction transaction = session.beginTransaction();
    try {
      Query query = session.createQuery(Constants.HQL.GET_GAME_BY_ID);
      query.setInteger("gameId", gameId);
      Game game = (Game) query.uniqueResult();

      if (game == null) {
        log.warn(
            String.format(
                "Trying to set status %s on non-existing " + "game : %s", statusString, gameId));
        return "error";
      }

      new GameHandler(game).handleStatus(session, statusString);

      transaction.commit();
    } catch (Exception e) {
      transaction.rollback();
      e.printStackTrace();
      return "error";
    } finally {
      session.close();
    }

    String gameLength = request.getParameter(Rest.REQ_PARAM_GAMELENGTH);
    if (gameLength != null && transaction.wasCommitted()) {
      log.info(String.format("Received gamelength %s for game %s", gameLength, gameId));
      MemStore.addGameLength(gameId, gameLength);
    }

    return "success";
  }
 public boolean wasCommitted() throws HibernateException {
   return realTxn.wasCommitted();
 }
示例#19
0
 /** Rollback transaction */
 public static void rollback(Transaction tx) {
   if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
     tx.rollback();
   }
 }