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

The following examples show how to use com.orientechnologies.orient.core.db.document.ODatabaseDocument#close() . 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: DBClosure.java    From wicket-orientdb with Apache License 2.0 6 votes vote down vote up
/**
 * @return result of execution
 */
public final V execute()
{
	ODatabaseDocument db = null;
	ODatabaseRecordThreadLocal orientDbThreadLocal = ODatabaseRecordThreadLocal.instance();
	ODatabaseDocument oldDb = orientDbThreadLocal.getIfDefined();
	if(oldDb!=null) orientDbThreadLocal.remove(); //Required to avoid stack of transactions
	try
	{
		db = getSettings().getDatabasePoolFactory().get(getDBUrl(), getUsername(), getPassword()).acquire();
		db.activateOnCurrentThread();
		return execute(db);
	} 
	finally
	{
		if(db!=null) db.close();
		if(oldDb!=null) orientDbThreadLocal.set((ODatabaseDocumentInternal)oldDb);
		else orientDbThreadLocal.remove();
	}
}
 
Example 2
Source File: AbstractDataInstallator.java    From wicket-orientdb with Apache License 2.0 6 votes vote down vote up
@Override
public void onAfterInitialized(Application application) {
	OrientDbWebApplication app = (OrientDbWebApplication)application;
	ODatabaseDocument db = getDatabase(app);
	try
	{
		installData(app, db);
	}
	catch(Exception ex)
	{
		LOG.error("Data can't be installed", ex);
	}
	finally
	{
		db.close();
	}
}
 
Example 3
Source File: TestInAppOrientDBCompatibility.java    From wicket-orientdb with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore
public void testClassChange()
{
	ODatabaseDocument db = wicket.getTester().getDatabase();
	OSchema schema = db.getMetadata().getSchema();
	OClass classA = schema.createClass("TestClassChangeA");
	OClass classB = schema.createClass("TestClassChangeB");
	ODocument doc = new ODocument(classA);
	doc.save();
	doc = doc.getIdentity().getRecord();
	doc.setClassName(classB.getName());
	assertEquals(classB.getName(), doc.getClassName());
	doc = doc.getIdentity().getRecord();
	assertEquals(classB.getName(), doc.getClassName());
	ORID id = doc.getIdentity();
	db.commit(true);
	db.close();
	db = wicket.getTester().getDatabase();
	doc = id.getRecord();
	assertEquals(classB.getName(), doc.getClassName());
}
 
Example 4
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 5
Source File: OrientScriptCleanupHandler.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void cleanup(final String context) {
  ODatabaseDocument database = ODatabaseRecordThreadLocal.instance().getIfDefined();
  if (database != null && database.getStatus() == STATUS.OPEN) {
    database.close();
    log.warn("{} left a database connection open. Any opened connections should also be explicitly closed.", context);
  }
}
 
Example 6
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 7
Source File: TestStandaloneOrientDBCompatibility.java    From wicket-orientdb with Apache License 2.0 5 votes vote down vote up
public void testOrientDbLifeCycle(String dbURL, boolean createDb, boolean dropDb) throws Exception
	{
		Orient.instance().startup();
		assertNotNull(ODatabaseRecordThreadLocal.instance());
		Orient.instance().removeShutdownHook();
		OServer server = OServerMain.create();
		server.startup(OrientDbTestWebApplication.class.getResource("db.config.xml").openStream());
		server.activate();
		if(createDb)
		{
			ODatabaseDocument dbToCreate = new ODatabaseDocumentTx(dbURL);
			if(!dbToCreate.exists()) dbToCreate.create();
			dbToCreate.close();
		}
		assertNotNull(ODatabaseRecordThreadLocal.instance());
		ODatabaseDocument db = new OPartitionedDatabasePoolFactory().get(dbURL, "admin", "admin").acquire();
		db.close();
		assertNotNull(ODatabaseRecordThreadLocal.instance());
		if(dropDb)
		{
			@SuppressWarnings("resource")
			ODatabaseDocument dbToDrop = new ODatabaseDocumentTx(dbURL);
			dbToDrop.open("admin", "admin");
			dbToDrop.drop();
		}
		server.shutdown();
		Orient.instance().shutdown();
//		Thread.sleep(50);
	}
 
Example 8
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 9
Source File: DocumentPool.java    From guice-persist-orient with MIT License 5 votes vote down vote up
@Override
public void rollback() {
    final ODatabaseDocument db = transaction.get();
    if (db == null) {
        // pool not participate in current transaction or already committed (may happen if one other pool's
        // transaction fail: in this case all other transactions will be committed and after that
        // transactional manager call rollback, which will affect only failed pool and others will simply ignore it)
        return;
    }
    final boolean externalTransaction = transactionManager.isExternalTransaction();
    try {
        // do nothing fo external transaction
        if (!externalTransaction) {
            // may not cause actual rollback immediately because force not used
            checkOpened(db).rollback();
        }
        logger.trace("Pool {} rollback successful", getType());
    } finally {
        // don't touch external tx
        if (!externalTransaction && !db.isClosed()) {
            try {
                // release connection back to pool in any case
                db.close();
            } catch (Throwable ignored) {
                logger.trace(String.format("Pool %s failed to close database", getType()), ignored);
            }
        }
        transaction.remove();
    }
}
 
Example 10
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 11
Source File: TestInAppOrientDBCompatibility.java    From wicket-orientdb with Apache License 2.0 4 votes vote down vote up
@Test
public void testOFunctions() throws Exception
{
	ODatabaseDocument db = wicket.getTester().getDatabase();
	ODocument doc =  new ODocument(OFunction.CLASS_NAME);
	doc.field("name", "testResurection");
	doc.field("language", "JavaScript");
	doc.field("idempotent", true);
	doc.save();
	ORID orid = doc.getIdentity();
	for(int i=0;i<10;i++)
	{
		db = wicket.getTester().getDatabase();
		String signature = "signature"+RANDOM.nextLong();
		boolean isGoodCall = (i+1)%3 != 0;
		db.begin();
		doc = orid.getRecord();
		String code = isGoodCall?"return \""+signature+"\";":"return nosuchvar;";
		doc.field("code", code);
		doc.save();
		db.commit();
		db.close();
		if(isGoodCall)
		{
			String result;
			for(int j=0; j<3;j++)
			{
				result = wicket.getTester().executeUrl("orientdb/function/db/testResurection", "GET", null);
				assertContains(signature, result);
			}
		}
		else
		{
			try
			{
				wicket.getTester().executeUrl("orientdb/function/db/testResurection", "GET", null);
				assertFalse("We should be there, because function should have 400 response", true);
			} catch (Exception e)
			{
				//NOP
			}
		}
	}
}