public static void update(Long idGroup, List<SqlRow> gpms) {
    Group group = Group.findById(idGroup);
    if (group == null) return;

    Ebean.beginTransaction();
    try {
      deleteForGroup(idGroup);

      Integer i = 1;
      for (SqlRow man : gpms) {
        Profile prof = Profile.lastProfileByGpmId(man.getLong("gpm"));
        if (prof == null) continue;

        CacheClassifier cc =
            new CacheClassifier(
                group,
                i++,
                prof.gpm.idGpm,
                prof.name,
                prof.image,
                (prof.gender == null) ? null : prof.gender.value,
                (prof.relationshipStatus == null) ? null : prof.relationshipStatus.status,
                prof.nFollowers);
        if (cc.name == null || cc.name.isEmpty())
          cc.name = Profile.getLastNotEmptyField(prof.gpm.id, "name");
        cc.save();
      }

      Ebean.commitTransaction();
    } finally {
      Ebean.endTransaction();
    }
  }
 /**
  * Mark the specified action as completed
  *
  * @param transactionId the unique transaction id for this action
  * @param scheduledActionUuid the unique name of an action
  */
 private void markAsCompleted(String transactionId, String scheduledActionUuid) {
   try {
     Ebean.beginTransaction();
     SchedulerState schedulerState =
         SchedulerState.getRunningSchedulerStateFromTransactionId(transactionId);
     if (schedulerState == null) {
       log.error(
           String.format(
               "Strange ... No running scheduled action for %s with transaction id %s while one was running and mark as completed is requested",
               scheduledActionUuid, transactionId));
     } else {
       schedulerState.isRunning = false;
       schedulerState.save();
       if (log.isDebugEnabled()) {
         log.debug(
             String.format(
                 "Scheduled action for %s with transaction id %s completed",
                 scheduledActionUuid, transactionId));
       }
     }
     Ebean.commitTransaction();
   } catch (Exception e) {
     log.error("Failed to mark as complete", e);
     rollbackTransactionSilent();
   } finally {
     endTransactionSilent();
   }
 }
  @Override
  public void deleteCarouselImage(Long id) throws NotFoundException {
    // We use explicit transactions, since we are going to execute more than
    // 1 query in the same transaction
    Ebean.beginTransaction();
    try {
      // We see if the carouselImage really exists
      if (finder.byId(id) == null)
        throw new NotFoundException("La imagen no existe asi que no se puede borrar");

      // We delete it
      finder.byId(id).delete();

      // Now we need to reorder all the carouselImages
      for (CarouselImage ci : finder.orderBy("id").findList()) {
        if (ci.getId() > id) {
          // We insert a copy of the ci in the previous position
          CarouselImage ciCopy = (CarouselImage) ci._ebean_createCopy();
          ciCopy.setId(ci.getId() - 1);
          ciCopy.save();
          // Then we delete the ci
          ci.delete();
        }
      }

      Ebean.commitTransaction();

    } finally {
      Ebean.endTransaction();
    }
  }
  @Override
  public void addCarouselImage(CarouselImage carouselImage) {
    // We use explicit transactions, since we are going to execute more than
    // 1 query in the same transaction
    Ebean.beginTransaction();
    try {
      // We set atomatically the caroueselImage id, in order to be the
      // first one
      carouselImage.setId((long) 1);

      List<CarouselImage> carouselImages = finder.orderBy().desc("id").findList();

      for (CarouselImage ci : carouselImages) {
        // We insert a copy of the ci in the next position
        CarouselImage ciCopy = (CarouselImage) ci._ebean_createCopy();
        ciCopy.setId(ci.getId() + 1);
        ciCopy.save();
        // Then we delete the ci
        ci.delete();
      }

      // Then we save
      carouselImage.save();
      Ebean.commitTransaction();

    } finally {
      Ebean.endTransaction();
    }
  }
  @Test
  public void testGetForValidId() throws IOException {
    final UUID uuid = UUID.randomUUID();
    Assert.assertFalse(exprRepo.get(uuid, Organization.DEFAULT).isPresent());
    final models.ebean.Expression ebeanExpression = TestBeanFactory.createEbeanExpression();
    ebeanExpression.setUuid(uuid);
    ebeanExpression.setOrganization(
        models.ebean.Organization.findByOrganization(Organization.DEFAULT));
    try (Transaction transaction = Ebean.beginTransaction()) {
      Ebean.save(ebeanExpression);
      transaction.commit();
    }

    final Optional<Expression> expected = exprRepo.get(uuid, Organization.DEFAULT);
    Assert.assertTrue(expected.isPresent());
    Assert.assertTrue(isExpressionEbeanEquivalent(expected.get(), ebeanExpression));
  }
 @Test
 public void testGetExpressionCountWithMultipleExpr() throws IOException {
   Assert.assertEquals(0, exprRepo.getExpressionCount(Organization.DEFAULT));
   try (Transaction transaction = Ebean.beginTransaction()) {
     final models.ebean.Expression ebeanExpression1 = TestBeanFactory.createEbeanExpression();
     ebeanExpression1.setUuid(UUID.randomUUID());
     ebeanExpression1.setOrganization(
         models.ebean.Organization.findByOrganization(Organization.DEFAULT));
     Ebean.save(ebeanExpression1);
     final models.ebean.Expression ebeanExpression2 = TestBeanFactory.createEbeanExpression();
     ebeanExpression2.setUuid(UUID.randomUUID());
     ebeanExpression2.setOrganization(
         models.ebean.Organization.findByOrganization(Organization.DEFAULT));
     Ebean.save(ebeanExpression2);
     transaction.commit();
   }
   Assert.assertEquals(2, exprRepo.getExpressionCount(Organization.DEFAULT));
 }
 @Test
 public void addOrUpdateExpressionUpdateCase() throws IOException {
   final UUID uuid = UUID.randomUUID();
   final models.ebean.Organization organization;
   try (Transaction transaction = Ebean.beginTransaction()) {
     final models.ebean.Expression ebeanExpression = TestBeanFactory.createEbeanExpression();
     ebeanExpression.setUuid(uuid);
     organization = ebeanExpression.getOrganization();
     Ebean.save(ebeanExpression);
     transaction.commit();
   }
   final Expression actual =
       TestBeanFactory.createExpressionBuilder().setId(uuid).setCluster("new-cluster").build();
   exprRepo.addOrUpdateExpression(actual, TestBeanFactory.organizationFrom(organization));
   final Expression expected =
       exprRepo.get(uuid, TestBeanFactory.organizationFrom(organization)).get();
   Assert.assertEquals(expected, actual);
 }
 @Test(expected = PersistenceException.class)
 public void testThrowsExceptionWhenQueryFails() {
   final UUID uuid = UUID.randomUUID();
   exprRepo.addOrUpdateExpression(
       TestBeanFactory.createExpressionBuilder().setId(uuid).setCluster("new-cluster").build(),
       Organization.DEFAULT);
   models.ebean.Expression ebeanExpression1 =
       Ebean.find(models.ebean.Expression.class).where().eq("uuid", uuid).findUnique();
   models.ebean.Expression ebeanExpression2 =
       Ebean.find(models.ebean.Expression.class).where().eq("uuid", uuid).findUnique();
   try (Transaction transaction = Ebean.beginTransaction()) {
     ebeanExpression1.setCluster("new-cluster1");
     ebeanExpression2.setCluster("new-cluster2");
     ebeanExpression2.save();
     ebeanExpression1.save();
     transaction.commit();
   } catch (final IOException e) {
     // Do Nothing
   }
 }
