Here is my answer from StackOverflow.
0
down vote
<>
you can use one connection per thread, so you don't have to recreate them per
statement.
Actually there is no uniqueness guarantee for multithreaded CREATE UNIQUE
operations.
You would have to lock the two nodes in question. E.g. by removing an
non-existant property.
MATCH (u:Airport {name:{1}}), (r:Airport {name:{2}})
REMOVE u._lock_, r._lock_
MERGE (u)-[:FLIGHT_TO]->(r) ON CREATE SET f.amount = {3}
PS: you also would want to use parameters for your airport names and the
amount. Using prepared statement and pst.setString(1,trip.getOutgoingAirport());
> Am 06.01.2016 um 16:30 schrieb Emanuele Ianni <[email protected]>:
>
> Hello!
>
> One of my bolt in Storm is a module that adds edge on Neo4j. If I use
> parallelism I have duplicates. Here's the code:
>
> try {
> Connection connection = getNeo4jConnection();
> try {
> Trip trip = (Trip) tuple.getValueByField("trip");
> try (Statement stmt = connection.createStatement()) {
> String query = "MATCH (u:Airport {name:'" +
> trip.getOutgoingAirport() + "'}), (r:Airport {name:'"
> +
> trip.getIngoingAirport() + "'})" +
> " CREATE UNIQUE (u)-[:FLIGHT_TO { amount: '"+ 1 +"'
> }]->(r)";
> System.out.println("QUERY " + query);
> stmt.execute(query);
> }
> } finally {
> connection.close();
> }
> } catch (SQLException e) {
> System.out.println(e.getMessage());
> }
>
> collector.ack(tuple);
>
> I'm experimenting so don't mind the ugly try catch, it's not production code.
> The connection is created with:
>
> I'm experimenting so don't mind the ugly try catch, it's not production code.
> The connection is created with:
>
> private static Connection getNeo4jConnection() throws SQLException {
> Driver driver = new org.neo4j.jdbc.Driver();
> Properties properties = new Properties();
> properties.put("user", "neo4j");
> properties.put("password", "neo4j");
> String url = "jdbc:neo4j://localhost:7474/";
> return driver.connect(url, properties);
> }
> As you can see a query is something like: MATCH (u:Airport {name:'CIA'}),
> (r:Airport {name:'STN'}) CREATE UNIQUE (u)-[:FLIGHT_TO { amount: '1' }]->(r).
> Now I would have expected, as you can read on the docs, that neo4j has acid
> transaction so I would assume that I had no duplicates but I have them when
> using concurrent bolts. I would assume it's a racing condition because if I
> use 1 thread, it doesn't happen.
>
> Could you help me to figure out what I'm doing wrong?
>
> The version is 2.3.1
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Neo4j" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected]
> <mailto:[email protected]>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
--
You received this message because you are subscribed to the Google Groups
"Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.