# beginNamedQueryTransaction questions

**URL:** <https://forum.inductiveautomation.com/t/beginnamedquerytransaction-questions/78788>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [September 5, 2023, 8:48pm UTC](https://forum.inductiveautomation.com/t/beginnamedquerytransaction-questions/78788 "2023-09-05T20:48:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpmccormick](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wpmccormick/32/13940_2.png) [@wpmccormick](https://forum.inductiveautomation.com/u/wpmccormick)\
**Post date:** [September 5, 2023, 8:48pm UTC](https://forum.inductiveautomation.com/t/beginnamedquerytransaction-questions/78788/1 "2023-09-05T20:48:14Z")

</div>

What are the implications of beginning a named query transaction, and having some system.db.runNamedQuery calls WITH tx, and others without? If the WITHOUT calls are SELECT (read only) queries, I would think it shouldn't having any bearing on the transaction. If the WITHOUT calls are UPDATE queries, I would expect that the DB updates would happen outside of the transaction.

If the runNamedQuery is called with tx=None, is that the same as not including the param in the sig?

When I do supply the tx param (to even scalar/SELECT queries) I'm getting a ProjectNotFoundException.

The same tx provided to a previous UPDATE NQ produces no exception.

I'm using system.util.getProjectName() to get the project name and it's a Gateway script.

Thanks! Cheers!

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [September 6, 2023, 2:45pm UTC](https://forum.inductiveautomation.com/t/beginnamedquerytransaction-questions/78788/2 "2023-09-06T14:45:32Z")

</div>

A lot of good questions here.

Just calling `system.db.beginNamedQueryTransaction()` doesn't do much. We create an object that holds the information you pass in, create a transaction number, and add it to an executor map. We don't contact the database yet because a transaction (often? always?) keeps a db connection tied up, so we wait until something of interest happens to do that.

Depending on the isolation level you set when you begin the transaction, other transactions (single statements in SQL are basically transactions even if they aren't wrapped in a transaction) may or may not affect the transaction in question. The [Oracle docs on transactions](https://docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html) have a decent summary of isolation levels, but unless you have a special need, I'd say just leave your isolation level at the default, which should be `serializable`. That will give you the behavior it sounds like you're looking for.

If you call `runNamedQuery` with any null or blank value for tx, that's the same as not including the parameter.

I'd need more information on the `ProjectNotFoundException` to be able to help you with that. Maybe a short example script that demonstrates the problem.

---

<div class="post-metadata">

**Author:** ![wpmccormick](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wpmccormick/32/13940_2.png) [@wpmccormick](https://forum.inductiveautomation.com/u/wpmccormick)\
**Post date:** [September 6, 2023, 8:10pm UTC](https://forum.inductiveautomation.com/t/beginnamedquerytransaction-questions/78788/3 "2023-09-06T20:10:48Z")

</div>

I have an idea of what the issue is: The first NQ in the transaction has an INSERT, which is then followed by an UPDATE into the same table using the SCOPE\_IDENTITY() from the INSERT to SET one of the values.

> INSERT INTO foo (fields) VALUES(values)
> 
> UPDATE foo  
> SET bar = 'foo' + cast (SCOPE\_IDENTITY() as varchar(10))  
> WHERE id = SCOPE\_IDENTITY()
> 
> SELECT SCOPE\_IDENTITY()

No exception is given when running this NQ (a Scalar Query Type), but the ProjectNotFound exception is thrown on the subsequent (even if it's a SELECT only).

Is this a known issue or will it require additional testing and/or proof?

I assume if I wrap the INSERT/UPDATE NQ in it's own BEGIN TRANSACTION, it will NOT be the same transaction as any formed with beginNamedQuery; that any rollback called wouldn't rollback the INSERT?

EDIT: If I remove the the UPDATE and final SELECT SCOPE\_IDENTITY(), and make the Query Type = Update, I still get the ProjectNotFound exception. Also defaulted isolationLevel and timeout; no joy.

---

<div class="post-metadata">

**Author:** ![wpmccormick](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/wpmccormick/32/13940_2.png) [@wpmccormick](https://forum.inductiveautomation.com/u/wpmccormick)\
**Post date:** [September 6, 2023, 9:09pm UTC](https://forum.inductiveautomation.com/t/beginnamedquerytransaction-questions/78788/4 "2023-09-06T21:09:18Z")

</div>

I think the issue is that I need to supply the project name to NQ. I suspect that when it's running outside of a transaction, and sill on the gateway, it's able to figure out the project name. I'll test later ...

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [September 6, 2023, 10:24pm UTC](https://forum.inductiveautomation.com/t/beginnamedquerytransaction-questions/78788/5 "2023-09-06T22:24:45Z")

</div>

Yes, there are cases where if it can figure out the project name (e.g. a Perspective session) it will supply that for you.
