apacheapache-nifihortonworks-dataflowdbcp

Do we need to close DBCPConnectionPool object in Custom Processor Or Is it handled by Controller Service itself?


I have created a Custom processor which take care of saving some records in mysql database. For setting up mysql database i am using DBCPConnectionPool object in my custom processor which does work of saving data to database tables correctly, But i am worried of pooling mechanism i am not closing this connection after my logic of saving is completed. This is working for 2 to 3 flowfiles but when i send multiple flowfile will it work correctly?

DBCPService dbcpService = context.getProperty(DBCP_SERVICE).asControllerService(DBCPService.class);
Connection con = dbcpService.getConnection();

I am looking for clarification as my currently flow is working correctly with less number of flowfile


Solution

  • You should be returning it to the pool, most likely with a try-with-resource:

    try (final Connection con = dbcpService.getConnection();
         final PreparedStatement st = con.prepareStatement(selectQuery)) {
    
    }
    

    You can always consult the standard processors to see what they do:

    https://github.com/apache/nifi/blob/master/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/AbstractExecuteSQL.java#L223