1

While I was looking over the documentation for transactions I came accross on this example:

var tx = session.beginTransaction();
tx.run("MERGE (bob:Person {name : {nameParam} }) RETURN bob.name AS name", {nameParam: 'Bob'})
  .subscribe({
    onNext: function (record) {
      console.log(record.get('name'));
    },
    onCompleted: function () {
      session.close();
    },
    onError: function (error) {
      console.log(error);
    }
  });

//decide if the transaction should be committed or rolled back
var success = false;

if (success) {
  tx.commit()
    .subscribe({
      onCompleted: function () {
        // this transaction is now committed 
      },
      onError: function (error) {
        console.log(error);
      }
    });
} else {
  //transaction is rolled black and nothing is created in the database
  console.log('rolled back');
  tx.rollback();
}

But on snippet above it does not seem to change the success somehow I mean how it does determine whetherthe transaction sucessfully executed or not the success variable does not change the value at all.

Dimitrios Desyllas
  • 9,082
  • 15
  • 74
  • 164

1 Answers1

1

It's because you are calling session.close() on the onCompleted callback function.

When you close a session, it will automatically commit all the underlying opened transactions.

Moreover on your example, you are not waiting that the tx.run promise is finished or not. you should do it on the section decide if the transaction should be committed or rolled back

So you should do something like that :

    var driver = neo4j.v1.driver("bolt://localhost",  neo4j.v1.auth.basic("neo4j", "neo4j"), { encrypted:false });
    var session = driver.session();
    // run statement in a transaction
    var tx = session.beginTransaction();
    tx.run("MERGE (bob:Person {name : {nameParam} }) RETURN bob.name AS name", {nameParam: 'Bob'})
      .subscribe({
        onNext: function (record) {
          console.log(record.get('name'));
        },
        onCompleted: function () {
          console.log('statement completed')
        },
        onError: function (error) {
          console.log(error);
        }
      });

    //decide if the transaction should be committed or rolled back
    var success = true;

    if (success) {
      tx.commit()
        .subscribe({
          onCompleted: function () {
            // this transaction is now committed 
          },
          onError: function (error) {
            console.log(error);
          }
        });
    } else {
      //transaction is rolled black and nothing is created in the database
      console.log('rolled back');
      tx.rollback();
    }
    session.close();

PS: I will contact the dev team to update the example

logisima
  • 7,340
  • 1
  • 18
  • 31
  • So the `var success = true;` will be updated on a custom piece of code I need to develop, that decides whether the transaction will be completed or not right? – Dimitrios Desyllas Jan 18 '18 at 21:27
  • Furthermore on the snippet seems that is no clear that the ` session.close()` gets called on any callback at all. Is is getting called outside of any promice at the code snippet. – Dimitrios Desyllas Jan 18 '18 at 21:32
  • For your first question : yes you can do what you want ! For the second quesion, the `session.close()` is just called on `onCompleted` callback, not on the other. – logisima Jan 19 '18 at 08:34
  • So the piece of code that start from `var success = true;` and end into ` session.close();` right? – Dimitrios Desyllas Jan 22 '18 at 12:19