<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Nifi Execute Groovy Script in Support Questions</title>
    <link>https://community.cloudera.com/t5/Support-Questions/Nifi-Execute-Groovy-Script/m-p/293921#M216933</link>
    <description>&lt;P&gt;Hi All,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am facing issues with Execute Script processor. I am executing Groovy script which have oracle&amp;nbsp; DB in Execute Script processor but its not closing the DB connection.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Issues&lt;/STRONG&gt;: Database connection are not closing and its making Inactive connection in database.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The Groovy script we added proper open and close connections. Could you help fix this issues, also let me know where I am not correct .&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DB Connection Configuration is like that :&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 702px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/27127i16BC46AE617FF832/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Execute Script Process are configured as fallowing:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ExecuteScriptProcessor.JPG" style="width: 747px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/27128i830C25933164585E/image-size/large?v=v2&amp;amp;px=999" role="button" title="ExecuteScriptProcessor.JPG" alt="ExecuteScriptProcessor.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Groovy Script as following :&amp;nbsp;&lt;/P&gt;&lt;P&gt;import org.apache.commons.io.IOUtils&lt;BR /&gt;import org.apache.nifi.controller.ControllerService&lt;BR /&gt;import org.apache.nifi.processor.io.StreamCallback&lt;BR /&gt;import java.nio.charset.*&lt;BR /&gt;import groovy.sql.OutParameter&lt;BR /&gt;import groovy.sql.Sql&lt;BR /&gt;import oracle.jdbc.OracleTypes&lt;BR /&gt;import java.sql.ResultSet&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;def lookup = context.controllerServiceLookup&lt;BR /&gt;def dbServiceName = ConnectionPoolScriptExecution.value&lt;BR /&gt;def dbcpServiceId = lookup.getControllerServiceIdentifiers(ControllerService).find {&lt;BR /&gt;cs -&amp;gt; lookup.getControllerServiceName(cs) == dbServiceName&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;def conn = lookup.getControllerService(dbcpServiceId)?.getConnection();&lt;BR /&gt;def sql = new Sql(conn);&lt;/P&gt;&lt;P&gt;def flowFile = session.get()&lt;BR /&gt;if(!flowFile) return&lt;BR /&gt;//Session Values&lt;BR /&gt;strApplication = flowFile.getAttribute('application')&lt;BR /&gt;strUsername = flowFile.getAttribute('username')&lt;BR /&gt;strPassword = flowFile.getAttribute('password')&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;// SQL Query Creation and execution&lt;/P&gt;&lt;P&gt;def ValidOrNot&lt;/P&gt;&lt;P&gt;String sqlString = "select FN_HP_IS_VALID_APP('${strApplication}','${strUsername}','${strPassword}') as ValidOrNot from dual";&lt;BR /&gt;try {&lt;BR /&gt;rowNum = 0&lt;BR /&gt;sql.eachRow(sqlString, { dataResult -&amp;gt;&lt;BR /&gt;//println(dataResult.ValidOrNot)&lt;BR /&gt;ValidOrNot = dataResult.ValidOrNot&lt;BR /&gt;})&lt;/P&gt;&lt;P&gt;}&lt;BR /&gt;catch(Exception ex){&lt;BR /&gt;&lt;BR /&gt;conn?.close()&lt;BR /&gt;}&lt;BR /&gt;finally {&lt;/P&gt;&lt;P&gt;conn?.close()&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;conn?.close()&lt;BR /&gt;// Assign values to session object&lt;BR /&gt;def attrMap = ['VALIDORNOT':ValidOrNot,'sqlString':sqlString]&lt;BR /&gt;flowFile = session.putAllAttributes(flowFile, attrMap)&lt;BR /&gt;session.transfer(flowFile, REL_SUCCESS)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 14 Apr 2020 06:30:26 GMT</pubDate>
    <dc:creator>gkp_shakeel</dc:creator>
    <dc:date>2020-04-14T06:30:26Z</dc:date>
    <item>
      <title>Nifi Execute Groovy Script</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Nifi-Execute-Groovy-Script/m-p/293921#M216933</link>
      <description>&lt;P&gt;Hi All,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am facing issues with Execute Script processor. I am executing Groovy script which have oracle&amp;nbsp; DB in Execute Script processor but its not closing the DB connection.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Issues&lt;/STRONG&gt;: Database connection are not closing and its making Inactive connection in database.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The Groovy script we added proper open and close connections. Could you help fix this issues, also let me know where I am not correct .&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DB Connection Configuration is like that :&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 702px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/27127i16BC46AE617FF832/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Execute Script Process are configured as fallowing:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ExecuteScriptProcessor.JPG" style="width: 747px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/27128i830C25933164585E/image-size/large?v=v2&amp;amp;px=999" role="button" title="ExecuteScriptProcessor.JPG" alt="ExecuteScriptProcessor.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Groovy Script as following :&amp;nbsp;&lt;/P&gt;&lt;P&gt;import org.apache.commons.io.IOUtils&lt;BR /&gt;import org.apache.nifi.controller.ControllerService&lt;BR /&gt;import org.apache.nifi.processor.io.StreamCallback&lt;BR /&gt;import java.nio.charset.*&lt;BR /&gt;import groovy.sql.OutParameter&lt;BR /&gt;import groovy.sql.Sql&lt;BR /&gt;import oracle.jdbc.OracleTypes&lt;BR /&gt;import java.sql.ResultSet&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;def lookup = context.controllerServiceLookup&lt;BR /&gt;def dbServiceName = ConnectionPoolScriptExecution.value&lt;BR /&gt;def dbcpServiceId = lookup.getControllerServiceIdentifiers(ControllerService).find {&lt;BR /&gt;cs -&amp;gt; lookup.getControllerServiceName(cs) == dbServiceName&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;def conn = lookup.getControllerService(dbcpServiceId)?.getConnection();&lt;BR /&gt;def sql = new Sql(conn);&lt;/P&gt;&lt;P&gt;def flowFile = session.get()&lt;BR /&gt;if(!flowFile) return&lt;BR /&gt;//Session Values&lt;BR /&gt;strApplication = flowFile.getAttribute('application')&lt;BR /&gt;strUsername = flowFile.getAttribute('username')&lt;BR /&gt;strPassword = flowFile.getAttribute('password')&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;// SQL Query Creation and execution&lt;/P&gt;&lt;P&gt;def ValidOrNot&lt;/P&gt;&lt;P&gt;String sqlString = "select FN_HP_IS_VALID_APP('${strApplication}','${strUsername}','${strPassword}') as ValidOrNot from dual";&lt;BR /&gt;try {&lt;BR /&gt;rowNum = 0&lt;BR /&gt;sql.eachRow(sqlString, { dataResult -&amp;gt;&lt;BR /&gt;//println(dataResult.ValidOrNot)&lt;BR /&gt;ValidOrNot = dataResult.ValidOrNot&lt;BR /&gt;})&lt;/P&gt;&lt;P&gt;}&lt;BR /&gt;catch(Exception ex){&lt;BR /&gt;&lt;BR /&gt;conn?.close()&lt;BR /&gt;}&lt;BR /&gt;finally {&lt;/P&gt;&lt;P&gt;conn?.close()&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;conn?.close()&lt;BR /&gt;// Assign values to session object&lt;BR /&gt;def attrMap = ['VALIDORNOT':ValidOrNot,'sqlString':sqlString]&lt;BR /&gt;flowFile = session.putAllAttributes(flowFile, attrMap)&lt;BR /&gt;session.transfer(flowFile, REL_SUCCESS)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Apr 2020 06:30:26 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Nifi-Execute-Groovy-Script/m-p/293921#M216933</guid>
      <dc:creator>gkp_shakeel</dc:creator>
      <dc:date>2020-04-14T06:30:26Z</dc:date>
    </item>
    <item>
      <title>Re: Nifi Execute Groovy Script</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Nifi-Execute-Groovy-Script/m-p/294042#M217011</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;a href="https://community.cloudera.com/t5/user/viewprofilepage/user-id/38301"&gt;@mburgess&lt;/a&gt;&amp;nbsp; Hope you doing well, could you help on my issues. My script running fine but its process keeping oracle connection inactive and not closing, when i am checking in database its showing inactive&amp;nbsp; session which NIFI created by the NIFI user.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Apr 2020 12:56:12 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Nifi-Execute-Groovy-Script/m-p/294042#M217011</guid>
      <dc:creator>gkp_shakeel</dc:creator>
      <dc:date>2020-04-15T12:56:12Z</dc:date>
    </item>
    <item>
      <title>Re: Nifi Execute Groovy Script</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Nifi-Execute-Groovy-Script/m-p/308321#M223518</link>
      <description>&lt;P&gt;++&lt;/P&gt;&lt;P&gt;We are facing same problem.&lt;/P&gt;&lt;P&gt;Are there anyone who can fix this?&lt;/P&gt;</description>
      <pubDate>Wed, 23 Dec 2020 11:18:42 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Nifi-Execute-Groovy-Script/m-p/308321#M223518</guid>
      <dc:creator>rut</dc:creator>
      <dc:date>2020-12-23T11:18:42Z</dc:date>
    </item>
    <item>
      <title>Re: Nifi Execute Groovy Script</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Nifi-Execute-Groovy-Script/m-p/308322#M223519</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I am getting the same issues.could you help&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Please&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Dec 2020 11:26:36 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Nifi-Execute-Groovy-Script/m-p/308322#M223519</guid>
      <dc:creator>hus</dc:creator>
      <dc:date>2020-12-23T11:26:36Z</dc:date>
    </item>
    <item>
      <title>Re: Nifi Execute Groovy Script</title>
      <link>https://community.cloudera.com/t5/Support-Questions/Nifi-Execute-Groovy-Script/m-p/344265#M234128</link>
      <description>&lt;P&gt;Hello everyone!&lt;/P&gt;&lt;P&gt;In the company where I work they had the same problem, this time I had some time to review the script and do a proof of concept to try to solve the problem.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;In my test, after a few flowfiles the processor generates the oracle database connection error.&lt;/P&gt;&lt;P&gt;Searching groovy documentation I found that the example that is floating around the internet is not correctly using the closure for the connection (this incident for example).&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;The solution is to close the instance along with the connection, currently it is trying to close the connection that is declared at the beginning of the script but the instance is ignored. In my tests, when closing the connection instance, the process executed 30,000 flowfiles without generating any problems. Instead, for the definition of this script, after 14,000 flowfiles, the processor started to generate a connection error.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;You can find more info in the groovy documentancion:&lt;BR /&gt;&lt;A title="Connecting to HSQLDB" href="https://docs.groovy-lang.org/docs/latest/html/documentation/#_connecting_to_the_database" target="_blank" rel="noopener"&gt;Connecting to HSQLDB&lt;/A&gt;&amp;nbsp;&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="1.jpg" style="width: 999px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/34413i8DF609EB6E15D6F9/image-size/large?v=v2&amp;amp;px=999" role="button" title="1.jpg" alt="1.jpg" /&gt;&lt;/span&gt;&lt;BR /&gt;Well, what was expected arrived... the solution is as follows:&lt;BR /&gt;You need to add the finally statement and close sql&lt;/P&gt;&lt;LI-CODE lang="python"&gt;//Imports
import org.apache.commons.io.IOUtils
import org.apache.nifi.controller.ControllerService
import org.apache.nifi.processor.io.StreamCallback
import java.nio.charset.*
import groovy.sql.OutParameter
import groovy.sql.Sql
import java.sql.ResultSet


//Get the conncation string from NIFI conncation pool
def lookup = context.controllerServiceLookup
//Este valor se crea en el procesador con el nombre del controlador de la conexion
def dbServiceName = databaseConnectionPoolName.value
def dbcpServiceId = lookup.getControllerServiceIdentifiers(ControllerService).find {
    cs -&amp;gt; lookup.getControllerServiceName(cs) == dbServiceName
}

def conn = lookup.getControllerService(dbcpServiceId)?.getConnection()
sql = Sql.newInstance(conn);

////Get the session values from Nifi flow Start 
try {
    flowFile = session.get()
    if(!flowFile) return

    PRIMER_PARAMETRO = flowFile.getAttribute('PRIMER_PARAMETRO')
    SEGUNDO_PARAMETRO = flowFile.getAttribute('SEGUNDO_PARAMETRO')

    // Procedimiento con 2 parametros de entrada y 2 de salida
    String sqlString ="""{call PRUEBA_CONCEPTO.PROC_DUMMY_DUAL(?, ?, ?, ?)}""";

    //Parametros de salida
    def  CODIGO
    def  MENSAJE

    // Lista de parametros del procedimiento
    def parametersList = [PRIMER_PARAMETRO, SEGUNDO_PARAMETRO, Sql.NUMERIC, Sql.VARCHAR];

    //Ejecucion del procedimiento
    sql.call(sqlString, parametersList) {out_status_code, out_status_desc -&amp;gt;
        CODIGO = out_status_desc 
        MENSAJE = out_status_code
    };

    //Set the session values start 
    def attrMap = ['status_desc':CODIGO, 'status_code':String.valueOf(MENSAJE)]
    flowFile = session.putAllAttributes(flowFile, attrMap)
    session.transfer(flowFile, REL_SUCCESS)
} catch (e){
    log.error('Scripting error', e)
    flowFile = session.putAttribute(flowFile, "error", e.getMessage())
    session.transfer(flowFile, REL_FAILURE)
} finally {
    if (conn != null) conn.close();
    if (sql != null) sql.close();
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;In my case I use SQL.instance with a DBCPConnectionPool but it should be the same for this case.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2.jpg" style="width: 810px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/34414i82A82AB57CAF7ADF/image-size/large?v=v2&amp;amp;px=999" role="button" title="2.jpg" alt="2.jpg" /&gt;&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;Solution&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="3.png" style="width: 999px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/34416iB76987279360FC14/image-size/large?v=v2&amp;amp;px=999" role="button" title="3.png" alt="3.png" /&gt;&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;Issue&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="4.png" style="width: 999px;"&gt;&lt;img src="https://community.cloudera.com/t5/image/serverpage/image-id/34417i3099E277BCCB12D5/image-size/large?v=v2&amp;amp;px=999" role="button" title="4.png" alt="4.png" /&gt;&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;Dummy procedure:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="java"&gt;CREATE OR REPLACE 
PACKAGE PRUEBA_CONCEPTO AS 

  /* TODO enter package declarations (types, exceptions, methods etc) here */ 
    PROCEDURE PROC_DUMMY_DUAL ( PRIMER_PARAMETRO IN VARCHAR2 
                              , SEGUNDO_PARAMETRO IN NUMBER 
                              , CODIGO OUT NUMBER 
                              , MENSAJE OUT VARCHAR2 
                            );
END PRUEBA_CONCEPTO;
/
CREATE OR REPLACE
PACKAGE BODY PRUEBA_CONCEPTO AS

  PROCEDURE PROC_DUMMY_DUAL ( PRIMER_PARAMETRO IN VARCHAR2 
                              , SEGUNDO_PARAMETRO IN NUMBER 
                              , CODIGO OUT NUMBER 
                              , MENSAJE OUT VARCHAR2 
                            ) AS
  BEGIN
    -- TAREA: Se necesita implantación para PROCEDURE PRUEBA_CONCEPTO.PROC_DUMMY_DUAL
    CODIGO := SEGUNDO_PARAMETRO;
    MENSAJE := PRIMER_PARAMETRO;
  END PROC_DUMMY_DUAL;

END PRUEBA_CONCEPTO;&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;Test Procedure&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="java"&gt;SET SERVEROUTPUT ON;
DECLARE
PRIMER_PARAMETRO VARCHAR2(100);
SEGUNDO_PARAMETRO NUMBER;
CODIGO NUMBER;
MENSAJE VARCHAR2(100);
BEGIN

PRIMER_PARAMETRO := 'eSTO ES UN MENSAJE';
SEGUNDO_PARAMETRO := 1;

PRUEBA_CONCEPTO.PROC_DUMMY_DUAL(PRIMER_PARAMETRO, SEGUNDO_PARAMETRO, CODIGO, MENSAJE);

DBMS_OUTPUT.PUT_LINE('CODIGO : ' || CODIGO);
DBMS_OUTPUT.PUT_LINE('MENSAJE : ' || MENSAJE);

END;&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 20 May 2022 18:20:48 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Support-Questions/Nifi-Execute-Groovy-Script/m-p/344265#M234128</guid>
      <dc:creator>gdr</dc:creator>
      <dc:date>2022-05-20T18:20:48Z</dc:date>
    </item>
  </channel>
</rss>

