Package com.avaje.ebean

Examples of com.avaje.ebean.Transaction


    List<UTMaster> masters = new ArrayList<UTMaster>();
    for (int i = 0; i < numOfMasters; i++) {
      masters.add(createMasterAndDetails(i));
    }

    Transaction transaction = Ebean.beginTransaction();
    try {
      transaction.setBatchMode(true);
      transaction.setBatchSize(4);
      // transaction.setLogLevel(LogLevel.SUMMARY);
      // transaction.setBatchGetGeneratedKeys(false);

      Ebean.save(masters);

      transaction.commit();

    } finally {
      Ebean.endTransaction();
    }
  }
View Full Code Here


  @Override
  public void run(TestResult testResult) {
    try {
      super.run(testResult);
    } finally {
      Transaction tx = getServer().currentTransaction();
      if (tx != null && tx.isActive()) {
        // transaction left running after the test, rollback it to make
        // the environment ready for the next test
        tx.rollback();
      }
    }
  }
View Full Code Here

  public void test() throws SQLException {
   
    ResetBasicData.reset();
   
    // Transaction supplies our jdbc Connection
    Transaction txn = Ebean.beginTransaction();
   
    PreparedStatement pstmt = null;
   
    try {
      pstmt = txn.getConnection().prepareStatement("select id, name, billing_address_id from o_customer");
     
      // ResultSet will be closed by Ebean
      ResultSet resultSet = pstmt.executeQuery();
     
      RawSql rawSql = new RawSql(resultSet, "id", "name", "billingAddress.id");
     
      List<Customer> list = Ebean.find(Customer.class)
        .setRawSql(rawSql)
        // also test a secondary query join
        .fetch("billingAddress", new FetchConfig().query())
        .findList();

      for (Customer customer : list) {
        System.out.println("id:"+customer.getId()+" name:"+customer.getName()+" billingAddress:"+customer.getBillingAddress());
      }
     
    } finally {
      close(pstmt);
      txn.end();
    }
   
  }
View Full Code Here

    Ebean.createUpdate(Item.class, "delete from Item").execute();
    Ebean.createUpdate(Region.class, "delete from Region").execute();
    Ebean.createUpdate(Type.class, "delete from Type").execute();
    Ebean.createUpdate(SubType.class, "delete from SubType").execute();

        Transaction tx = getServer().beginTransaction();

    SubType subType = new SubType();
    SubTypeKey subTypeKey = new SubTypeKey();
    subTypeKey.setSubTypeId(1);
    subType.setKey(subTypeKey);
    subType.setDescription("ANY SUBTYPE");
    getServer().save(subType);

        Type type = new Type();
        TypeKey typeKey = new TypeKey();
        typeKey.setCustomer(1);
        typeKey.setType(10);
        type.setKey(typeKey);
        type.setDescription("Type Old-Item - Customer 1");
    type.setSubType(subType);
        getServer().save(type);

        type = new Type();
        typeKey = new TypeKey();
        typeKey.setCustomer(2);
        typeKey.setType(10);
        type.setKey(typeKey);
        type.setDescription("Type Old-Item - Customer 2");
    type.setSubType(subType);
        getServer().save(type);

        Region region = new Region();
        RegionKey regionKey = new RegionKey();
        regionKey.setCustomer(1);
        regionKey.setType(500);
        region.setKey(regionKey);
        region.setDescription("Region West - Customer 1");
        getServer().save(region);

        region = new Region();
        regionKey = new RegionKey();
        regionKey.setCustomer(2);
        regionKey.setType(500);
        region.setKey(regionKey);
        region.setDescription("Region West - Customer 2");
        getServer().save(region);

        Item item = new Item();
        ItemKey itemKey = new ItemKey();
        itemKey.setCustomer(1);
        itemKey.setItemNumber("ITEM1");
        item.setKey(itemKey);
    item.setUnits("P");
        item.setDescription("Fancy Car - Customer 1");
        item.setRegion(500);
        item.setType(10);
        getServer().save(item);

        item = new Item();
        itemKey = new ItemKey();
        itemKey.setCustomer(2);
        itemKey.setItemNumber("ITEM1");
        item.setKey(itemKey);
    item.setUnits("P");
        item.setDescription("Another Fancy Car - Customer 2");
        item.setRegion(500);
        item.setType(10);
        getServer().save(item);

        tx.commit();
    }
