Java Code Examples for com.orientechnologies.orient.core.db.document.ODatabaseDocument#commit()

The following examples show how to use com.orientechnologies.orient.core.db.document.ODatabaseDocument#commit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SaveODocumentsCommand.java    From Orienteer with Apache License 2.0 6 votes vote down vote up
@Override
public void onClick(Optional<AjaxRequestTarget> targetOptional) {
	table.visitChildren(OrienteerDataTable.MetaContextItem.class, new IVisitor<OrienteerDataTable.MetaContextItem<ODocument, ?>, Void>() {

		@Override
		public void component(MetaContextItem<ODocument, ?> rowItem, IVisit<Void> visit) {
			ODocument doc = rowItem.getModelObject();
			if(doc.isDirty()) {
				if(doc.getIdentity().isNew()) SaveODocumentCommand.realizeMandatory(doc);
				doc.save();
			}
			visit.dontGoDeeper();
		}
	});
	if(forceCommit) {
		ODatabaseDocument db = getDatabase();
		boolean active = db.getTransaction().isActive();
		db.commit();
		if(active) db.begin();
	}
	super.onClick(targetOptional);
}
 
Example 2
Source File: TestNotificationFactories.java    From Orienteer with Apache License 2.0 6 votes vote down vote up
@After
@Sudo
public void destroy() {
  ODatabaseDocument db = ODatabaseRecordThreadLocal.instance().get();
  for (int i = 1; i <= 10; i++) {
    try {
      db.begin();
      db.delete(testNotification.getDocument());
      db.commit();
      break;
    } catch (Exception e) {
      if (i == 10) {
        throw new IllegalStateException(e);
      }
    }
  }
}
 
Example 3
Source File: DocumentPool.java    From guice-persist-orient with MIT License 6 votes vote down vote up
@Override
public void commit() {
    final ODatabaseDocument db = transaction.get();
    if (db == null) {
        // pool not participate in current transaction
        return;
    }
    // this is an error for external transaction too because external unit of work must end
    // before manual connection close
    if (db.isClosed()) {
        // connection was closed manually, no need for rollback
        transaction.remove();
        checkOpened(db);
    }
    if (!transactionManager.isExternalTransaction()) {
        // may not cause actual commit/close because force parameter not used
        // in case of commit exception, transaction manager must perform rollback
        // (and close will take effect in rollback)
        db.commit();
        db.close();
    }
    transaction.remove();
    logger.trace("Pool {} commit successful", getType());
}
 
Example 4
Source File: TestInAppOrientDBCompatibility.java    From wicket-orientdb with Apache License 2.0 6 votes vote down vote up
@Ignore //TODO: Uncomment when OrientDB issue will be fixed: https://github.com/orientechnologies/orientdb/issues/8067
@Test
public void testLinkToOUser() {
	ODatabaseDocument db = wicket.getTester().getDatabase();
	OSchema schema = db.getMetadata().getSchema();
	final OClass classA = schema.createClass("TestLinkToOUser");
	classA.createProperty("name", OType.STRING);
	classA.createProperty("user", OType.LINK).setLinkedClass(schema.getClass("OUser"));
	ORID userRid = new ORecordId("#5:0");
	ODocument doc = new ODocument(classA);
	wicket.getTester().signIn("writer", "writer");
	db = wicket.getTester().getDatabase();
	db.begin();
	ODocument userDoc = userRid.getRecord();
	userDoc.field("roles");
	doc.field("Admin");
	doc.field("user", userDoc);
	doc.save();
	db.commit();
}
 
Example 5
Source File: ModuledDataInstallator.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
@Override
public void onBeforeDestroyed(Application application) {
	super.onBeforeDestroyed(application);
	OrienteerWebApplication app = (OrienteerWebApplication)application;
	ODatabaseDocument db = (ODatabaseDocument)getDatabase(app);
	try
	{
		Map<String, ODocument> installedModules = getInstalledModules(db);
		for(IOrienteerModule module: app.getRegisteredModules())
		{
			try
			{
				db.begin();
				module.onDestroy(app, db, installedModules.get(module.getName()));
				db.commit();
			} catch (Exception e)
			{
				LOG.error("Exception during destroying module '"+module.getName()+"'", e);
				db.rollback();
			}
		}
	} 
	finally
	{
		db.close();
	}
}
 
