You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by "Tesfaiesus, Mesel" <me...@hp.com> on 2005/08/12 15:40:12 UTC
Apache::Singleton (sharing one object)
Hi,
I'm trying to write an application, that allows the user to generate
complex SQL Queries step by step.
Therefore I need to make the same SQL-object available over many
requests.
I'm using the Aapache::Singleton Module and mod_perl 2.x/Apache2.x but I
always seem to get a new Object as the properties are set to default
every time.
My Testcode is printing out a $counter - which it saved as an attribute
of the object - that's supposed to increment with every request:
############### MyClass Module ###################
package MyClass;
use base qw (Apache::Singleton);
{
Sub _new_instance {
my $class = shift;
my $self = {};
bless $self, $class;
$self->setProperty( 'counter', 1);
}
}
#####################################################
################ Script: index.pl ################
Use MyClass;
my $instance = MyClass->instance();
my $counter = 0;
$counter = $instance->getProperty('counter');
print $counter;
$counter++;
$instance->setProperty('counter', $counter);
######################################################
The result is always 1. it doesn't increment. What am I doing wrong?
Any comment is appreciated.
Cheers,
mesel
Re: Apache::Singleton (sharing one object)
Posted by Perrin Harkins <pe...@elem.com>.
On Fri, 2005-08-12 at 15:40 +0200, Tesfaiesus, Mesel wrote:
> I'm using the Aapache::Singleton Module and mod_perl 2.x/Apache2.x but I
> always seem to get a new Object as the properties are set to default
> every time.
It defaults to request scope, as described in the documentation. To
make it switch to process scope, use this:
use base qw (Apache::Singleton::Process);
However, this will only persist in the one process (it's like putting
the object in a global). If what you need is to make it available in
all processes, create it during startup and put it in a global. If you
need that, but you need the data between processes to be in synch (e.g.
the counter in this example to be shared between all processes) you
should use a database or something like Cache::FastMmap to share the
state of it.
- Perrin
Re: Apache::Singleton (sharing one object)
Posted by Praveen Ray <pr...@yahoo.com>.
Shouldn't you be using the database itself to store the
state? Will Apache::Singleton instance replicate itself if
you were to run your application on multiple servers behind
load balancers (for scalability and fail-over purposes)?
If yes, you probably should usw Storage Module to save your
SQL object and retrieve in each request. That would be
'true' singleton, in my opinion.
--- "Tesfaiesus, Mesel" <me...@hp.com> wrote:
> Hi,
>
> I'm trying to write an application, that allows the user
> to generate
> complex SQL Queries step by step.
> Therefore I need to make the same SQL-object available
> over many
> requests.
>
> I'm using the Aapache::Singleton Module and mod_perl
> 2.x/Apache2.x but I
> always seem to get a new Object as the properties are set
> to default
> every time.
>
>
> My Testcode is printing out a $counter - which it saved
> as an attribute
> of the object - that's supposed to increment with every
> request:
>
> ############### MyClass Module ###################
>
> package MyClass;
>
> use base qw (Apache::Singleton);
>
> {
> Sub _new_instance {
> my $class = shift;
>
> my $self = {};
> bless $self, $class;
>
> $self->setProperty( 'counter', 1);
>
> }
> }
>
> #####################################################
>
>
>
>
> ################ Script: index.pl ################
>
> Use MyClass;
>
> my $instance = MyClass->instance();
>
> my $counter = 0;
>
> $counter = $instance->getProperty('counter');
>
> print $counter;
>
> $counter++;
>
> $instance->setProperty('counter', $counter);
>
> ######################################################
>
>
>
> The result is always 1. it doesn't increment. What am I
> doing wrong?
>
> Any comment is appreciated.
>
> Cheers,
> mesel
>
- Praveen
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com