# SQL script cause database to lock block

**URL:** <https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [September 16, 2023, 1:37am UTC](https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246 "2023-09-16T01:37:24Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [September 17, 2023, 10:53pm UTC](https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246/21 "2023-09-17T22:53:21Z")

</div>

You can use `system.db.runNamedQuery()` it will work.

---

<div class="post-metadata">

**Author:** ![Eugene49](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@Eugene49](https://forum.inductiveautomation.com/u/Eugene49)\
**Post date:** [September 18, 2023, 3:08pm UTC](https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246/22 "2023-09-18T15:08:27Z")

</div>

Is there a way to programmatically get the Project Name and Default database name?

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [September 18, 2023, 3:11pm UTC](https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246/23 "2023-09-18T15:11:43Z")

</div>

You can use `system.util.getProjectName()` to get the project name, I don't know of an easy way to get the Default Database name, if there is even one set. I _ **always** _ set the database name explicitly.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 18, 2023, 3:21pm UTC](https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246/24 "2023-09-18T15:21:09Z")

</div>

You shouldn't need to get the default database name for use with `runPrepUpdate` and friends. Anywhere you can't omit it, there is no default database to get.

By contrast, IA royally screwed up the order of arguments to `runNamedQuery` such that you cannot omit the project name in gateway scope even when in a scope where it **could have** been implied, so you should use `.getProjectName()` for those cases.

{Typo spotted by Paul.}

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [September 18, 2023, 3:30pm UTC](https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246/25 "2023-09-18T15:30:20Z")

</div>

> [@pturmel](#):
>
> By contrast, IA royally screwed up the order of arguments to `runPrepQuery` such that you cannot omit the project name in gateway scope even when in a scope where it **could have** been implied, so you should use `.getProjectName()` for those cases.

`runPrepQuery`, or `runNamedQuery`?

---

<div class="post-metadata">

**Author:** ![Eugene49](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@Eugene49](https://forum.inductiveautomation.com/u/Eugene49)\
**Post date:** [September 18, 2023, 3:33pm UTC](https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246/26 "2023-09-18T15:33:49Z")

</div>

> [@pturmel](#):
>
> ou shouldn't need to get the default database name for use with `runPrepUpdate` and friends.

um... on the beginNamedQueryTransaction, Project Name and Database are required, as it threw an error when omitted.

I am using the whole runNamedQuery gang, but only beginNamedQueryTransaction needs Project Name and Database Name.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [September 18, 2023, 4:42pm UTC](https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246/27 "2023-09-18T16:42:53Z")

</div>

> [@Eugene49](#):
>
> I am using the whole runNamedQuery gang, but only beginNamedQueryTransaction needs Project Name and Database Name.

To my knowledge, there is no supported way to get the default data source. I'm sure if you went through the trouble of getting a GatewayContext, there is a way to get to it. But that is a lot more trouble then just typing in the database name. Why is just using a constant database name unacceptable?

---

<div class="post-metadata">

**Author:** ![Eugene49](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@Eugene49](https://forum.inductiveautomation.com/u/Eugene49)\
**Post date:** [September 18, 2023, 4:44pm UTC](https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246/28 "2023-09-18T16:44:12Z")

</div>

Thank's a lot for the input.  
I have since split multiple queries into one query per NamedQuery. That said, previously, I have two inserts, and one select in Named Query, and it work just fine so far, including the Begin and Commit statement contained in the NamedQuery..

I have my try-except, commit-rollback logic in place.  
do you guys know how to get the error message from (JDBC and Python) the try block?

I will pass this message to Popup parameter.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [September 18, 2023, 4:52pm UTC](https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246/29 "2023-09-18T16:52:11Z")

</div>

> [@Eugene49](#):
>
> including the Begin and Commit statement contained in the NamedQuery..
> 
> I have my try-except, commit-rollback logic in place.

If you are not using Ignition to create the transaction, then Ignition will not be able to rollback the transaction. Don't do this. Create the transaction in ignition with the rollback/commit logic in Ignition, or do it in a stored procedure if you want it to be purely handled by the DB. Don't do it in both places.

You said you are using `system.db.beginNamedQueryTransaction()` if that is the case, then you are effectively wrapping a transaction with a transaction (not good).

Multiple queries inside of a named query is just asking for trouble (Even if it works currently). On top of that it makes it difficult to troubleshoot should it come to that. Split them out completely. If for some reason you insist on multiple statements being executed in one call then you should use a stored procedure.

Use `java.lang.Throwable` to catch errors.

> [@system.db.runUpdateQuery Exception Handling](https://forum.inductiveautomation.com/t/system-db-runupdatequery-exception-handling/68237/2):
>
> Normal jython try: -- except: won't trap java exceptions. You often need two except: clauses, the first being except java.lang.Throwable, t: or similar. Some [related discussions](https://forum.inductiveautomation.com/search?q=Throwable%20%40pturmel). Note that when catching such exceptions in Vision Client scope or Designer scope, the real SQL exception is not available--it won't serialize and the JDBC classes don't exist in those scopes anyways.

---

<div class="post-metadata">

**Author:** ![Eugene49](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@Eugene49](https://forum.inductiveautomation.com/u/Eugene49)\
**Post date:** [September 18, 2023, 6:48pm UTC](https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246/30 "2023-09-18T18:48:28Z")

</div>

Sorry for confusion.  
I have since then split the multiple query into individual, one query per runNamed function.

Below is my script:

```auto
numQuery = 4
result = [None]*numQuery
queryPath = [None]*numQuery
param = [None]*numQuery

queryPath[0]="Insert_Into_table1"
queryPath[1]="Insert_Into_table2"
queryPath[2]="Insert_Into_table3"
queryPath[3]="Select_From_table4"

tx = system.db.beginNamedQueryTransaction(system.util.getProjectName(), Project.database, timeout=5000)
	
try:
	#Edit This
	result[0] = system.db.runNamedQuery(path=queryPath[0], parameters=param[0], tx=tx, getKey=1)
	Id = result[0]
	
	param[1]['Id'] = Id 
	result[1] = system.db.runNamedQuery(path=queryPath[1], parameters=param[1], tx=tx, getKey=0)
	
	param[2]['Id'] = Id
	result[2] = system.db.runNamedQuery(path=queryPath[2], parameters=param[2], tx=tx, getKey=0)
	
	param[3]['Id'] = Id
	result[3] = system.db.runNamedQuery(path=queryPath[3], parameters=param[3], tx=tx, getKey=0)
except:
	isError = True
	system.db.rollbackTransaction(tx)
else:
	system.db.commitTransaction(tx)
finally:
	system.db.closeTransaction(tx)
	

```

---

<div class="post-metadata">

**Author:** ![Eugene49](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@Eugene49](https://forum.inductiveautomation.com/u/Eugene49)\
**Post date:** [September 18, 2023, 6:56pm UTC](https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246/31 "2023-09-18T18:56:31Z")

</div>

> [@lrose](#):
>
> Why is just using a constant database name unacceptable?

I will be working with production database.  
I have a development database.  
I copy codes from development script, into production script.  
I might forget to change the database path..  
-\> since there is no way to get default database programatically, so I used python variable instead.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [September 18, 2023, 7:13pm UTC](https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246/32 "2023-09-18T19:13:11Z")

</div>

Okay, I think you want a script like this.

Note: I don't think that the script as you have posted it will run, as `param[1]` is not defined anywhwere as a dictionary.

```auto
results = []
queryPaths = ['Insert_Into_Table1','Insert_Into_Table2','Insert_Into_Table3','Select_From_Table4']

tx = system.db.beginNamedQueryTransaction(system.util.getProjectName(),Project.database, timeout=5000)

try:
    id = system.db.runNamedQuery(path=queryPaths[0], tx=tx,getKey=True)
    results = [system.db.runNamedQuery(path=path,parameters={'Id':id}, tx=tx) for path in queryPaths[1:]]
    results.insert(0,id)
except Exception e:
    isError = True
    system.db.rollbackTransaction(tx)
except Throwable t:
    isError = True
    system.db.rollbackTransaction(tx)
else:
    system.db.commitTransaction(tx)
finally:
    system.db.closeTransaction(tx)

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 18, 2023, 7:16pm UTC](https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246/33 "2023-09-18T19:16:33Z")

</div>

> [@Eugene49](#):
>
> I will be working with production database.  
> I have a development database.  
> I copy codes from development script, into production script.

This is a really bad practice. Use one database connection name for both development and production environments. In your development environment, it would point at a development DB instead of the production DB.

---

<div class="post-metadata">

**Author:** ![Eugene49](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@Eugene49](https://forum.inductiveautomation.com/u/Eugene49)\
**Post date:** [September 18, 2023, 7:58pm UTC](https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246/34 "2023-09-18T19:58:46Z")

</div>

yes, you are right. good point.

---

<div class="post-metadata">

**Author:** ![Eugene49](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@Eugene49](https://forum.inductiveautomation.com/u/Eugene49)\
**Post date:** [September 21, 2023, 2:44pm UTC](https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246/35 "2023-09-21T14:44:53Z")

</div>

What is Exeception e?  
What is Throwable t?

how do I get the error message?

does avove covers all possibe error?

is there a general catch where it catch any error, and capture the error message?

Thanks.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [September 21, 2023, 2:47pm UTC](https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246/36 "2023-09-21T14:47:04Z")

</div>

`Exception` are python's errors.  
`Throwable` are java errors, which are raised by database operations.  
Catching both should cover pretty much everything, but you could add a bare `except:` and get the error from the stacktrace... But frankly it's not necessary.  
What's not caught will end up in the logs anyway.  
By the way, I usually reraise errors at some point to make sure they're logged:

```python
except Exception as e:
    handle_exception_whichever_way_you_like
    raise

```

> [@lrose](#):
>
> ```auto
> except Exception e:
> isError = True
> system.db.rollbackTransaction(tx)
> except Throwable t:
> isError = True
> system.db.rollbackTransaction(tx)
> 
> ```

You can do this:

```python
except (Expection, Throwable) as e:
    # error handling

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 21, 2023, 2:51pm UTC](https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246/37 "2023-09-21T14:51:19Z")

</div>

> [@pascal.fragnoud](#):
>
> `except (Expection, Throwable) as e:`

I don't recommend this. `Exception` is Jython's type, which is unrelated to java's `Throwable` type. Java's `Exception` is a subclass of `Throwable` but Jython's `Exception` is not. In the above, you would have to test the type of `e` for anything more complex then stringifying its message.

Catching them separately gives you more options for logging with complete backtraces, particularly if you use `PythonAsJavaException` from [later.py](https://www.automation-pros.com/ignition/later.py).

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [September 21, 2023, 2:52pm UTC](https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246/38 "2023-09-21T14:52:37Z")

</div>

Well yes, if you handle the errors differently.  
But if the handling is exactly the same, I don't see a reason to duplicate the handling code.

---

<div class="post-metadata">

**Author:** ![Eugene49](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@Eugene49](https://forum.inductiveautomation.com/u/Eugene49)\
**Post date:** [September 21, 2023, 4:39pm UTC](https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246/39 "2023-09-21T16:39:00Z")

</div>

Hi,

I searched but did not find.

error: global name 'Throwable' is not defined.

how do you fix the above error.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [September 21, 2023, 4:40pm UTC](https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246/40 "2023-09-21T16:40:20Z")

</div>

`from java.lang import Throwable`

[Previous page](https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246.md?page=1)

[Next page](https://forum.inductiveautomation.com/t/sql-script-cause-database-to-lock-block/79246.md?page=3)
