neo4j - Component org.neo4j.kernel was successfully initialized, but failed to start.

我自己搭建了博客,以后可能不太在CSDN上发博文了,https://www.qingdujun.com/


参考“neo4j教程_w3cschool”上的例子,死活连接不上服务端。https://www.w3cschool.cn/neo4j/neo4j_native_java_api_example.html

报错:“Component ‘org.neo4j.kernel.NeoStoreDataSource@43738a82’ was successfully initialized, but failed to start.”

public class Neo4jJavaAPIDBOperation {
	public static void main(String[] args) {
		GraphDatabaseFactory dbFactory = new GraphDatabaseFactory();
		GraphDatabaseService db = dbFactory.newEmbeddedDatabase(new File("C:/TPNeo4jDB"));
		try (Transaction tx = db.beginTx()) {
			// Perform DB operations	
			tx.success();
		}
	}
}

关于该问题网上有几种说法:

  1. 为了美好的明天,在《org.neo4j.kernel.StoreLockException:》 一文中写道 :“产生异常的原因是,以内嵌方式连接的话,一个库应该是只能有一个连接。执行neo4j.bat console时就已经占用了一个链接,后来换HTTP的方式就行了”。

  2. gsp12356,在《Neo4j的安装及一些报错的解决》 中写道:“当时出现这个原因主要是我在命令行打了bin\Neo4j start所以我直接bin\Neo4j stop就可以了。”

  3. kingjack1176,在《java 链接neo4j数据库方法》 中也提到:“Java在对接数据库时,数据库服务器不能启动,只能在关闭状态才能使用。”

我挨个尝试了上述的几种说法,问题并没有得到解决。初步怀疑是neo4j的版本不一致造成的。于是,我按照官网的JAVA最新实例《Using Neo4j from Java》 参考其中的The Example Project内容,运行成功。

备注:我是Windows 10,安装的是neo4j server版(zip)。运行以下例子前,前先执行neo4j start

package com.neo4j.examples;

import org.neo4j.driver.v1.AuthTokens;
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.GraphDatabase;
import org.neo4j.driver.v1.Session;
import org.neo4j.driver.v1.StatementResult;
import org.neo4j.driver.v1.Transaction;
import org.neo4j.driver.v1.TransactionWork;
import org.neo4j.driver.v1.Values;

public class Neo4j implements AutoCloseable{
	private final Driver driver;
	
	public Neo4j(String uri, String username, String password){
		this.driver = GraphDatabase.driver(uri, AuthTokens.basic(username, password));
	}
	
	@Override
	public void close() throws Exception {
		this.driver.close();
	}
	
	public void printGreeting(final String message){
		try (Session session = this.driver.session()){
			String greeting = session.writeTransaction(new TransactionWork<String>() {
				@Override
				public String execute(Transaction arg0) {
					String cql = "CREATE (a:Greeting) SET a.message = $message RETURN a.message +', from node '+ id(a)";
					StatementResult result = arg0.run(cql, Values.parameters("message", message));
					return result.single().get(0).asString();
				}
			});
			System.out.println(greeting);
		}
	}
	
	public static void main(String[] args) throws Exception {
		try (Neo4j greeter = new Neo4j("bolt://localhost:7687", "neo4j", "a12345")) {
			greeter.printGreeting( "hello, world" );
		}
		System.out.println("Ok");
		//Component 'org.neo4j.kernel.NeoStoreDataSource@43738a82' was successfully initialized, but failed to start.
	}
}

对以上代码中CQL拼接部分稍作解释。其中,我们熟悉的写法是如下这样:

MATCH(a:Greeting) RETURN a.message, id(a)

a.message           	id(a)
"hello, world"         	0
"hello, world"         	20

其实也可以这样拼接起来,

MATCH(a:Greeting) RETURN a.message+id(a)

a.message+id(a)
"hello, world0" 
"hello, world20"

再扩展一下,中间再拼接一段字符串…

MATCH(a:Greeting) RETURN a.message+'-----'+id(a)

a.message+'-----'+id(a)
"hello, world-----0" 
"hello, world-----20"

另外,$message是一种Values.parameters("message", message)填充的写法。可以参阅孑然一身踽踽而行,在《Java连接Neo4j》 一文中的说明。



©qingdujun
2019-2-19 北京 海淀


References:
[1] https://neo4j.com/developer/java/#_the_example_project
[2] https://blog.csdn.net/u011809553/article/details/80369242
[3] https://my.oschina.net/zjllovecode/blog/2051095
[4] https://blog.csdn.net/gsp12356/article/details/83715743
[5] https://blog.csdn.net/kingjack1176/article/details/75344710?utm_source=blogxgwz0

©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页