gpt4 book ai didi

java - Neo4j 嵌入式仅存储在一个运行时内

转载 作者:行者123 更新时间:2023-11-30 08:01:08 24 4
gpt4 key购买 nike

我正在通过嵌入式 Java API 尝试 Neo4J。我的构建路径看起来没问题(运行时没有异常)。当我创建一些节点和关系时,我可以在成功后直接查询它。但是在关闭并重新运行我的程序后,我只能获取我在新运行时中创建的数据,而没有获取之前的数据。但是如果我查看我的目录,我会发现大小随着每次运行时的增加而增加,我执行了数据的创建。

这是我的代码:

public static void main(String[] args)
{
GraphDatabaseService gdb = new GraphDatabaseFactory().newEmbeddedDatabase( "/mytestdb/" );

create( gdb );
query( gdb );

gdb.shutdown();
}

private static void query( GraphDatabaseService gdb )
{
StringLogger sl = StringLogger.wrap( new Writer()
{

@Override
public void write( char[] arg0, int arg1, int arg2 ) throws IOException
{
for( int i=arg1; i<=arg2; i++ ) System.out.print( arg0[i] );

}

@Override
public void flush() throws IOException
{}

@Override
public void close() throws IOException
{}
} );
ExecutionEngine ee = new ExecutionEngine( gdb, sl );
ExecutionResult result = ee.execute( "MATCH (p:Privilleg) RETURN p" );
System.out.println( result.dumpToString() );
}

private static void create( GraphDatabaseService gdb )
{
Transaction tx = gdb.beginTx();
Node project = gdb.createNode( MyLabels.Project );
Node user = gdb.createNode( MyLabels.User );
Node priv1 = gdb.createNode( MyLabels.Privilleg );
Node priv2 = gdb.createNode( MyLabels.Privilleg );

user.setProperty( "name", "Heinz" );
user.setProperty( "email", "heinz@gmx.net" );
priv1.setProperty( "name", "Allowed to read all" );
priv1.setProperty( "targets", Short.MAX_VALUE );
priv1.setProperty( "read", true );
priv1.setProperty( "write", false );
priv2.setProperty( "name", "Allowed to write all" );
priv2.setProperty( "targets", Short.MAX_VALUE );
priv2.setProperty( "read", false );
priv2.setProperty( "write", true );
project.setProperty( "name", "My first project" );
project.setProperty( "sname", "STARTUP" );
user.createRelationshipTo( priv1, MyRelationships.UserPrivilleg );
user.createRelationshipTo( priv2, MyRelationships.UserPrivilleg );
priv1.createRelationshipTo( project, MyRelationships.ProjectPrivilleg );
priv2.createRelationshipTo( project, MyRelationships.ProjectPrivilleg );
tx.success();
}

最佳答案

您的代码不会关闭交易。通常您使用 try-with-resources block :

try (Transaction tx=gdb.beginTx()) {
// do stuff in the graph
tx.success();
}

由于 TransactionAutoClosable,其 close() 方法将在离开代码块时隐式调用。如果(无论出于何种原因)您决定不使用 try-with-resources,请务必显式调用 close()

不同的注意事项:您的代码使用ExecutionEngine。从 Neo4j 2.2 开始,您可以直接调用 gdb.execute(myCypherString)

关于java - Neo4j 嵌入式仅存储在一个运行时内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31901708/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com