Hi, I've already posted the question to StackOverflow, without any 
help: 
http://stackoverflow.com/questions/33409911/executing-osql-script-why-current-database-instance-is-not-active-on-current-th

Could you help me passing the attached Unit Test (the required resources 
are attached too)?

Briefly if I execute a DELETE command it fails only if there is some record 
in my db. The exception thrown is:

java.lang.IllegalStateException: Current database instance 
(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx@1e8ce150) 
is not active on current thread (Thread[main,5,main]). Current active 
database is: 
com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx@604f2bd2
at 
com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.checkIfActive(ODatabaseDocumentTx.java:3206)
at 
com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.commit(ODatabaseDocumentTx.java:2628)
at 
com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.commit(ODatabaseDocumentTx.java:2622)
at 
it.celi.orient.util.OrientScriptImporterTest$OrientScriptImporter.executeScriptFromFile(OrientScriptImporterTest.java:153)
at 
it.celi.orient.util.OrientScriptImporterTest$OrientScriptImporter.process(OrientScriptImporterTest.java:124)
at 
it.celi.orient.util.OrientScriptImporterTest.testWithPopulate(OrientScriptImporterTest.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at 
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at 
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at 
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at 
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at 
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at 
org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:264)
at 
org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
at 
org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124)
at 
org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:200)
at 
org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)


Thanks,
  Riccardo

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.
package it.celi.orient.util;

import static org.junit.Assert.fail;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;

import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory;
import com.tinkerpop.blueprints.impls.orient.OrientVertex;

public class OrientScriptImporterTest {

	private static final String DATABASE_URL = "plocal:/tmp/orient";
	private static final String USER = "admin";
	private static final String PASSWORD = "admin";

	@Before
	public void before() throws IOException {
		FileUtils.deleteDirectory(new File("/tmp/orient"));
	}
	
	@Test
	public void testWithPopulate() {
		OrientGraphFactory pool = new OrientGraphFactory(DATABASE_URL, USER, PASSWORD);

		OrientScriptImporter schemaImporter = new OrientScriptImporter(pool, "src/test/resources/schema.osql");
		schemaImporter.initialize();
		schemaImporter.process();
		
		populateDB(pool);
		
		OrientScriptImporter cleanImporter = new OrientScriptImporter(pool, "src/test/resources/clean.osql");
		cleanImporter.initialize();
		cleanImporter.process();
		
		browseSomeClass(pool);
		
	}

	private void populateDB(OrientGraphFactory pool) {
		ODatabaseDocumentTx db = null;

		try {
			db = pool.getDatabase();
			
			OrientGraph g = new OrientGraph(db);
			OrientVertex v = g.addVertex(null);
			v.setProperty("something", 5);
			
			db.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			OrientUtil.closeQuietly(db);
		}

	}

	private void browseSomeClass(OrientGraphFactory pool) {
		ODatabaseDocumentTx db = null;

		try {
			db = pool.getDatabase();
			
			try {
				db.browseClass("B");
				fail();
			} catch (Exception e) {
				
			}
			try {
				db.browseClass("Z");
			} catch (Exception e) {
				e.printStackTrace();
				fail();
			}

			db.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			OrientUtil.closeQuietly(db);
		}
	}
	
	private class OrientScriptImporter {

		private OrientGraphFactory pool;
		private final String[] orientSchemas;
		
		public OrientScriptImporter(OrientGraphFactory pool, String... orientSchemas) {
			this.pool = pool;
			this.orientSchemas = orientSchemas;
		}

		public void initialize() {
		}

		public void process() {

			ODatabaseDocumentTx db = null;

			try {
				db = pool.getDatabase();
				for(String fileString : orientSchemas) {
					File file = new File(fileString);
					if(!(file.exists())) {
						continue;
					}
					executeScriptFromFile(db, fileString);
				}	
			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				OrientUtil.closeQuietly(db);
			}
		}
		
		private void executeScriptFromFile(ODatabaseDocument db, String fileString) throws IOException {
			
			String line;
			try (
			    InputStream fis = new FileInputStream(fileString);
			    InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
			    BufferedReader br = new BufferedReader(isr);
			) {
			    while ((line = br.readLine()) != null) {
			    	line = prepareScriptLine(line);
			    	if(line.length() == 0) {
			    		continue;
			    	}
			    	db.commit();
			    	System.out.println("Executing script: " + line);
					OCommandSQL iCommand = new OCommandSQL(line);
					db.command(iCommand).execute();
					db.getMetadata().getSchema().reload();
			    	db.commit();
			    }
			}		
		}

		private String prepareScriptLine(String line) {
			line = line.trim();
			if(line.length() == 0 || line.startsWith("#")) {
				return "";
			}
			line = line.substring(0, line.length() - 1);
			return line;
		}

	}

}

Attachment: clean.osql
Description: Binary data

Attachment: schema.osql
Description: Binary data

Reply via email to