如何在 mod_perl 中拥有 DBIC 持久数据库连接?

发布于 2024-07-16 14:56:54 字数 986 浏览 5 评论 0原文

我正在为我的 Web 应用程序使用 mod_perl。 目前,我计划使用跨网络的mysql数据库。 在对 display_customer_transaction.cgi 的每个 CGI 请求中,我的脚本将

  1. 通过网络打开数据库连接
  2. 使用 SQL 语句对数据库执行查询
  3. 分析从数据库检索的数据
  4. 以 HTML 格式打印数据
  5. 关闭数据库连接

经过一些分析后,我意识到步骤(1)是瓶颈。 因此,我希望避免为每个 CGI 请求打开和关闭数据库连接。 我的愿望是,如果我的第一个 CGI 请求打开数据库连接,我的第二个传入 CGI 请求(来自不同的客户端)可以重用第一个数据库连接。

我尝试在 Google 上搜索“DBIX 持久数据库连接”,但几乎找不到相关结果。 (编辑:那是因为它被称为 DBIC,或 DBIx::Class,而不是 DBIX。)

我使用 Apache::DBI (但是,我的目的是 DBIX,而不是 Apache::DBI)。 有一些信息让我很困惑:

Apache::DBI 模块仍然有一个限制:它使数据库连接在每个进程的基础上保持持久性。

一直以来,我对 Apache 如何服务 CGI 请求的概念是

  1. Apache 总是会生成一个新进程来服务传入的新 CGI 请求。 每当 Perl 解释器执行完 Perl 脚本时,该进程就会终止。

那么,如果 Apache::DBI 模块只能在每个进程的基础上保持数据库连接持久,那么我的第二个 CGI 请求如何重新使用第一个 CGI 请求打开的连接?

但回到我原来的问题。 如何在 mod_perl 中建立 DBIX 持久数据库连接?

I am using mod_perl for my web application. Currently, I plan to use a mysql database across the network. In every CGI request to display_customer_transaction.cgi, my script will

  1. Open up database connection across network
  2. Perform query on the database using SQL statement
  3. Analysis the data retrieved from database
  4. Print out the data in HTML format
  5. Close the database connection

After some profiling, I realize step (1) is the bottleneck. Hence, I wish to avoid opening and closing a database connection for every CGI request. My wish is, if my first CGI request opens up a database connection, my second incoming CGI request (from different client) may reuse the first database connection.

I tried to Google for "DBIX Persistent Database Connection" but hardly find relevant result. (Edit: that's because it's called DBIC, or DBIx::Class, not DBIX.)

I further find for relevant information, using Apache::DBI (However, my intention is on DBIX, not Apache::DBI). There are some information which confused me:

The Apache::DBI module still has a limitation: it keeps database connections persistent on a per process basis.

All the while, my concept on how Apache serving CGI request is that

  1. Apache will always spawn a new process to serve an incoming new CGI request. Whenever the Perl interpreter finish executed Perl script, the process will dead.

So, if Apache::DBI module only able to keeps database connections persistent on a per process basis, how can my second CGI request re-use back the connection opened by the first CGI request?

But come back to my original question. How can I have DBIX persistent database connection in mod_perl?

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

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

发布评论

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

评论(3

零度° 2024-07-23 14:56:54

在你放弃之前尝试 Apache::DBI。 但是,您还希望使 CGI 脚本持久存在。 如果您现在有普通的 CGI 程序,则可以使用 PerlRun 或 PerlRegistry 选项使它们持久化。 与 Apache::DBI 一起,应该可以完成这项工作。 当然,每个子进程都有 DBI 连接,但这并非没有道理。

在放弃之前先尝试一下。 :)

Try Apache::DBI before you write it off. However, you also want to make your CGI scripts persistent. If you have vanilla CGI programs right now, you can use the PerlRun or PerlRegistry options to make them persistent. That, along with Apache::DBI, should do the job. Sure, each child process has DBI connections, but that's not unreasonable.

Give it a try before you give up on it. :)

享受孤独 2024-07-23 14:56:54

Apache::DBI 改变了 DBI 模块的工作方式。 假设您正在使用 DBIx::Class (您不够具体),那么它使用 DBI 模块来获取其数据库连接,所以这对您有用。

当您使用 mod_perl 时(假设您的 mod_perl 设置正确),Perl 解释器会加载到 Apache 中,并且您的程序也会加载到其中。 Apache 运行有限数量的进程来处理请求 - 每个进程处理多个请求。

Apache 进程最终会死亡并重新生成,但在此之前它们会处理许多请求。 这些是 Apache::DBI 文档引用的进程; 它试图说“您将一遍又一遍地重复使用同一个连接,但这并不意味着一次只有一个连接打开。”

Apache::DBI alters the way the DBI module works. Assuming you're using DBIx::Class (you aren't being specific enough) then it uses the DBI module to get its DB connections, so this will work for you.

When you use mod_perl (and assuming your mod_perl setup is right), a Perl interpreter is loaded into Apache and your program loads into that. Apache runs a limited number of processes to serve requests - each one serves more than one request.

The Apache processes do die eventually, and get respawned, but they serve many requests before that happens. These are the processes that the Apache::DBI documentation refers to; it's trying to say "you will re-use the same connection over and over, but that doesn't mean that only one connection is open at a time."

被你宠の有点坏 2024-07-23 14:56:54

最后,基于我的每秒连接数很低的判断,我此时没有使用持久连接。 对于性能扩展计划,我宁愿选择 DBIx + memcache 解决方案。

Finally, I didn't use persistence connection at this time based on the judgment of my connection per seconds is very low. For performance scaling plan, I would rather go for DBIx + memcache solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文