Example 6
Source File: AbstractCustomValueModel.java    From wicket-orientdb with Apache License 2.0 5 votes vote down vote up
@Override
public void setObject(V object) {
	ODatabaseDocument db = OrientDbWebSession.get().getDatabase(); 
	boolean isActiveTransaction = db.getTransaction().isActive();
	if(isActiveTransaction) db.commit(); // Schema changes should be done outside of transaction
	try {
		setValue(objectModel.getObject(), parameterModel.getObject(), object);
	} finally {
		if(isActiveTransaction) db.begin();
	}
}
 
Example 7
Source File: ApplyEditorChangesBehavior.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
private void addClassesToSchema(List<OArchitectOClass> classes) {
    ODatabaseDocument db = OrienteerWebApplication.get().getDatabase();
    db.commit();
    OSchema schema = db.getMetadata().getSchema();
    for (OArchitectOClass architectOClass : classes) {
        addClassToSchema(schema, architectOClass);
    }
    db.commit();
}
 
Example 8
Source File: Module.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
@Override
public void onUpdate(OrienteerWebApplication app, ODatabaseDocument db, int oldVersion, int newVersion) {
	super.onUpdate(app, db, oldVersion, newVersion);
	onInstall(app, db);
	db.commit();
	db.command(new OCommandSQL("ALTER CLASS "+OIntegrationConfig.TASK_CLASS+" SUPERCLASS "+OTask.TASK_CLASS)).execute();
}
 
Example 9
Source File: TestSendNotificationTask.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
@Before
@Sudo
public void init() {
  ONotificationScheduler.stopAll();

  ODatabaseDocument db = ODatabaseRecordThreadLocal.instance().get();

  ODocument mailTransport = notificationDAO.findTransportByAlias(TestDataModule.TRANSPORT_MAIL);

  if (mailTransport == null) {
    throw new IllegalStateException("There is no transport with alias: " + TestDataModule.TRANSPORT_MAIL);
  }

  OMail mail = OMailUtils.getOMailByName(TestDataModule.MAIL_TEST)
          .orElseThrow(IllegalStateException::new);

  notifications = new LinkedList<>();

  for (int i = 0; i < NOTIFICATIONS; i++) {
    db.begin();
    OPreparedMail preparedMail = new OPreparedMail(mail);
    IOMailNotification notification = DAO.create(IOMailNotification.class);
    notification.fromStream(new ODocument(IOMailNotification.CLASS_NAME));
    notification.setTransport(mailTransport);
    notification.setPreparedMail(preparedMail.getDocument());
    preparedMail.addRecipient("[email protected]");
    preparedMail.save();
    notification.save();
    notifications.add(notification);
    db.commit();
  }
}
 
Example 10
Source File: SaveODocumentCommand.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
@Override
public void onClick(Optional<AjaxRequestTarget> targetOptional) {
	ODocument doc = getModelObject();
	if(doc.getIdentity().isNew()) realizeMandatory(doc);
	doc.save();
	if(forceCommit) {
		ODatabaseDocument db = getDatabase();
		boolean active = db.getTransaction().isActive();
		db.commit();
		if(active) db.begin();
	}
       super.onClick(targetOptional);
}
 
