如何在CGI :: Application中使用mod_perl持久化DBIx :: Class?

时间:2022-10-05 17:37:26

I am using CGI::Application on mod_perl with DBIx::Class and I'd like to have something like new define a new dbic schema on instantiation. So far I haven't been able to get it to work. The closest thing I have come to is a superclass that has a connect() method that returns a new object, but I would rather it be already be connected and instantiated.

我正在使用带有DBIx :: Class的mod_perl上的CGI :: Application,我想在实例化时使用new定义新的dbic架构。到目前为止,我还没能让它发挥作用。我最接近的是一个超类,它有一个返回一个新对象的connect()方法,但我宁愿它已经连接并实例化了。

I would really appreciate any thoughts at all.

我真的很感激任何想法。

Thanks!

Note: Ok, so obviously no help yet, but, in the meantime I made an accessor that lazily instantiates the DBIx::Class, so that might be a little bit better. Check it:

注意:好的,显然没有任何帮助,但是,与此同时,我创建了一个懒惰实例化DBIx :: Class的访问器,因此可能会更好一些。核实:

sub schema {
    my $self = shift;
    unless ($self->{schema}) {
        $self->{schema} = ACD::Model->connect(@{$self->cfg->{$ENV{MODE}}->{connect_params}});
    }
    return $self->{schema};
}

and then of course to use it you'd do something like:

然后当然要使用它你会做类似的事情:

$self->schema->resultset('Foo')->find(1234);

2 个解决方案

#1


Of course you can't serialise a database connection into a session file or whatever, and you can't create it before apache forks, but you can keep one live per child process.

当然,您无法将数据库连接序列化为会话文件或其他任何内容,并且您无法在apache forks之前创建它,但您可以为每个子进程保留一个实时进程。

An option for creating it ahead of time is to do it in your base mod_perl handler sub, but since the client connection has already started at that point it doesn't buy you any response time improvement.

提前创建它的选项是在你的基础mod_perl处理程序子中执行它,但由于客户端连接已经在那时开始,它不会为你带来任何响应时间改进。

So I would do a lazy implementation like you have above, but instead of caching the schema object in $self, cache it in a package level private variable, which will mean each apache child process has exactly one schema connection:

所以我会像上面一样做一个懒惰的实现,但不是在$ self中缓存模式对象,而是将它缓存在一个包级别的私有变量中,这意味着每个apache子进程只有一个模式连接:

my $_schema;

sub schema {
    return $_schema
        if $_schema; # actually, you should check the db connection is live

    return $_schema = ACD::Model->connect(...);
}

#2


I don't have a single answer, but http://lists.scsys.co.uk/pipermail/dbix-class/2006-June/001690.html is probably worth a read, so you understand how DBIC manages connections.

我没有一个答案,但http://lists.scsys.co.uk/pipermail/dbix-class/2006-June/001690.html可能值得阅读,因此您了解DBIC如何管理连接。

#1


Of course you can't serialise a database connection into a session file or whatever, and you can't create it before apache forks, but you can keep one live per child process.

当然,您无法将数据库连接序列化为会话文件或其他任何内容,并且您无法在apache forks之前创建它,但您可以为每个子进程保留一个实时进程。

An option for creating it ahead of time is to do it in your base mod_perl handler sub, but since the client connection has already started at that point it doesn't buy you any response time improvement.

提前创建它的选项是在你的基础mod_perl处理程序子中执行它,但由于客户端连接已经在那时开始,它不会为你带来任何响应时间改进。

So I would do a lazy implementation like you have above, but instead of caching the schema object in $self, cache it in a package level private variable, which will mean each apache child process has exactly one schema connection:

所以我会像上面一样做一个懒惰的实现,但不是在$ self中缓存模式对象,而是将它缓存在一个包级别的私有变量中,这意味着每个apache子进程只有一个模式连接:

my $_schema;

sub schema {
    return $_schema
        if $_schema; # actually, you should check the db connection is live

    return $_schema = ACD::Model->connect(...);
}

#2


I don't have a single answer, but http://lists.scsys.co.uk/pipermail/dbix-class/2006-June/001690.html is probably worth a read, so you understand how DBIC manages connections.

我没有一个答案,但http://lists.scsys.co.uk/pipermail/dbix-class/2006-June/001690.html可能值得阅读,因此您了解DBIC如何管理连接。