执行OSQL脚本:为什么当前数据库实例在当前线程上不活动?

时间:2015-10-29 09:07:12

标签: java orientdb

我有一个旧项目,我正在尝试将OrientDB从2.0.14(代码工作)升级到最新的2.1.4。

我想进行以下测试:

package it.celi.orient.util;

import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;

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

import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
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);
        }
    }

}

所需的资源是:

# schema.osql
CREATE CLASS V;

# clean.osql

DELETE VERTEX V;

# comment

CREATE CLASS Z EXTENDS V;

堆栈跟踪是:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running it.celi.orient.util.OrientScriptImporterTest
ott 29, 2015 9:53:10 AM com.orientechnologies.common.log.OLogManager log
INFORMAZIONI: OrientDB auto-config DISKCACHE=4.061MB (heap=1.748MB os=7.857MB disk=12.613MB)
Executing script: DELETE VERTEX V
java.lang.IllegalStateException: Current database instance (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx@25bfcafd) is not active on current thread (Thread[main,5,main]). Current active database is: com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx@4b6690c0
    at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.checkIfActive(ODatabaseDocumentTx.java:3138)
    at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.commit(ODatabaseDocumentTx.java:2557)
    at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.commit(ODatabaseDocumentTx.java:2551)
    at it.celi.orient.util.OrientUtil.executeScriptFromFile(OrientUtil.java:82)
    at it.celi.orient.util.OrientScriptImporter.process(OrientScriptImporter.java:40)
    at it.celi.orient.util.OrientScriptImporterTest.testWithPopulate(OrientScriptImporterTest.java:40)
    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)
java.lang.IllegalArgumentException: Class 'Z' not found in current database
    at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.browseClass(ODatabaseDocumentTx.java:2249)
    at com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx.browseClass(ODatabaseDocumentTx.java:2241)
    at it.celi.orient.util.OrientScriptImporterTest.browseSomeClass(OrientScriptImporterTest.java:79)
    at it.celi.orient.util.OrientScriptImporterTest.testWithPopulate(OrientScriptImporterTest.java:42)
    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)
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 3.984 sec <<< FAILURE! - in it.celi.orient.util.OrientScriptImporterTest
testWithPopulate(it.celi.orient.util.OrientScriptImporterTest)  Time elapsed: 3.927 sec  <<< FAILURE!
java.lang.AssertionError: null
    at org.junit.Assert.fail(Assert.java:86)
    at org.junit.Assert.fail(Assert.java:95)
    at it.celi.orient.util.OrientScriptImporterTest.browseSomeClass(OrientScriptImporterTest.java:82)
    at it.celi.orient.util.OrientScriptImporterTest.testWithPopulate(OrientScriptImporterTest.java:42)


Results :

Failed tests: 
  OrientScriptImporterTest.testWithPopulate:42->browseSomeClass:82 null

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.532s
[INFO] Finished at: Thu Oct 29 09:53:13 CET 2015
[INFO] Final Memory: 16M/215M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.16:test (default-test) on project dd-orient: There are test failures.
[ERROR] 
[ERROR] Please refer to /home/rayman/workspace/dd-bones/dd-backbone/dd-orient/target/surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

实用程序类是:

package it.celi.orient.util;

import static it.celi.util.ObjectUtil.not;

import java.io.File;
import java.io.IOException;

import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory;

import it.celi.orient.util.OrientUtil;
import it.celi.platform.Engine;

public class OrientScriptImporter implements Engine {

    private OrientGraphFactory pool;
    private final String[] orientSchemas;

    public OrientScriptImporter(OrientGraphFactory pool, String... orientSchemas) {
        this.pool = pool;
        this.orientSchemas = orientSchemas;
    }

    @Override
    public void initialize() {
    }

    @Override
    public void process() {

        ODatabaseDocumentTx db = null;

        try {
            db = pool.getDatabase();
            for(String fileString : orientSchemas) {
                File file = new File(fileString);
                if(not(file.exists())) {
                    continue;
                }
                OrientUtil.executeScriptFromFile(db, fileString);
            }   
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            OrientUtil.closeQuietly(db);
        }
    }

}

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

1 个答案:

答案 0 :(得分:0)

我试过你的测试用例,它似乎在这里工作正常,也许我错过了一些东西......

无论如何,作为快速建议,您可以尝试添加

 db.activateOnCurrentThread();

executeScriptFromFile()方法的开头