I’m working on a script which includes a database transaction and uses a try/except to make sure that the transactions gets rolled back and closed sanely when an exception is caught.
txID = system.db.beginTransaction(database, isolationLevel, timeout)
<...do a bunch of database stuff...>
except java.lang.Exception, err:
import StringIO, traceback
fp = StringIO.StringIO()
excpet Exception, err:
<...similar logging for Python exceptions....>
The problem I’m having is that when the transaction times out, it is already closed when it reaches the “finally” section of my code. Then I end up getting a new java.lang.Exception for trying to execute system.db.closeTransaction() on a transaction which is already closed. (This is a little confusing to me, as it doesn’t complain when I run system.db.rollbackTransaction() on it before then.)
Is there some way that I can examine the transaction to see if it is still open before I try to close it? Or should I be examining the java.lang.Exception when I catch it to see if the problem was a timeout?
If it’s the latter, any guidelines for how best to be sure it’s a timeout? From the logs I captured, I can see that the cause of the general Exception is a GatewayException, which was itself cause by a SQLException. Either of those could be raised for reasons that aren’t a timeout, though, so I’m not sure what I could do besides parse some of the text. What my logging shows:
ERROR [sharedDbTx-AWT-EventQueue-2] err=java.lang.Exception: Error executing system.db.runPrepUpdate(INSERT INTO dbo.foo (a, b, c) VALUES (?, ?, ?), [1, 2, 3], , f9860da0-3a72-4028-9695-162163c90636, false, false)
ERROR [sharedDbTx-AWT-EventQueue-2] err.getCause()=com.inductiveautomation.ignition.client.gateway_interface.GatewayException: Connection Error: Transaction "f9860da0-3a72-4028-9695-162163c90636" is closed.
ERROR [sharedDbTx-AWT-EventQueue-2] fp.getvalue()=Traceback (most recent call last):
File "<module:shared.db.tx>", line 78, in handleTransaction
File "<custom-function saveCheckSheet>", line 24, in saveCheckSheet
File "<custom-function saveToCheckSheet>", line 26, in saveToCheckSheet
Exception: java.lang.Exception: Error executing system.db.runPrepUpdate(INSERT INTO dbo.foo(a, b, c) VALUES (?, ?, ?), [1, 2, 3], , f9860da0-3a72-4028-9695-162163c90636, false, false)
I supposed I could parse that and find that the transaction is closed, but that seems a bit of a hack to me. Does anyone have any suggestions for a better approach?