Named Query Transaction Closed

Why when I run the following code in a gateway script I get an error on line 3 saying the transaction has been closed? If I run the same code in the script console it works fine.

com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File “”, line 3, in at com.inductiveautomation.ignition.gateway.db.namedquery.NamedQueryExecutor$NamedQueryInstance.execute(NamedQueryExecutor.java:413) at com.inductiveautomation.ignition.gateway.db.namedquery.NamedQueryExecutor.execute(NamedQueryExecutor.java:176) at com.inductiveautomation.ignition.gateway.db.namedquery.GatewayNamedQueryManager.execute(GatewayNamedQueryManager.java:91) at com.inductiveautomation.ignition.gateway.script.GatewayDBUtilities.runNamedQuery(GatewayDBUtilities.java:402) at jdk.internal.reflect.GeneratedMethodAccessor40.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) java.lang.Exception: java.lang.Exception: java.sql.SQLException: Transaction “cf24130f-799d-40ba-b1fa-830cb651e99d” is closed.