0

question background:

 1.database is neo4j 2.3.1, driver using jdbc;  
 2.db connection initialized as a class member, default is auto-commit(not changed); 

To avoid insert duplicates, i query before insert. after program stopped, found duplicates. why?

code:

String query = "CREATE (n:LABEL {name:'jack'})";
System.out.println(query);
Statement stmt = dbConnection.createStatement();
stmt.executeUpdate(query);
stmt.close();
Supamiu
  • 8,501
  • 7
  • 42
  • 76
baron.wang
  • 331
  • 4
  • 5

2 Answers2

0
  1. Use MERGE + unique constraints instead
  2. How do you "check"
  3. You would have to check in the same tx and also take a write lock
Michael Hunger
  • 41,339
  • 3
  • 57
  • 80
  • I think adding "use prepared statements" can be great too ;) – Supamiu Nov 25 '15 at 08:56
  • hi @Michael Hunger, thanks . 1.can u be more specific on "MERGE", i know being "unique" is one way out. 2. just think that the api is kind of confusing, if u just inserted a record, why can not query it on the same connection?(i tried, if using a new connection each insert, can query it out, thus no duplicates) – baron.wang Nov 25 '15 at 11:47
0

after debugging i found that for neo4j-jdbc(v2.1.4), the default db connection transaction level is TRANSACTION_NONE, then i set it to TRANSACTION_READ_COMMITTED, above issue disappeared. so i think that TRANSACTION_READ_COMMITTED will force the previous insert committed, though this is not the recommended way. for isolation level refer to:Difference between read commit and repeatable read

Community
  • 1
  • 1
baron.wang
  • 331
  • 4
  • 5