Example 11
Source File: OTasksTest.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore
public void consoleTaskTest() throws Exception{
	assertTrue(OrientDbWebSession.get().signIn("admin", "admin"));
	ODatabaseDocument db = OrientDbWebSession.get().getDatabase();
	assertFalse(db.isClosed());
	db.commit();
	try
	{
		ODocument taskDocument = new ODocument(OConsoleTask.TASK_CLASS);
		taskDocument.field(OTask.Field.AUTODELETE_SESSIONS.fieldName(),false);
		taskDocument.field(OConsoleTask.Field.INPUT.fieldName(),CONSOLE_TEST_COMMAND);
		taskDocument.save();
		db.commit();
		OTask task = OTask.makeFromODocument(taskDocument);

		ODocument taskDocumentAD = new ODocument(OConsoleTask.TASK_CLASS);
		taskDocumentAD.field(OTask.Field.AUTODELETE_SESSIONS.fieldName(),true);
		taskDocumentAD.field(OConsoleTask.Field.INPUT.fieldName(),CONSOLE_TEST_COMMAND);
		taskDocumentAD.save();
		db.commit();
		OTask taskAD = OTask.makeFromODocument(taskDocumentAD);
		
		OTaskSessionRuntime taskSession = task.startNewSession();
		OTaskSessionRuntime taskSessionAD = taskAD.startNewSession();
		Thread.sleep(CONSOLE_TASK_DELAY);
		db.commit();
		ODocument taskSessionDoc = taskSession.getOTaskSessionPersisted().getDocument();
		taskSessionDoc.load();
		assertNull(taskSessionDoc.field(ITaskSession.Field.ERROR.fieldName()));
		assertNotNull(taskSessionDoc.field(ITaskSession.Field.FINISH_TIMESTAMP.fieldName()));
		assertEquals(12L, (Object) taskSessionDoc.field(ITaskSession.Field.PROGRESS_CURRENT.fieldName()));
		assertEquals(ITaskSession.Status.INTERRUPTED,taskSession.getStatus());
	} finally
	{
		OrientDbWebSession.get().signOut();
	}
}
 
Example 12
Source File: TestInAppOrientDBCompatibility.java    From wicket-orientdb with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore
public void testLoosingLinkedClass() throws Exception
{
	ODatabaseDocument db = wicket.getTester().getDatabase();
	OSchema schema = wicket.getTester().getSchema();
	OClass mainClass = schema.createClass("LMainClass");
	OClass embeddedClass = schema.createClass("LEmbeddedClass");
	mainClass.createProperty("name", OType.STRING);
	mainClass.createProperty("embedded", OType.EMBEDDED).setLinkedClass(embeddedClass);
	embeddedClass.createProperty("name", OType.STRING);
	
	db.begin();
	ODocument main = new ODocument(mainClass);
	main.field("name", "main");
	ODocument embedded = new ODocument(embeddedClass);
	//embedded.field("name", "embedded");
	main.field("embedded", embedded);
	//NO Save here!
	db.commit();
	db.close();
	
	main.fromStream(main.toStream());
	
	db = wicket.getTester().getDatabase();
	db.begin();
	assertEmbeddedIsCorrect(main);
	main.save();
	ORID recordId = main.getIdentity();
	db.commit();
	db.close();
	
	db = wicket.getTester().getDatabase();
	db.begin();
	main = recordId.getRecord();
	assertEmbeddedIsCorrect(main);
	db.commit();
}
 
Example 13
Source File: OTasksTest.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore
public void taskTestAndTaskSessionTest() throws Exception{
	assertTrue(OrientDbWebSession.get().signIn("admin", "admin"));
	ODatabaseDocument db = OrientDbWebSession.get().getDatabase();
	assertFalse(db.isClosed());
	db.commit();

	TestTask.init(db);
	
	try{
		ODocument taskDocument = new ODocument(TestTask.TASK_CLASS);
		taskDocument.field(OTask.Field.AUTODELETE_SESSIONS.fieldName(),false);
		taskDocument.save();
		db.commit();
		
		OTask task = OTask.makeFromODocument(taskDocument);
		OTaskSessionRuntime taskSession = task.startNewSession();
		
		ODocument taskSessionDoc = taskSession.getOTaskSessionPersisted().getDocument();

		assertNotNull(taskSessionDoc.field(ITaskSession.Field.THREAD_NAME.fieldName()));
		assertEquals(ITaskSession.Status.FINISHED,taskSession.getStatus());
		assertNotNull(taskSessionDoc.field(ITaskSession.Field.START_TIMESTAMP.fieldName()));
		assertNotNull(taskSessionDoc.field(ITaskSession.Field.FINISH_TIMESTAMP.fieldName()));
		assertEquals(TestTask.PROGRESS, (Object) taskSessionDoc.field(ITaskSession.Field.PROGRESS.fieldName()));
		assertEquals(TestTask.PROGRESS_CURRENT,(Object) taskSessionDoc.field(ITaskSession.Field.PROGRESS_CURRENT.fieldName()));
		assertEquals(TestTask.PROGRESS_FINAL,(Object) taskSessionDoc.field(ITaskSession.Field.PROGRESS_FINAL.fieldName()));
		assertEquals(false,taskSessionDoc.field(ITaskSession.Field.IS_STOPPABLE.fieldName()));
		assertEquals(false,taskSessionDoc.field(ITaskSession.Field.DELETE_ON_FINISH.fieldName()));
		assertNull(taskSessionDoc.field(ITaskSession.Field.ERROR_TYPE.fieldName()));
		assertNull(taskSessionDoc.field(ITaskSession.Field.ERROR.fieldName()));
	} finally
	{
		TestTask.close(db);
	}
	OrientDbWebSession.get().signOut();
}
 