View Full Code Here

    ResetBasicData.reset();

    ResetBasicData.createOrderCustAndOrder("testPc");

    Transaction t = Ebean.beginTransaction();
    SpiTransaction spiTxn = (SpiTransaction) t;
    PersistenceContext pc = spiTxn.getPersistenceContext();

    System.out.println("pc0:" + pc.toString());

    // no orders or customers in the PC
    Assert.assertEquals(0, pc.size(Order.class));
    Assert.assertEquals(0, pc.size(Customer.class));

    Order order0 = null;
    try {

      EbeanServer server = Ebean.getServer(null);
      List<Order> list = server.find(Order.class).fetch("customer").fetch("details").findList();

      int orderSize = list.size();
      Assert.assertTrue(orderSize > 1);

      // keep a hold of one of them
      order0 = list.get(0);

      System.out.println("pc1:" + pc.toString());
      Assert.assertEquals(orderSize, pc.size(Order.class));

      System.gc();
      Assert.assertEquals(orderSize, pc.size(Order.class));
      Assert.assertTrue(pc.size(Customer.class) > 0);

      list = null;
      // System.gc();

      // transaction still holds PC ...
      // System.out.println("pc2:"+pc);
      // These asserts may not succeed depending on JVM
      // Assert.assertEquals(pc.size(Order.class), 1);
      // Assert.assertEquals(pc.size(Customer.class), 1);

    } finally {
      t.end();
    }

    System.gc();
    System.out.println("pc4:" + pc.toString());
View Full Code Here

    Customer c = Ebean.find(Customer.class).setId(1).findUnique();
    Assert.assertNotNull(c.getContacts());
    Assert.assertTrue("no contacts on test customer 1", c.getContacts().size() > 0);

    // start transaction so we have a "long running" persistence context
    Transaction tx = Ebean.beginTransaction();
    try {
      List<Order> order = Ebean.find(Order.class).where(Expr.eq("customer.id", 1)).findList();

      Assert.assertNotNull(order);
      Assert.assertTrue(order.size() > 0);

      Customer customer = order.get(0).getCustomer();
      Assert.assertNotNull(customer);
      Assert.assertEquals(1, customer.getId().intValue());

      // this should lazily fetch the contacts
      List<Contact> contacts = customer.getContacts();

      Assert.assertNotNull(contacts);
      Assert.assertTrue("contacts not lazily fetched", contacts.size() > 0);
    } finally {
      tx.commit();
    }
  }
View Full Code Here

    try {
      dao.doSomething(v);
      // never get here
      Assert.assertTrue(false);
    } catch (OptimisticLockException e) {
      Transaction inMethodTransaction = dao.getInMethodTransaction();
      boolean active = inMethodTransaction.isActive();
      Assert.assertFalse(active);
    }
  }
View Full Code Here

        String prevLogDesc = al.getDescription();

        resetEvent();

        // simulate external change and test if we get change notification when we refresh the entity
        Transaction tx = getServer().beginTransaction();
        PreparedStatement pstm = tx.getConnection().prepareStatement("update audit_log set description = ? where id = ?");
        pstm.setString(1, "GHI");
        pstm.setLong(2, al.getId());
        int updated = pstm.executeUpdate();
        pstm.close();

        assertEquals(1, updated);

        tx.commit();

        assertNull(lastPce);
        assertEquals(0, nuofEvents);

        getServer().refresh(al);
View Full Code Here

      if (!"h2".equals(platformName)){
        // readSequenceValue is H2 specific
        return;
      }
     
        Transaction tx = getServer().beginTransaction();

        long sequenceStart = readSequenceValue(tx, GenKeySequence.SEQUENCE_NAME);       
       
        GenKeySequence al = new GenKeySequence();
        al.setDescription("my description");
View Full Code Here

      if (!IdType.IDENTITY.equals(idType)){
        // only run this test when SEQUENCE is being used
        return;
      }

        Transaction tx = getServer().beginTransaction();

        GenKeyIdentity al = new GenKeyIdentity();
        al.setDescription("my description");
        getServer().save(al);

        // For JDBC batching we won't get the id until after
        // the batch has been flushed explicitly or via commit
        //assertNotNull(al.getId());

        tx.commit();
       
        assertNotNull(al.getId());

    }
View Full Code Here

TOP

Related Classes of com.avaje.ebean.Transaction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.