简体   繁体   English

使用mod_perl创建新的数据库句柄

[英]Creating new database handles with mod_perl

I've run into an issue when stress testing mod_perl that the database connections are going away. 在对mod_perl进行压力测试时,我遇到了一个问题,即数据库连接将消失。 I suspect that processes are sharing database connections, causing the issue. 我怀疑进程正在共享数据库连接,从而导致了问题。

But I've followed all instructions for Apache::DBI, and can't figure this out. 但是我已经遵循了Apache :: DBI的所有说明,因此无法弄清楚。

I'm making the connections in the child process and not in startup.pl. 我在子进程中建立连接,而不是在startup.pl中建立连接。 But when I examine the $dbh returned by each child from the DBI->connnect, the address is the same for every httpd process. 但是,当我检查每个孩子从DBI-> connnect返回的$ dbh时,每个httpd进程的地址都相同。 Firstly, if this is working properly and reconnecting for each process, should the address returned by DBI->connect be different for each child process? 首先,如果此方法正常工作并为每个进程重新连接,那么每个子进程的DBI-> connect返回的地址是否应该不同? I've assumed so, but as far as I can tell the core C code in DBI (dbih_setup_handle) is managing this and is returning the same address. 我已经假设是这样,但是据我所知,DBI中的核心C代码(dbih_setup_handle)正在管理此操作并返回相同的地址。 So maybe I'm not understanding what it means to reconnect in the child. 因此,也许我不了解在孩子中重新连接意味着什么。

Am I reconnecting properly if the $dbh handles are the same? 如果$ dbh句柄相同,我是否可以重新正确连接?

It sounds like you are establishing the database connection in a <perl>...</perl> section in your startup config, or in a module loaded at startup, and keeping it around until forked processes attempt to use it. 听起来您好像在启动配置的<perl>...</perl>部分中,或在启动时加载的模块中建立数据库连接,并保持该状态,直到派生进程尝试使用它为止。

Unfortunately you can't get away with this. 不幸的是,你无法摆脱这个。 You'll need to somehow make sure that new processes get new connections. 您需要以某种方式确保新进程获得新连接。

My solution to this problem was a central function to obtain a dbh that kept track of what $$ (the current process id) was when the connection was established. 我对这个问题的解决方案是获得dbh的核心功能,该dbh可以跟踪建立连接时的$$ (当前进程ID)。 If when handing over the connection, the function found that $$ had changed, it would dispose of any existing connections without closing them , and create a new one: 如果在移交连接时,该函数发现$$已更改,它将在不关闭任何现有连接的情况下处置任何现有连接,并创建一个新连接:

my $_dbh;
my $pid;

sub dbh {
        if($pid != $$) {
                $_dbh = DBI->connect(...);
                $pid = $$;
        }
        return $_dbh;
}

Any code that wanted to use the database would first call dbh() to obtain a database handle, and let it create a new one if necessary, or hand over a previously established connection if it can be used. 任何想要使用数据库的代码都将首先调用dbh()来获取数据库句柄,并在必要时让它创建一个新的句柄,或者如果可以使用它,则移交一个先前建立的连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM