# Difference between session.run and session.readTransaction or session.writeTransaction

**URL:** <https://community.neo4j.com/t/difference-between-session-run-and-session-readtransaction-or-session-writetransaction/14720>\
**Category:** Javascript\
**Created:** [February 14, 2020, 1:08pm UTC](https://community.neo4j.com/t/difference-between-session-run-and-session-readtransaction-or-session-writetransaction/14720 "2020-02-14T13:08:53Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jariwals](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jariwals/32/6312_2.png) [@jariwals](https://community.neo4j.com/u/jariwals)\
**Post date:** [February 14, 2020, 1:08pm UTC](https://community.neo4j.com/t/difference-between-session-run-and-session-readtransaction-or-session-writetransaction/14720/1 "2020-02-14T13:08:53Z")

</div>

In Javascript driver, what is the difference between running Cypher queries using session.run() and session.readTransaction((tx) =\> {tx.run()}) or session.writeTransaction((tx) =\> {tx.run()}) ?

---

<div class="post-metadata">

**Author:** ![david\_allen](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/david_allen/32/13090_2.png) [@david\_allen](https://community.neo4j.com/u/david_allen)\
**Post date:** [February 14, 2020, 3:21pm UTC](https://community.neo4j.com/t/difference-between-session-run-and-session-readtransaction-or-session-writetransaction/14720/2 "2020-02-14T15:21:36Z")

</div>

This is a great question, a common one, and a difference with big consequences, so let's break it down.

Session.run is the simplest, easiest form, something called an "[autocommit transaction](https://neo4j.com/docs/driver-manual/current/session-api/simple/#driver-simple-autocommit-transactions)". While it's an easy way to get started, it's not recommended in production. You should instead use readTransaction and writeTransaction, for a bunch of reasons.

1. You should signal to the driver whether you're trying to read or write. In Neo4j clustered setups, when you're using the `bolt+routing://` or `neo4j://` schemes, this tells the driver how to appropriately route your query to the right machine. If you want to know more about this topic, have a look at [Querying Neo4j Clusters](https://medium.com/neo4j/querying-neo4j-clusters-7d6fde75b5b4).

2. With `session.run`, note you're passing a cypher query. With the other two, you're passing a _function that performs the work of the transaction_. This is a critical difference. One lets you run a single query. In the other, you might have a function that does a number of different sub-queries, all of which you want to wrap. up into a single overall TX that either succeeds or fails. See ([Transaction Management](https://neo4j.com/docs/java-reference/current/transaction-management/) for much more detail). In complex setups this comes up a lot that doing a particular update to your system might take 3 cypher queries, not 1. And you don't want to "partially succeed" you want either all 3 to go through, or none. To do something like that you want a tx work function.

**Bottom line** -- prefer the use of readTransaction and writeTransaction. Get into that habit and you'll always be in good shape.

---

<div class="post-metadata">

**Author:** ![jariwals](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jariwals/32/6312_2.png) [@jariwals](https://community.neo4j.com/u/jariwals)\
**Post date:** [February 14, 2020, 10:38pm UTC](https://community.neo4j.com/t/difference-between-session-run-and-session-readtransaction-or-session-writetransaction/14720/3 "2020-02-14T22:38:30Z")

</div>

Thanks for the clarification, David!  
So, if I have a bunch of queries that should be performed sequentially (dependent queries) then how should I write them in a transaction so that the second one is performed only when the first one is successfully completed?  
I'd highly appreciate it if you could direct me to any such detailed examples to understand and implement the best-practices in this regard.

Thank you,  
Sheekha

---

<div class="post-metadata">

**Author:** ![david\_allen](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/david_allen/32/13090_2.png) [@david\_allen](https://community.neo4j.com/u/david_allen)\
**Post date:** [February 17, 2020, 4:45pm UTC](https://community.neo4j.com/t/difference-between-session-run-and-session-readtransaction-or-session-writetransaction/14720/4 "2020-02-17T16:45:32Z")

</div>

Check the "Transactions Functions" documentation in the driver manual, they have examples in all supported languages.

> **[Sessions and transactions - Neo4j Driver Manual](https://neo4j.com/docs/driver-manual/1.7/sessions-transactions/#driver-transactions-transaction-functions)**
>
> This section describes how to create units of work and provide a logical context for that work.

Specifically, you can use tx.run to do the things inside of the transaction sequentially that you need.

---

<div class="post-metadata">

**Author:** ![jariwals](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jariwals/32/6312_2.png) [@jariwals](https://community.neo4j.com/u/jariwals)\
**Post date:** [February 18, 2020, 10:00pm UTC](https://community.neo4j.com/t/difference-between-session-run-and-session-readtransaction-or-session-writetransaction/14720/5 "2020-02-18T22:00:35Z")

</div>

I tried the following code and I couldn't get results from the database -

> session.readTransaction((transaction) =\> {  
> var getUserProfileResult = transaction.run("match (u:user{id:'" + userid + "'}) return u");  
> result = { ...result, userProfile: getUserProfileResult.summary }  
> var getUserLanguagesResult = transaction.run("match (u:user{id:'" + userid + "'})-[k:knows]-\>(v) return u,k,v");  
> result = { ...result, languages: getUserLanguagesResult.summary };  
> var getUserFriendsResult = transaction.run("match (u:user{id:'" + userid + "'})-[f:friendOf]-\>(v) return u,f,v");  
> result = { ...result, friends: getUserFriendsResult.summary };  
> console.log('result is ', result);  
> return result  
> }).then(result =\> {  
> return res.json({ result });  
> }).catch(function(error) {  
> console.log("get user error: " + error);  
> }).finally((result) =\> {  
> session.close();  
> });

I get 'undefined' when I print the result.summary or result.records.  
Could you help me figure out what am I doing wrong here?

---

<div class="post-metadata">

**Author:** ![david\_allen](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/david_allen/32/13090_2.png) [@david\_allen](https://community.neo4j.com/u/david_allen)\
**Post date:** [February 19, 2020, 7:36pm UTC](https://community.neo4j.com/t/difference-between-session-run-and-session-readtransaction-or-session-writetransaction/14720/6 "2020-02-19T19:36:06Z")

</div>

You're not threading those promises. Each time you run transaction.run() you're creating a new promise, and they each need .then() and .catch() handlers.

---

<div class="post-metadata">

**Author:** ![jariwals](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jariwals/32/6312_2.png) [@jariwals](https://community.neo4j.com/u/jariwals)\
**Post date:** [February 20, 2020, 8:05pm UTC](https://community.neo4j.com/t/difference-between-session-run-and-session-readtransaction-or-session-writetransaction/14720/7 "2020-02-20T20:05:02Z")

</div>

I'm not sure if this is what you recommended.  
I'm only getting the result for txresult1. But I'm getting a tx2 error which says ''Cannot run statement, because transaction has already been successfully closed.''. Am I missing something here?  
Really appreciate all your help!

```auto
const getUserProfilePromise = session.readTransaction((transaction) => {
        transaction.run("match (u:user{id:'" + userid + "'}) return u")
            .then(txresult1 => {
                result = { ...result, userProfile: txresult1 };
                transaction.run("match (u:user{id:'" + userid + "'})-[k:knows]->(v) return u,k,v")
                    .then(txresult2 => {
                        result = { ...result, languages: txresult2 };
                        transaction.run("match (u:user{id:'" + userid + "'})-[f:friendOf]->(v) return u,f,v")
                            .then(txresult3 => {
                                result = { ...result, friends: txresult3 };
                            }).catch(txerror3 => {
                                console.log('error from tx3 - ', txerror3);
                        })
                    }).catch(txerror2 => {
                        console.log('tx2 error - ', txerror2);
                })
            }).catch(txerror1 => {
                console.log('tx1 error - ', txerror1);
        })
        return result
    }).then(result => {
        return res.json({ result });
    }).catch(function(error) {
        console.log("get user profile error 3213: " + error);
    }).finally((result) => {
        session.close();
    });
    
});

```

---

<div class="post-metadata">

**Author:** ![david\_allen](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/david_allen/32/13090_2.png) [@david\_allen](https://community.neo4j.com/u/david_allen)\
**Post date:** [February 20, 2020, 9:30pm UTC](https://community.neo4j.com/t/difference-between-session-run-and-session-readtransaction-or-session-writetransaction/14720/8 "2020-02-20T21:30:33Z")

</div>

You're running into some basic javascript promise threading issues here. You did get what I mean, but you're not returning your inner promises out, you're instead nesting them.

I.e. don't do this:

```auto
somePromise()
   .then(() => {
        somePromise2().then(() => {
            somePromise3();
        })
   })

```

This is called "Pyramid of doom" in JS land. 🙂

Instead do something like this:

```auto
somePromise()
   .then(() => {
       return somePromise2()
   })
   .then(() => {
       return somePromise3()
   })

```

When you don't "return" the promises, the async processing they're doing gets lost and doesn't resolve. As a result, you get to the bottom of your code and the TX gets closed before the inner stuff has finished executing.

---

<div class="post-metadata">

**Author:** ![jariwals](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jariwals/32/6312_2.png) [@jariwals](https://community.neo4j.com/u/jariwals)\
**Post date:** [February 21, 2020, 9:54pm UTC](https://community.neo4j.com/t/difference-between-session-run-and-session-readtransaction-or-session-writetransaction/14720/9 "2020-02-21T21:54:14Z")

</div>

That worked! 🙂  
Thanks a ton, David for the detailed code and explanation!  
Much appreciated!

---

<div class="post-metadata">

**Author:** ![mithun.das](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/mithun.das/32/8732_2.png) [@mithun.das](https://community.neo4j.com/u/mithun.das)\
**Post date:** [August 10, 2020, 4:11pm UTC](https://community.neo4j.com/t/difference-between-session-run-and-session-readtransaction-or-session-writetransaction/14720/10 "2020-08-10T16:11:37Z")

</div>

Hi sir, I am new to this, your answer helped me... I hv a couple of questions.

so i hv a workflow like -\> ` MATCH x where x.a=1 CREATE (x)-[has]->(y) SET x.b = y.c`

here do I hv to the break the workflow into READ and WRITE?  
But if i do that... how will i get x as a node from the READ query to create relationship with y in my WRITE query? Or hv I completely misunderstood the system?  
Can you please help?

---

<div class="post-metadata">

**Author:** ![david\_allen](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/david_allen/32/13090_2.png) [@david\_allen](https://community.neo4j.com/u/david_allen)\
**Post date:** [August 10, 2020, 4:59pm UTC](https://community.neo4j.com/t/difference-between-session-run-and-session-readtransaction-or-session-writetransaction/14720/11 "2020-08-10T16:59:44Z")

</div>

The entire query is either read or write. In your case, it's write. If you ever use the keywords MERGE or CREATE, it's a dead give-away that the entire query is a write query.

---

<div class="post-metadata">

**Author:** ![mithun.das](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/mithun.das/32/8732_2.png) [@mithun.das](https://community.neo4j.com/u/mithun.das)\
**Post date:** [August 10, 2020, 7:08pm UTC](https://community.neo4j.com/t/difference-between-session-run-and-session-readtransaction-or-session-writetransaction/14720/12 "2020-08-10T19:08:10Z")

</div>

great i will try this out today... and many thanks for finding the time to answer,....

---

<div class="post-metadata">

**Author:** ![yyakovson613](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/yyakovson613/32/217_2.png) [@yyakovson613](https://community.neo4j.com/u/yyakovson613)\
**Post date:** [November 9, 2021, 1:47pm UTC](https://community.neo4j.com/t/difference-between-session-run-and-session-readtransaction-or-session-writetransaction/14720/13 "2021-11-09T13:47:29Z")

</div>

Hi @david_allen. Thank you for this nice summary!

I've created a new node package [neo-forte](https://github.com/YizYah/neo-forte) to simplify working with the driver. I actually have put links in the README to this post of yours as well as your medium article about clusters.

The goal of `neo-forte` is to make running queries in code as simple as in the data browser. Fundamentally, `neo-forte` has a single function `run()`. But under the cover it runs transactions.

To enable selection between `readTransaction` and `writeTransaction`, I have a crude but generally effective approach of searching a query string for the reserved cypher update clause words ("create", "merge", "set", etc.). If any are found, it runs the query within a WRITE transaction, and otherwise READ. Of course, there are "false positives", so you can overwrite the automatic selection to specify READ or WRITE if you like. (That was part of the invaluable feedback I received from @antonio_barcelo.)

If you want to pass in a _function_ rather than a simple query and params, you must use the session transaction methods provided by the neo4j-driver.

If you have any suggestions for neo-forte, I'd really appreciate hearing them!
