# Catching transaction timeouts

**URL:** <https://forum.inductiveautomation.com/t/catching-transaction-timeouts/16025>\
**Category:** Ignition\
**Created:** [November 16, 2017, 4:49pm UTC](https://forum.inductiveautomation.com/t/catching-transaction-timeouts/16025 "2017-11-16T16:49:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan\_Bridgeman](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dan_bridgeman/32/3610_2.png) [@Dan\_Bridgeman](https://forum.inductiveautomation.com/u/Dan_Bridgeman)\
**Post date:** [November 16, 2017, 4:49pm UTC](https://forum.inductiveautomation.com/t/catching-transaction-timeouts/16025/1 "2017-11-16T16:49:54Z")

</div>

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.

`import java.lang.Exception
try:
    txID = system.db.beginTransaction(database, isolationLevel, timeout)
    <...do a bunch of database stuff...>
    system.db.commitTransaction(txID)
except java.lang.Exception, err:
    import StringIO, traceback
    fp = StringIO.StringIO()
    traceback.print_exc(file=fp)
    logger.errorf("err=%", str(err))
    logger.errorf("err.getCause()=%s", str(err.getCause()))
    logger.errorf("fp.getvalue()=%s", fp.getvalue())
    system.db.rollbackTransaction(txID)
excpet Exception, err:
    <...similar logging for Python exceptions....>
finally:
   closeTransaction(txID)`

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?

Thanks,  
Dan

---

<div class="post-metadata">

**Author:** ![drewdin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/drewdin/32/3363_2.png) [@drewdin](https://forum.inductiveautomation.com/u/drewdin)\
**Post date:** [March 23, 2018, 2:13pm UTC](https://forum.inductiveautomation.com/t/catching-transaction-timeouts/16025/2 "2018-03-23T14:13:43Z")

</div>

Were you able to fix this issue? Just curious to see how you handled the exception.

Thanks,  
Drew

---

<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:** [March 23, 2018, 4:13pm UTC](https://forum.inductiveautomation.com/t/catching-transaction-timeouts/16025/3 "2018-03-23T16:13:53Z")

</div>

I would just nest the closeTransaction in a nested try with a “pass” in the except clause. There’s no further handling you can or should do if that fails.