Пример #9
0
  @Override
  protected Result hook_call(Context ctx) throws Throwable {
    // TODO make it possible to get an extra transaction (either nested, or separate)
    // TODO allow for transactions across multiple requests
    boolean successful = false;
    try {
      Ebean.beginTransaction();

      Result result = delegate.call(ctx);
      successful = true;
      return result;
    } finally {
      // if running tests, don't commit
      if (AppContext.Mode.isRunningTests() || !successful) {
        Ebean.rollbackTransaction();
      } else {
        // TODO do retry logic
        Ebean.commitTransaction();
      }
    }
  }
  @Test
  public void test() {

    LoggedSqlCollector.start();
    Transaction txn = Ebean.beginTransaction();
    try {
      txn.setBatch(PersistBatch.ALL);

      EdParent parent = new EdParent();
      parent.setName("MyComputer");

      EdChild child = new EdChild();
      child.setName("Harddisk 123");
      child.setParent(parent);
      ArrayList<EdChild> children = new ArrayList<EdChild>();
      children.add(child);
      parent.setChildren(children);

      Ebean.save(parent);

      // nothing flushed yet
      List<String> loggedSql0 = LoggedSqlCollector.start();
      assertEquals(0, loggedSql0.size());

      parent.setName("MyDesk");
      Ebean.save(parent);

      // nothing flushed yet
      assertEquals(0, LoggedSqlCollector.start().size());

      Ebean.commitTransaction();

      // insert statements for EdExtendedParent
      List<String> loggedSql2 = LoggedSqlCollector.start();
      assertEquals(2, loggedSql2.size());

    } finally {
      Ebean.endTransaction();
    }
  }
 /**
  * Mark the specified action as completed
  *
  * @param transactionId the unique transaction id for this action
  * @param scheduledActionUuid the unique name of an action
  */
 private void markAsStarted(String transactionId, String scheduledActionUuid) {
   try {
     Ebean.beginTransaction();
     SchedulerState schedulerState = new SchedulerState();
     schedulerState.actionUuid = scheduledActionUuid;
     schedulerState.transactionId = transactionId;
     schedulerState.isRunning = true;
     schedulerState.save();
     if (log.isDebugEnabled()) {
       log.debug(
           String.format(
               "Scheduled action for %s with transaction id %s started",
               scheduledActionUuid, transactionId));
     }
     Ebean.commitTransaction();
   } catch (Exception e) {
     log.error("Failed to mark as started", e);
     rollbackTransactionSilent();
   } finally {
     endTransactionSilent();
   }
 }
  @Test
  public void test_countUsingForegound() throws ExecutionException, InterruptedException {

    ResetBasicData.reset();

    PagedList<Order> pagedList = Ebean.find(Order.class).findPagedList(0, 6);

    LoggedSqlCollector.start();

    // kinda not normal but just wrap in a transaction to assert
    // the background fetch does not occur (which explicitly creates
    // its own transaction) ... so a bit naughty with the test here
    Ebean.beginTransaction();
    try {

      List<Order> orders = pagedList.getList();
      int totalRowCount = pagedList.getTotalRowCount();

      // invoke it again but cached...
      int totalRowCountAgain = pagedList.getTotalRowCount();

      List<String> loggedSql = LoggedSqlCollector.stop();

      assertTrue(orders.size() < totalRowCount);
      assertEquals(2, loggedSql.size());
      assertEquals(totalRowCount, totalRowCountAgain);

      String firstTxn = loggedSql.get(0).substring(0, 10);
      String secTxn = loggedSql.get(1).substring(0, 10);

      assertEquals(firstTxn, secTxn);

    } finally {
      Ebean.endTransaction();
    }
  }