Example 14
Source File: TestInAppOrientDBCompatibility.java    From wicket-orientdb with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore
public void testTransactions2() throws Exception {
	ODatabaseDocument db = wicket.getTester().getDatabase();
	try {
		assertFalse(db.getTransaction().isActive());
		OSchema schema = db.getMetadata().getSchema();
		OClass classA = schema.createClass("TransB");
		classA.createProperty("name", OType.STRING);
		ODocument doc = new ODocument(classA);
		doc.field("name", "test1");
		doc.save();
		ORID orid = doc.getIdentity();
		
		db.begin();
		assertTrue(db.getTransaction().isActive());
		doc = orid.getRecord();
		assertEquals("test1", doc.field("name"));
		doc.field("name", "test2");
		doc.save();
		doc = orid.getRecord();
		assertEquals("test2", doc.field("name"));
		doc.field("name", "test3");
		assertEquals("test3", doc.field("name"));
		//There is NO SAVE!
		db.commit();
		db.getLocalCache().clear();
		/* COMMENT START */
		//db.close();
		//db = wicket.getTester().getDatabase();
		/* COMMENT STOP */
		doc = orid.getRecord();
		assertEquals("test2", doc.field("name"));
		
	} finally {
		db.commit();
	}
}
 
Example 15
Source File: TestInAppOrientDBCompatibility.java    From wicket-orientdb with Apache License 2.0 5 votes vote down vote up
@Test
public void testTransactions() throws Exception {
	ODatabaseDocument db = wicket.getTester().getDatabase();
	try {
		assertFalse(db.getTransaction().isActive());
		OSchema schema = db.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();
		db.begin();
		assertTrue(db.getTransaction().isActive());
		doc = orid.getRecord();
		assertEquals("test1", doc.field("name"));
		doc.field("name", "test2");
		doc = orid.getRecord();
		assertEquals("test2", doc.field("name"));
		//There is NO SAVE!
		db.commit();
		db.getLocalCache().clear();
		/* COMMENT START */
		//db.close();
		//db = wicket.getTester().getDatabase();
		/* COMMENT STOP */
		doc = orid.getRecord();
		assertEquals("test1", doc.field("name"));
		
	} finally {
		db.commit();
	}
}
 
Example 16
Source File: OIndexMetaPanel.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
@Override
protected void setValue(OIndex<?> entity, String critery, V value) {
	ODatabaseDocument db = OrientDbWebSession.get().getDatabase();
	db.commit();
	try
	{
		if(OIndexPrototyper.DEF_COLLATE.equals(critery))
		{
			if(value!=null)
			{
				String collate = value.toString();
				entity.getDefinition().setCollate(OSQLEngine.getCollate(collate));
			}
			else
			{
				entity.getDefinition().setCollate(null);
			}
		}
		else
		{
			PropertyResolver.setValue(critery, entity, value, null);
		}
	} finally
	{
		db.begin();
	}
}
 
