我的递归方法调用有什么问题?

发布于 2024-10-12 06:58:54 字数 631 浏览 3 评论 0原文

我有一个执行存储过程并返回结果的搜索函数。如果没有结果,我想尝试通过更广义的搜索再次运行该函数。因此,我将 cfif 放入我的代码中 -

<cfif results.recordCount EQ 0 And Not arguments.searchForPotentialMatches>
   <cfset arguments.searchForPotentialMatches = True />
   <cfinvoke method="thisMethod" argumentCollection="#arguments#" />
</cfif>

基本上,如果没有结果并且我还没有尝试通用搜索,它应该再次调用此方法。然后,在方法的开头,在调用存储过程之前,我检查 searchForPotentialMatches 是否为 true,如果是,则概括搜索查询。

不过,似乎有一个问题...当我尝试运行它时,它会挂起 - 直到存储过程超时。通过调试和输出变量,我已经能够看到它到达存储过程,然后在尝试执行它时陷入困境。但是,在这些重新运行更改之前使用原始函数,如果我进行常规搜索,然后在 2 个单独的调用中进行广义搜索,它会正确执行。所以我不确定为什么当我尝试以编程方式构建它时它会失败......我做错了什么?

I have a search function that executes a stored procedure and returns results. If there are no results, I want to try running the function one more time with a more generalized search. So, I put a cfif into my code -

<cfif results.recordCount EQ 0 And Not arguments.searchForPotentialMatches>
   <cfset arguments.searchForPotentialMatches = True />
   <cfinvoke method="thisMethod" argumentCollection="#arguments#" />
</cfif>

Basically, if there were no results AND I haven't already tried a generalized search, it should invoke this method again. Then, in the beginning of the method, before calling the stored procedure, I check if searchForPotentialMatches is true, and if it is, I generalize the search query.

There seems to be a problem, though... When I try to run this, it hangs - until there's a timeout with the stored procedure. Through debugging and outputting variables, I've been able to see that it gets to the stored procedure, and then gets stuck trying to execute it. However, using the original function before these rerun changes, if I do the regular search and then the generalized search in 2 separate calls, it executes correctly. So I'm not sure why it fails when I try to build this in programmatically... What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

作业与我同在 2024-10-19 06:58:55

函数标签上的访问属性是什么,您是否给它一个使函数无法调用自身的值?

What is your access attribute on the function tag, have you given it a value that leaves the function unable to call itself?

完美的未来在梦里 2024-10-19 06:58:55

这感觉不公平......但问题是完全不同的东西。递归调用工作正常,但由于在调用存储过程之前检查函数并导致存储过程挂起,因此另一个字段发生了更改。对此深表歉意,并感谢您的帮助!

This feels unfair... But the issue was with something completely different. The recursive call works correctly, but there was another field that was getting changed due to a check in the function before calling the stored procedure and causing the stored proc to hang. Sorry about that, and thanks for all your help!

哭泣的笑容 2024-10-19 06:58:54

确实可以有很多东西。所有这些代码都在 cfc 中吗? cfc 是否处于持久作用域中,并且您是否正确地对所有变量进行了 var'd 操作?

您能否在正常和通用条件下独立执行存储过程而不会出现问题?

尝试粘贴更多代码(包括对存储过程的第一次调用),以便我们可以尝试更多地跟踪您的数据流。

Could really be any number of things. Is all of this code inside of a cfc? Is that cfc in a persistent scope and have you properly var'd all your variables?

Can you execute the stored proc under both normal and generalized conditions standalone without issue?

Try pasting in more of your code (including the first call to the stored proc) so we can try to trace your data flow a bit more.

悍妇囚夫 2024-10-19 06:58:54

递归是:

  • 理论上简单得诱人,但实践中却很麻烦——调试。
  • 通常需要走树或遍历图形,但是当人们可以不用时,就不用。

所以正如你所写,我会失去递归,并按顺序进行。由于没有@scrittler 要求的更多代码,我将重写如下:

<cfcomponent output="false">
    <cffunction name="search" output="false" access="public" returntype="any" hint="I am called by the client">
        <!--- <cfargument/> tags --->
        <!--- what ever you need to do with the arg before actually searching --->
        <cfset var results = doSearch(argumentCollection=arguments)>
        <cfif NOT results.recordcount>
            <!--- whatever you need to change about the args to perform a generalized search --->
            <cfset var results = doSearch(argumentCollection=arguments)>
        </cfif>

        <cfreturn results>
    </cffunction>

    <cffunction name="doSearch" output="false" access="private" returntype="query" hint="I run the query">
        <!--- <cfargument/> tags --->
        <!--- results query (i.e. call to sproc)--->
        <cfreturn results>
    </cffunction>
</cfcomponent>

Recursion is:

  • seductively simple in theory and a pain in the ass in practice - to debug.
  • often necessary to walk trees or traverse graphs, but when one can do without, do without.

So as you wrote, I'd lose the recursion, and do it sequentially. Absent any more code as @scrittler requested, I'd rewrite as such:

<cfcomponent output="false">
    <cffunction name="search" output="false" access="public" returntype="any" hint="I am called by the client">
        <!--- <cfargument/> tags --->
        <!--- what ever you need to do with the arg before actually searching --->
        <cfset var results = doSearch(argumentCollection=arguments)>
        <cfif NOT results.recordcount>
            <!--- whatever you need to change about the args to perform a generalized search --->
            <cfset var results = doSearch(argumentCollection=arguments)>
        </cfif>

        <cfreturn results>
    </cffunction>

    <cffunction name="doSearch" output="false" access="private" returntype="query" hint="I run the query">
        <!--- <cfargument/> tags --->
        <!--- results query (i.e. call to sproc)--->
        <cfreturn results>
    </cffunction>
</cfcomponent>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文