# Issue using transactions with named queries?

**URL:** <https://forum.inductiveautomation.com/t/issue-using-transactions-with-named-queries/56276>\
**Category:** Ignition\
**Tags:** ignition80\
**Created:** [February 17, 2022, 7:10pm UTC](https://forum.inductiveautomation.com/t/issue-using-transactions-with-named-queries/56276 "2022-02-17T19:10:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [February 17, 2022, 7:10pm UTC](https://forum.inductiveautomation.com/t/issue-using-transactions-with-named-queries/56276/1 "2022-02-17T19:10:00Z")

</div>

Trying to run some named queries in a `Form` class. I am getting a datasource error and I have no idea why.

Heres the portion of my code that is running

```auto
class Form(object):
	"""
	Abstract class to be implemented by script modules in forms package.
	Properties:
		NamedQueries: for subclasses should be under the forms folder of named queries, 
			with a sub folder matching the name of the class. For now, lets just leave it strictly like this to enforce some organization...
		idx: int, used for updating only, or will be assigned during after creation
		newData: dictionary, used for insert statements or update statements
		oldData: dictionary, can be provided for update statements in the event comparisons are needed
		defaultValues: dictionary, used to fill in blank columns of newData for inserts
		logger: logger, from system.util.getLogger
		tx: str, transaction id for running of queries
	"""
	namedQueryFolder = ''
	idx = None
	newData = None
	oldData = None
	defaultValues = {}
	logger = None
	# Transaction information
	tx = None
	isolationLevel = system.db.SERIALIZABLE
	timeout = 5000
	
	def __init__ (self):
		self.calculateNamedQueries()
		self.getLogger()
	
	def _insert(self):
		createNamedQuery = self.namedQueryFolder + '/create'
		print 'createNamedQuery:' + createNamedQuery
		print 'newData: ' + str(self.newData)
		print 'tx: ' + str(self.tx)
		self.idx = system.db.runNamedQuery(createNamedQuery, self.newData, tx=self.tx, getKey=1)

	def create(self, newData):
		self.logger.info("running create")
		self.newData = newData
		self.setDefaultsNew()
		self.modifyNew()
		self.validateNew()
		self.sanitizeNew()
		self.inputDefaultValues()
		self.tx = system.db.beginNamedQueryTransaction(self.isolationLevel, self.timeout)
		self._insert()
		self.afterInsert()
		system.db.commitTransaction(self.tx)
		system.db.closeTransaction(self.tx)
		self.updateUIAfterInsert()
		self.logger.info("Created new record with idx %i"%(self.idx))

```

The last line `self.idx = system.db.runNamedQuery(createNamedQuery, self.newData, tx=self.tx, getKey=1)` in `def _insert(self)` is throwing the following error and I have no idea why -

```auto
createNamedQuery:forms/Customer/create
newData: {'defaultProjectManager': -1, 'description': u'abcde', 'active': True, 'customerPath': u'abcde', 'creditHold': False, 'viewable': True, 'unpaidInvoiceTotal': 0.0, 'paidOnUnpaidInvoice': 0.0, 'name': u'abcde', 'creditLimit': 0.0, 'activeInvoiceFlags': 0, 'companyTypeId': 1, 'discountId': 5}
tx: b4fcf72b-20ce-4028-9fb8-0001894a5479
13:58:54.142 [AWT-EventQueue-0] ERROR com.inductiveautomation.ignition.client.util.gui.ErrorUtil - <HTML>Error executing script for event:&nbsp;<code><b>actionPerformed</b></code><BR>on component:&nbsp;<code><b>cmdOK</b></code>.
com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last):
  File "<event:actionPerformed>", line 31, in <module>
  File "<module:classes.Form>", line 94, in create
  File "<module:classes.Form>", line 74, in _insert
	at com.inductiveautomation.ignition.client.gateway_interface.GatewayInterface.newGatewayException(GatewayInterface.java:351)
	at com.inductiveautomation.ignition.client.gateway_interface.GatewayInterface.sendMessage(GatewayInterface.java:325)
	at com.inductiveautomation.ignition.client.gateway_interface.GatewayInterface.sendMessage(GatewayInterface.java:278)
	at com.inductiveautomation.ignition.client.gateway_interface.GatewayInterface.invoke(GatewayInterface.java:934)
	at com.inductiveautomation.factorypmi.application.db.namedqueries.ClientNamedQueryManager.execute(ClientNamedQueryManager.java:30)
	at com.inductiveautomation.ignition.client.script.ClientDBUtilities.runNamedQuery(ClientDBUtilities.java:116)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.base/java.lang.reflect.Method.invoke(Unknown Source)
com.inductiveautomation.ignition.client.gateway_interface.GatewayException: com.inductiveautomation.ignition.client.gateway_interface.GatewayException: java.sql.SQLException: Datasource "8" does not exist in this Gateway.

```

If I change the problematic line to `self.idx = system.db.runNamedQuery(createNamedQuery, self.newData, getKey=1)` and get rid of the lines starting/closing the transaction, it works fine, so it must be related to my `tx=self.tx` but I don’t know what I am doing wrong here. Any thoughts?

Another odd thing to me is that with `runNamedQuery` you don’t get to pass in the datasource into the funciton per the docs [system.db.runNamedQuery - Ignition User Manual 8.0 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC80/system.db.runNamedQuery). My named query definitions all have the default database connection set and as mentioned work fine when I do it without the attempting to do it in a transaction.

The only place I can see an 8 even potentially coming from is system.db.SERIALIZABLE which is 8, but I don’t know how that that would happen.

---

<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:** [February 17, 2022, 7:16pm UTC](https://forum.inductiveautomation.com/t/issue-using-transactions-with-named-queries/56276/2 "2022-02-17T19:16:26Z")

</div>

I recall a bug in transactions for Named Queries. Pretty sure it was fixed some time in v8.1.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [February 17, 2022, 7:17pm UTC](https://forum.inductiveautomation.com/t/issue-using-transactions-with-named-queries/56276/3 "2022-02-17T19:17:20Z")

</div>

Ah ok that makes sense. I’m on 8.0.17. Guess it’s time to upgrade.

---

<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:** [February 17, 2022, 7:18pm UTC](https://forum.inductiveautomation.com/t/issue-using-transactions-with-named-queries/56276/4 "2022-02-17T19:18:37Z")

</div>

Yup. And discussion here:

> [@\[IGN-3829\] Error executing system.db.commitTransaction](https://forum.inductiveautomation.com/t/ign-3829-error-executing-system-db-committransaction/49595):
>
> When I use a named query transaction, but do not actually run any queries against it I receive an error. If I do the same thing using system.db.beginTransaction I do not get an error. Any ideas on how to get around this? txId = system.db.beginNamedQueryTransaction("myDatabaseConnection") system.db.commitTransaction(txId) system.db.closeTransaction(txId) Java Traceback: Traceback (most recent call last): File "\<input\>", line 28, in \<module\> at com.inductiveautomation.ignition.common.scrip…

---

<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:** [February 17, 2022, 7:22pm UTC](https://forum.inductiveautomation.com/t/issue-using-transactions-with-named-queries/56276/5 "2022-02-17T19:22:43Z")

</div>

Hmm. I’m not seeing this in the changelogs…

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [February 17, 2022, 7:23pm UTC](https://forum.inductiveautomation.com/t/issue-using-transactions-with-named-queries/56276/6 "2022-02-17T19:23:41Z")

</div>

@kcollins Hey, a thread you were involved in last year regarding transactions for named queries was mentioned. Do you know if this something that has been fixed and if so do you know what version?

---

<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:** [February 18, 2022, 3:33pm UTC](https://forum.inductiveautomation.com/t/issue-using-transactions-with-named-queries/56276/7 "2022-02-18T15:33:25Z")

</div>

3829 hasn’t been picked up yet. Paul has escalated it, but if you want it even higher in the list contact support and have them attach a support ticket to it.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [February 18, 2022, 3:37pm UTC](https://forum.inductiveautomation.com/t/issue-using-transactions-with-named-queries/56276/8 "2022-02-18T15:37:15Z")

</div>

Ok thank you. It is not urgent, but I am in the midst of refactoring a project to all namedQueries and at the same time started putting things into transaction groups to have some atomicity to the database operations. Project still works as is so everyone is happy but I would definitely love this functionality for any fresh future projects.
