Search in sources :

Example 46 with OObjectDatabaseTx

use of com.orientechnologies.orient.object.db.OObjectDatabaseTx in project orientdb by orientechnologies.

the class TransactionConsistencyTest method testTransactionsCache.

public void testTransactionsCache() throws Exception {
    OObjectDatabaseTx database = new OObjectDatabaseTx(url);
    database.open("admin", "admin");
    try {
        Assert.assertFalse(database.getTransaction().isActive());
        OSchema schema = database.getMetadata().getSchema();
        OClass classA = schema.createClass("TransA");
        classA.createProperty("name", OType.STRING);
        ODocument doc = new ODocument(classA);
        doc.field("name", "test1");
        doc.save();
        ORID orid = doc.getIdentity();
        database.begin();
        Assert.assertTrue(database.getTransaction().isActive());
        doc = orid.getRecord();
        Assert.assertEquals("test1", doc.field("name"));
        doc.field("name", "test2");
        doc = orid.getRecord();
        Assert.assertEquals("test2", doc.field("name"));
        // There is NO SAVE!
        database.commit();
        doc = orid.getRecord();
        Assert.assertEquals("test1", doc.field("name"));
    } finally {
        database.close();
    }
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) OObjectDatabaseTx(com.orientechnologies.orient.object.db.OObjectDatabaseTx) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ORID(com.orientechnologies.orient.core.id.ORID) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 47 with OObjectDatabaseTx

use of com.orientechnologies.orient.object.db.OObjectDatabaseTx in project orientdb by orientechnologies.

the class TransactionConsistencyTest method testRollbackWithRemove.

/**
   * When calling .remove(o) on a collection, the row corresponding to o is deleted and not restored when the transaction is rolled
   * back.
   * 
   * Commented code after data model change to work around this problem.
   */
@SuppressWarnings("unused")
@Test
public void testRollbackWithRemove() {
    // check if the database exists and clean before running tests
    OObjectDatabaseTx database = new OObjectDatabaseTx(url);
    database.open("admin", "admin");
    try {
        Account account = new Account();
        account.setName("John Grisham");
        account = database.save(account);
        Address address1 = new Address();
        address1.setStreet("Mulholland drive");
        Address address2 = new Address();
        address2.setStreet("Via Veneto");
        List<Address> addresses = new ArrayList<Address>();
        addresses.add(address1);
        addresses.add(address2);
        account.setAddresses(addresses);
        account = database.save(account);
        database.commit();
        String originalName = account.getName();
        database.begin(TXTYPE.OPTIMISTIC);
        Assert.assertEquals(account.getAddresses().size(), 2);
        // delete one of the objects in the Books collection to see how rollback behaves
        account.getAddresses().remove(1);
        Assert.assertEquals(account.getAddresses().size(), 1);
        // change an attribute to see if the change is rolled back
        account.setName("New Name");
        account = database.save(account);
        // before rollback this is fine because one of the books was removed
        Assert.assertEquals(account.getAddresses().size(), 1);
        // rollback the transaction
        database.rollback();
        // ignore cache, get a copy of author from the datastore
        account = database.reload(account, true);
        // this is fine, author still linked to 2 books
        Assert.assertEquals(account.getAddresses().size(), 2);
        // name is restored
        Assert.assertEquals(account.getName(), originalName);
        int bookCount = 0;
        for (Address b : database.browseClass(Address.class)) {
            if (b.getStreet().equals("Mulholland drive") || b.getStreet().equals("Via Veneto"))
                bookCount++;
        }
        // this fails, only 1 entry in the datastore :(
        Assert.assertEquals(bookCount, 2);
    } finally {
        database.close();
    }
}
Also used : Account(com.orientechnologies.orient.test.domain.business.Account) Address(com.orientechnologies.orient.test.domain.business.Address) OObjectDatabaseTx(com.orientechnologies.orient.object.db.OObjectDatabaseTx) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test) DatabaseAbstractTest(com.orientechnologies.DatabaseAbstractTest)

Example 48 with OObjectDatabaseTx

use of com.orientechnologies.orient.object.db.OObjectDatabaseTx in project orientdb by orientechnologies.

the class OServerTest method testRestart.

/**
   * Test for https://github.com/orientechnologies/orientdb/issues/1667
   */
@Test
public void testRestart() throws Exception {
    // set ORIENTDB_HOME
    final String buildDirectory = System.getProperty("buildDirectory", ".");
    System.setProperty("ORIENTDB_HOME", buildDirectory + File.separator + OServerTest.class.getSimpleName());
    OLogManager.instance().info(this, "ORIENTDB_HOME: " + System.getProperty("ORIENTDB_HOME"));
    // loop for start & stop server
    for (int i = 0; i < 5; i++) {
        OLogManager.instance().info(this, "Iteration " + i);
        OServer server = new OServer(false).activate();
        // create database if does not exist
        OObjectDatabaseTx database = new OObjectDatabaseTx("plocal:" + System.getProperty("ORIENTDB_HOME") + "/test-db");
        if (!database.exists())
            database.create();
        database.open("admin", "admin");
        database.countClass("ouser");
        database.close();
        server.shutdown();
    }
}
Also used : OServer(com.orientechnologies.orient.server.OServer) OObjectDatabaseTx(com.orientechnologies.orient.object.db.OObjectDatabaseTx) Test(org.testng.annotations.Test)

Aggregations

OObjectDatabaseTx (com.orientechnologies.orient.object.db.OObjectDatabaseTx)48 Test (org.testng.annotations.Test)25 ORID (com.orientechnologies.orient.core.id.ORID)15 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)14 HashMap (java.util.HashMap)11 HashSet (java.util.HashSet)11 Map (java.util.Map)9 ORecordId (com.orientechnologies.orient.core.id.ORecordId)8 EnumTest (com.orientechnologies.orient.test.domain.base.EnumTest)8 JavaAttachDetachTestClass (com.orientechnologies.orient.test.domain.base.JavaAttachDetachTestClass)8 Planet (com.orientechnologies.orient.test.domain.base.Planet)8 Satellite (com.orientechnologies.orient.test.domain.base.Satellite)8 Child (com.orientechnologies.orient.test.domain.business.Child)8 Set (java.util.Set)8 ArrayList (java.util.ArrayList)5 Proxy (javassist.util.proxy.Proxy)5 BeforeClass (org.testng.annotations.BeforeClass)5 CycleChild (com.orientechnologies.orient.test.domain.cycle.CycleChild)4 GrandChild (com.orientechnologies.orient.test.domain.cycle.GrandChild)4 LazyChild (com.orientechnologies.orient.test.domain.lazy.LazyChild)4