Example 17
Source File: ODatabaseMetaPanel.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
@Override
protected void setValue(ODatabase<?> entity, String critery, V value) {
    ODatabaseDocument db = OrientDbWebSession.get().getDatabase();
    db.commit();
    try
    {
        if(ATTRIBUTES.CLUSTERSELECTION.name().equals(critery))
        {
            if(value!=null) entity.set(ATTRIBUTES.valueOf(critery), value.toString());
        } else if(ATTRIBUTES.CUSTOM.name().equals(critery)) { 
        	if(value!=null) {
        		String stringValue = value.toString();
        		String[] customs = stringValue.split("\\r?\\n");
        		for (String custom : customs) {
		if(custom.indexOf('=')>0) entity.set(ATTRIBUTES.CUSTOM, custom);
	}
        	} else {
        		entity.set(ATTRIBUTES.CUSTOM, "clear");
        	}
        }else {
            entity.set(ATTRIBUTES.valueOf(critery), value);
        }
    } finally
    {
        db.begin();
    }
}
 
Example 18
Source File: TransactionRequestCycleListener.java    From wicket-orientdb with Apache License 2.0 5 votes vote down vote up
@Override
public void onDetach(RequestCycle cycle) {
	ODatabaseDocument db = ODatabaseRecordThreadLocal.instance().getIfDefined();
	if(db!=null) {
		if(db.getTransaction().isActive()) db.commit(true);
		db.close();
		ODatabaseRecordThreadLocal.instance().remove();
	}
}
 
Example 19
Source File: HooksTest.java    From Orienteer with Apache License 2.0 4 votes vote down vote up
@Test
@Sudo
public void testCalculableHook() throws Exception
{
	ODatabaseDocument db = OrientDbWebSession.get().getDatabase();
	OSchema schema = db.getMetadata().getSchema();
	
	assertFalse(db.isClosed());
	db.commit();
	if(schema.existsClass(TEST_CLASS_A)) schema.dropClass(TEST_CLASS_A);
	OClass oClass = schema.createClass(TEST_CLASS_A);
	try
	{
		oClass.createProperty("a", OType.INTEGER);
		oClass.createProperty("b", OType.INTEGER);
		OProperty cProperty = oClass.createProperty("c", OType.INTEGER);
		OProperty dProperty = oClass.createProperty("d", OType.INTEGER);
		CustomAttribute.CALCULABLE.setValue(cProperty, true);
		CustomAttribute.CALCULABLE.setValue(dProperty, true);
		CustomAttribute.CALC_SCRIPT.setValue(cProperty, "select sum(a, b) as value from TestClassA where @rid = ?");
		CustomAttribute.CALC_SCRIPT.setValue(dProperty, "sum(a, b)");
		
		ODocument doc = new ODocument(oClass);
		doc.field("a", 2);
		doc.field("b", 2);
		doc.save();
		doc.reload();
		assertEquals(4, (Object) doc.field("c"));
		assertEquals(4, (Object) doc.field("d"));
		doc.field("a", 3);
		doc.field("b", 3);
		doc.save();
		doc.reload();
		assertEquals(6, (Object) doc.field("c"));
		assertEquals(6, (Object) doc.field("d"));
		db.begin();
		doc.field("a", 4);
		doc.field("b", 4);
		doc.save();
		doc.reload();
		assertEquals(8, (Object) doc.field("c"));
		assertEquals(8, (Object) doc.field("d"));
		db.commit();
		db.begin();
		doc.field("a", 5);
		doc.field("b", 5);
		doc.save();
		doc.reload();
		assertEquals(10, (Object) doc.field("c"));
		assertEquals(10, (Object) doc.field("d"));
		db.commit();
	} finally
	{
		if(db.getTransaction().isActive()) db.commit();
		schema.dropClass(TEST_CLASS_A);
	}
}
 
Example 20
Source File: TransactionRequestCycleListener.java    From wicket-orientdb with Apache License 2.0 4 votes vote down vote up
@Override
public void end(RequestCycle cycle) {
	ODatabaseDocument db = ODatabaseRecordThreadLocal.instance().getIfDefined();
	if(db!=null && db.getTransaction().isActive()) db.commit();
}