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();
}
}
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();
}
}
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();
}
}
Aggregations