@Override
 public void deleteHomeInvite(HomeInvite homeInvite) throws StorageException {
   Transaction tx = ebean.beginTransaction();
   tx.setPersistCascade(false);
   ebean.delete(homeInvite, tx);
   tx.commit();
 }
  @Transactional
  public void doSomething(EBasicVer v) {

    EbeanServer server = Ebean.getServer(null);

    inMethodTransaction = server.currentTransaction();

    Transaction t = server.createTransaction();
    SqlUpdate u =
        server.createSqlUpdate("update e_basicver set last_update = last_update+1 where id = ?");
    u.setParameter(1, v.getId());
    int count = server.execute(u, t);
    Assert.assertEquals(1, count);

    t.commit();

    v.setName("some change");
    try {
      Ebean.save(v);
      // never get here
      Assert.assertTrue(false);
    } catch (PersistenceException e) {
      throw e;
    }
  }
  @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
  public void testBatch() {
    int rowsNumber = 100;

    EbeanServer server = Ebean.getServer(null);
    Transaction transaction = server.beginTransaction();
    transaction.setBatchMode(true);
    transaction.setBatchSize(30);
    try {
      for (int i = 0; i < rowsNumber; i++) {
        server.save(buildAccount(i));
      }
      server.commitTransaction();
    } finally {
      server.endTransaction();
    }
    List<Account> all = server.find(Account.class).select("name").findList();
    Assert.assertEquals("Batch update", rowsNumber, all.size());
  }
 @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
   }
 }
  @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();
    }
  }
  /** Get more Id's by executing a query and reading the Id's returned. */
  protected ArrayList<Integer> getMoreIds(int loadSize, Transaction t) {

    String sql = getSql(loadSize);

    ArrayList<Integer> newIds = new ArrayList<Integer>(loadSize);

    boolean useTxnConnection = t != null;

    Connection c = null;
    PreparedStatement pstmt = null;
    ResultSet rset = null;
    try {
      c = useTxnConnection ? t.getConnection() : dataSource.getConnection();

      pstmt = c.prepareStatement(sql);
      rset = pstmt.executeQuery();
      while (rset.next()) {
        int val = rset.getInt(1);
        newIds.add(Integer.valueOf(val));
      }
      if (newIds.size() == 0) {
        String m = "Always expecting more than 1 row from " + sql;
        throw new PersistenceException(m);
      }

      return newIds;

    } catch (SQLException e) {
      if (e.getMessage().contains("Database is already closed")) {
        String msg = "Error getting SEQ when DB shutting down " + e.getMessage();
        logger.info(msg);
        System.out.println(msg);
        return newIds;
      } else {
        throw new PersistenceException("Error getting sequence nextval", e);
      }
    } finally {
      if (useTxnConnection) {
        closeResources(null, pstmt, rset);
      } else {
        closeResources(c, pstmt, rset);
      }
    }
  }
 @Override
 public void saveHomeInvite(HomeInvite homeInvite) {
   Transaction tx = ebean.beginTransaction();
   ebean.save(homeInvite, tx);
   tx.commit();
 }