In the solution that I ended up using, the singleton in question contains
the reference to IOC::Container, which contains service literals for each
of the constants which are available in $apache->dir_config. As a
performance enhancement, when I attempt to retrieve an object from the
container, I check the server hostname against the hostname in the
container and only reinitialize the container if they don't match. This
avoids reinitializing the container if it happens to be sequentially used
in the same virtualhost multiple times. I suppose I could have built a more
sophisticated caching system which stores container instances in a hash and
retrieves the correct one by hostname but I didn't feel like spending the
time and it seemed like a more fragile approach than I'm comfortable with.

if (is_isa($apache, 'Apache2::RequestRec')) {
my $server_hostname = $apache->server->server_hostname;
my $container_hostname = $container->get('hostname');
if ($server_hostname ne $container_hostname) {
$container->{C} = undef;
$container->_init_components($apache);
}
}



On Thu, Feb 27, 2014 at 12:34 PM, Rolf Schaufelberger <r...@plusw.de> wrote:

> Hi,
>
> we are using Class:Singleton , yet we manually destroy the singleton at
> the beginning of each request before we create a new one.
> So I have a class PW::Application which isa Class::Singleton  and then
>
>     {
>         no strict 'refs';
>         ${"PW::Application\::_instance"}= undef;
>     }
>     $self->{appl}= PW::Application->instance($param{base});
>
> I tried Apache::Singleton before, but, as far as I can remember,  it
> didn't work with mp2.
> So why are we using a singleton ? Well  , our app does not  only  consist
> of the web part , we also have many scripts running that are part of the
> application, and we put things like DBIx::Class schema object, config,
> current user etc in our singleton and so we can access these data  the same
> way, neither if we run a function from web interface or from command line.
> Since we don't have heavy  load on our apps creating this singleton with
> every request works for me .
>
>
>
> Am 02.02.2014 um 02:14 schrieb John Dunlap <j...@lariat.co>:
>
> > In mod_perl, can instantiated singletons survive requests? I'm asking
> because I appear to have one which *appears* to be bouncing back and forth
> between virtual hosts as perl interpreters are recycled. Is this possible
> and, if yes, how do I prevent it?
> >
> > Cheers!
> > John
>
>
> Rolf Schaufelberger
>

Reply via email to