Basic problem to access database Neo4j

251 views
Skip to first unread message

Claudia Morgado

unread,
Aug 6, 2012, 9:42:01 PM8/6/12
to ne...@googlegroups.com
Hello,

I'm studying Neo4j and  I'm trying to create a database to Erdos with Neo4J and Java.

I'm two files.
One file, I have one identifier and one name. Example: 1 - Cláudia 
I read this file and i create all nodes.

After this i read another file, and I would like create to relationships, but apper the error:
Exception in thread "main" java.lang.IllegalStateException: Unable to lock store [C:\Users\cmorgado\Documents\Mestrado\Neoj4\Exercicios\Erdos\data\neostore], this is usually a result of some other Neo4j kernel running using the same store.
at org.neo4j.kernel.impl.nioneo.store.CommonAbstractStore.checkStorage(CommonAbstractStore.java:161)
at org.neo4j.kernel.impl.nioneo.store.CommonAbstractStore.<init>(CommonAbstractStore.java:109)


I think this error occurs  because when a read de first file i'm creating the database:
//FIRST FILE
private static boolean treatFile(File configDirOrFile) {
GraphDatabaseService graphDatabaseService = new EmbeddedGraphDatabase(DB_PATH);
BancoService bancoService = new BancoService(graphDatabaseService);

How I use this database to create the database?
// SECOND FILE 
private static boolean treatFile2(File configDirOrFile2) {
GraphDatabaseService graphDatabaseService = new EmbeddedGraphDatabase(DB_PATH);
BancoService bancoService = new BancoService(graphDatabaseService);

Michael Hunger

unread,
Aug 7, 2012, 1:34:56 AM8/7/12
to ne...@googlegroups.com
Create the graphdatabase outside your functions and pass it in together with the files.

Don't forget to call db.shutdown() at the end of your app.

Michael

Claudia Morgado

unread,
Aug 7, 2012, 7:10:09 AM8/7/12
to ne...@googlegroups.com
You have a example, please.

2012/8/7 Michael Hunger <michael...@neotechnology.com>

Michael Hunger

unread,
Aug 7, 2012, 7:14:32 AM8/7/12
to ne...@googlegroups.com
Claudia,

sure, see also the Java examples in the Manual: http://docs.neo4j.org/chunked/milestone/tutorials-java-embedded.html

public void setupDatabase() {
GraphDatabaseService graphDatabaseService = new EmbeddedGraphDatabase(DB_PATH);
    try {
    createNodes(graphDatabaseService);
createRelationships(graphDatabaseService);
} finally {
graphDatabaseService.shutdown();
}
}

private void createNodes(GraphDatabaseService graphDatabaseService) {
BancoService bancoService = new BancoService(graphDatabaseService);
....
}

private void createRelationships(GraphDatabaseService graphDatabaseService) {
BancoService bancoService = new BancoService(graphDatabaseService);
....
}

Claudia Morgado

unread,
Aug 7, 2012, 7:46:38 AM8/7/12
to ne...@googlegroups.com
Thank you very much!



2012/8/7 Michael Hunger <michael...@neotechnology.com>

Claudia Morgado

unread,
Aug 7, 2012, 7:22:05 PM8/7/12
to ne...@googlegroups.com
Hi,

Sorry, but you could see my code. I don't know what is wrong, but occurred the error:
LOG:
Criando nos
Criando Relacionamentos
Ago 07, 2012 5:52:54 PM org.neo4j.kernel.impl.transaction.TxManager commit
Grave: Error writing transaction log
java.io.IOException: A operação solicitada não pode ser executada em um arquivo com uma seção mapeada pelo usuário aberta
at sun.nio.ch.FileDispatcherImpl.truncate0(Native Method)
at sun.nio.ch.FileDispatcherImpl.truncate(Unknown Source)
at sun.nio.ch.FileChannelImpl.truncate(Unknown Source)
at org.neo4j.kernel.impl.transaction.TxLog.truncate(TxLog.java:132)
at org.neo4j.kernel.impl.transaction.TxLog.switchToLogFile(TxLog.java:496)
at org.neo4j.kernel.impl.transaction.TxManager.getTxLog(TxManager.java:245)
at org.neo4j.kernel.impl.transaction.TxManager.commit(TxManager.java:532)
at org.neo4j.kernel.impl.transaction.TxManager.commit(TxManager.java:406)
at org.neo4j.kernel.impl.transaction.TransactionImpl.commit(TransactionImpl.java:117)
at org.neo4j.kernel.TopLevelTransaction.finish(TopLevelTransaction.java:115)
at br.com.mestrado.erdos.BancoService.criaRelacionamento(BancoService.java:68)
at br.com.mestrado.erdos.TesteErdos.treatFile2(TesteErdos.java:115)
at br.com.mestrado.erdos.TesteErdos.main(TesteErdos.java:53)

FIM
Exception in thread "main" org.neo4j.graphdb.TransactionFailureException: Unable to commit transaction
at org.neo4j.kernel.TopLevelTransaction.finish(TopLevelTransaction.java:131)
at br.com.mestrado.erdos.BancoService.criaRelacionamento(BancoService.java:68)
at br.com.mestrado.erdos.TesteErdos.treatFile2(TesteErdos.java:115)
at br.com.mestrado.erdos.TesteErdos.main(TesteErdos.java:53)
Caused by: javax.transaction.SystemException: TM encountered a problem,  error writing transaction log
at org.neo4j.kernel.impl.transaction.TxManager.commit(TxManager.java:539)
at org.neo4j.kernel.impl.transaction.TxManager.commit(TxManager.java:406)
at org.neo4j.kernel.impl.transaction.TransactionImpl.commit(TransactionImpl.java:117)
at org.neo4j.kernel.TopLevelTransaction.finish(TopLevelTransaction.java:115)
... 3 more
Caused by: java.io.IOException: A operação solicitada não pode ser executada em um arquivo com uma seção mapeada pelo usuário aberta
at sun.nio.ch.FileDispatcherImpl.truncate0(Native Method)
at sun.nio.ch.FileDispatcherImpl.truncate(Unknown Source)
at sun.nio.ch.FileChannelImpl.truncate(Unknown Source)
at org.neo4j.kernel.impl.transaction.TxLog.truncate(TxLog.java:132)
at org.neo4j.kernel.impl.transaction.TxLog.switchToLogFile(TxLog.java:496)
at org.neo4j.kernel.impl.transaction.TxManager.getTxLog(TxManager.java:245)
at org.neo4j.kernel.impl.transaction.TxManager.commit(TxManager.java:532)
... 6 more

--------------------
Code:
package br.com.mestrado.erdos;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.kernel.EmbeddedGraphDatabase;

import br.com.mestrado.erdos.*;

import java.io.BufferedReader;

public class TesteErdos {

private static final String DB_PATH = "/Users/cmorgado/Documents/Mestrado/Neoj4/Exercicios/Erdos/data";
private GraphDatabaseService graphDb;
 
public static void main (final String [] args) {
// criar o banco de dados
GraphDatabaseService graphDatabaseService = new EmbeddedGraphDatabase(DB_PATH);
try {
   
// ler o arquivo
File configDirOrFile = new File("/Users/cmorgado/Documents/Mestrado/Neoj4/Exercicios/Erdos/file/Cadastro");
if (configDirOrFile.isDirectory()) {
File[] files = configDirOrFile.listFiles();
for (File file : files) {
System.out.println("Criando nos");
treatFile(file, graphDatabaseService);
}
} else {
if (!treatFile(configDirOrFile, graphDatabaseService)) return;
}
// ler o arquivo
File configDirOrFile2 = new File("/Users/cmorgado/Documents/Mestrado/Neoj4/Exercicios/Erdos/file/Relacionamento");
if (configDirOrFile2.isDirectory()) {
File[] files = configDirOrFile2.listFiles();
for (File file : files) {
System.out.println("Criando Relacionamentos");
treatFile2(file, graphDatabaseService);
}
} else {
if (!treatFile2(configDirOrFile2, graphDatabaseService)) return;
}
} finally {
System.out.println("FIM");
graphDatabaseService.shutdown();
}
}


private static boolean treatFile(File configDirOrFile, GraphDatabaseService graphDatabaseService) {
BancoService bancoService = new BancoService(graphDatabaseService);

FileReader reader = null;
try {
reader = new FileReader(configDirOrFile);
} catch (FileNotFoundException e) {
System.out.println("Arquivo nao encontrado: " + configDirOrFile.getAbsolutePath());
return false;
}
BufferedReader buffer = new BufferedReader(reader);
String  line;
try {
while ((line = buffer.readLine()) != null) {
String[] lineParts = line.split("-");
Node usernode1 = bancoService.getOrCreateUserPessimistically((lineParts[0]),(lineParts[1]));
//Node usernode2 = bancoService.getOrCreateUserPessimistically((lineParts[1]));
//Banco usernode3 = bancoService.criaRelacionamento(usernode1,usernode2);
}
} catch (IOException e) {
System.out.println("Erro ao ler arquivo: " + configDirOrFile.getAbsolutePath());
return false;
}
return true;
}
  ///
private static boolean treatFile2(File configDirOrFile2, GraphDatabaseService graphDatabaseService) {
BancoService bancoService = new BancoService(graphDatabaseService);
FileReader reader = null;
try {
reader = new FileReader(configDirOrFile2);
} catch (FileNotFoundException e) {
System.out.println("Arquivo nao encontrado: " + configDirOrFile2.getAbsolutePath());
return false;
}
BufferedReader buffer = new BufferedReader(reader);
String  line;
try {
while ((line = buffer.readLine()) != null) {
String[] lineParts = line.split(" ");
int i = 0;
while ((lineParts[i]) != null) {
Node usernode1 = bancoService.getOrCreateUserPessimistically((lineParts[0]),"Teste");
Node usernode2 = bancoService.getOrCreateUserPessimistically((lineParts[i]),"Teste");
Banco usernode3 = bancoService.criaRelacionamento(usernode1,usernode2);
i = i++;
}
}
} catch (IOException e) {
System.out.println("Erro ao ler arquivo: " + configDirOrFile2.getAbsolutePath());
return false;
}
return true;
}
}


2012/8/7 Claudia Morgado <cmor...@cpqd.com.br>

Michael Hunger

unread,
Aug 8, 2012, 1:23:02 AM8/8/12
to ne...@googlegroups.com
Looks like a filesystem problem can you check access rights and the path for the db?

Sent from mobile device

Claudia Morgado

unread,
Aug 8, 2012, 3:28:35 PM8/8/12
to ne...@googlegroups.com
 
Michael,

Very thanks.
Now are  right.

Thanks.

2012/8/8 Michael Hunger <michael...@neopersistence.com>
Reply all
Reply to author
Forward
0 new messages