You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Michael Greenish <gr...@yahoo.com> on 2005/12/18 20:31:13 UTC

apache::session not writing to db

Hello,

This seems be a rather basic problem but I haven't
found the solution in the last several hours and have
run out of ideas.  I am using MP2 with Apache::Session
1.80.  I have created a session object class so I can
keep the retrieving of the session out of my main
code.  The session is called started/retrieved in a
constructor:

sub new {
  my $this = shift;
  my $class_name = ref($this) || $this;   
  my $sid = shift;            # should be undef if not
passed

  # create a session object based on the cookie we got
from the
  # browser, or a new session if we got no cookie
  my %session;
  tie %session, 'Apache::Session::MySQL', $sid, {
    Handle => $_dbh,
    LockHandle => $_dbh
  };

  $session{'stuff'} = 'is this in the database?';

  my $objRef = {'sid' => $session{_session_id}, 
       'session' => \%session };  # create references
to object
  bless $objRef, $class_name;     # associate
reference with class

  return $objRef;
}
Note that I set 'stuff' immediately after the session
is created.  I would expect at this point that the
database be updated. 

Then I use subroutines to add and retrieve data from
the session. 

The session is inserted into the sessions table, but
the a_session is never updated.  I tried using the
tied( %session )->make_modified, and I tried untie(
%session ) when I am done with the request, but none
of those seem to cause anything to be written in the
database.

Here is the table definition:

mysql> describe sessions;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default |
Extra |
+-----------+-------------+------+-----+---------+-------+
| id        | varchar(32) |      | PRI |         |    
  |
| a_session | text        | YES  |     | NULL    |    
  |
+-----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

Any one have any ideas?  

much appreciatively,

greanie

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Re: apache::session not writing to db

Posted by Jonathan Vanasco <jo...@2xlp.com>.
first-

	add a timestamp column to your db store.  you're going to need to  
clean out old sessions somehow.

mysql> describe sessions;
+-----------+-------------+------+-----+-------------------+-------+
| Field     | Type        | Null | Key | Default           | Extra |
+-----------+-------------+------+-----+-------------------+-------+
| id        | varchar(32) |      | PRI |                   |       |
| time      | timestamp   | YES  |     | CURRENT_TIMESTAMP |       |
| a_session | text        | YES  |     | NULL              |       |
+-----------+-------------+------+-----+-------------------+-------+

second -

this is off of my code.  maybe this will help.

SiteUserClass:

	my $ApacheSessionOptions = 	
	   {
	      'MySQL' =>
	      {
			DataSource => 'dbi:mysql:rs_session',
			UserName   => 'rs_session_user',
			Password   => 'rs_session_pass',
			LockDataSource => 'dbi:mysql:rs_session',
			LockUserName   => 'rs_session_user',
			LockPassword   => 'rs_session_pass',
	      }
	   };

	constructor:
		$this->{'ApacheSessionOptions'}		= $ApacheSessionOptions-> 
{$ApacheSessionStore};

MyFramework Site User Base Class

	if 	( ${$this->{'ApacheSessionStore'}} eq 'MySQL' )
	{
		DEBUG >0 && print STDERR "\nTying? Apache::Session::MySQL -  
$sessionID\n";
		eval{ tie %{$this->{'__SESSION'}} , 'Apache::Session::MySQL',  
$sessionID, $this->{'ApacheSessionOptions'} } ;
		if ($@)
		{
			$error = 1;
			$sessionID = undef;
		}
	}


Thirdly-

you can either turn debugging on with the DBI.  set tracelevel to 1  
or 2.  i don't know how to do that through apache::sesssion, you  
might be able to subclass the connect string.  or set a general  
default for the DBI.  i've just done it per handle on instantiation  
before.  if you can do that, it helps a ton.

another option is to restart mysql and have it log all queries, then  
tail that log in a terminal window.  i often do that on my dev box,  
and just run a cronjob to truncate the file every night.  also helps  
a ton.

	


Re: apache::session not writing to db

Posted by Michael Greenish <gr...@yahoo.com>.
Thanks Malcolm for the response.

Ok, so the constructor now is:

    my %session;
    tie %session, 'Apache::Session::MySQL', $sid, {
        Handle => $_dbh,
        LockHandle => $_dbh
    };

    $session{'stuff'} = 'is this in the database?';
    untie( %session );

But still no data in the a_session:
+----------------------------------+---------------------------------------------------------------------------------------------+
| id                               | a_session        
                                                      
                   |
+----------------------------------+---------------------------------------------------------------------------------------------+
| e478ac9a4b1a141cfeb9c784f5fdbe47 |                  
                                                      
                |
+----------------------------------+---------------------------------------------------------------------------------------------+
I truncated the table between attempts.

I checked the privileges:
+-----------+--------------+------------+-------------+-------------+-------------+-------------+-------------+-----------+------------+-----------------+------------+------------+-----------------------+------------------+
| Host      | Db           | User       | Select_priv
| Insert_priv | Update_priv | Delete_priv |
Create_priv | Drop_priv | Grant_priv | References_priv
| Index_priv | Alter_priv | Create_tmp_table_priv |
Lock_tables_priv |
+-----------+--------------+------------+-------------+-------------+-------------+-------------+-------------+-----------+------------+-----------------+------------+------------+-----------------------+------------------+
localhost | db_name | db_user | Y           | Y       
   | Y           | Y           | Y           | Y      
  | N          | Y               | Y          | Y     
    | Y                     | Y           |

The only one with 'N' is the Grant_priv, which one
would expect for a non-root account.  And nothing in
the apache error.log relating to the database.  I
couldn't track down the location of the MySQL binary
files to see if an actual update is being performed.

And I killed and restarted Apache2 just in case.

--- Malcolm J Harwood
<mj...@liminalflux.net> wrote:

> On Sunday 18 December 2005 02:31 pm, Michael
> Greenish wrote:
> 
> > Note that I set 'stuff' immediately after the
> session
> > is created.  I would expect at this point that the
> > database be updated.
> 
> That's incorrect. The database is only updated when
> the session is untied 
> (usually on going out of scope).
> 
> > The session is inserted into the sessions table,
> but
> > the a_session is never updated.  I tried using the
> > tied( %session )->make_modified, and I tried
> untie(
> > %session ) when I am done with the request, but
> none
> > of those seem to cause anything to be written in
> the
> > database.
> 
> untie %session should work. What happens if you do
> that in your constructor 
> after:
> 
> >	$session{'stuff'} = 'is this in the database?';
> 
> If that works, you've got a problem in your code
> somewhere. If it doesn't, the 
> it could be a db permissions issue or something. Do
> you get any errors in 
> your logs?
> 
> 
> -- 
> If you want to kiss the sky 
> Better learn how to kneel
> - 'Mysterious Ways' U2
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Re: apache::session not writing to db

Posted by Malcolm J Harwood <mj...@liminalflux.net>.
On Sunday 18 December 2005 02:31 pm, Michael Greenish wrote:

> Note that I set 'stuff' immediately after the session
> is created.  I would expect at this point that the
> database be updated.

That's incorrect. The database is only updated when the session is untied 
(usually on going out of scope).

> The session is inserted into the sessions table, but
> the a_session is never updated.  I tried using the
> tied( %session )->make_modified, and I tried untie(
> %session ) when I am done with the request, but none
> of those seem to cause anything to be written in the
> database.

untie %session should work. What happens if you do that in your constructor 
after:

>	$session{'stuff'} = 'is this in the database?';

If that works, you've got a problem in your code somewhere. If it doesn't, the 
it could be a db permissions issue or something. Do you get any errors in 
your logs?


-- 
If you want to kiss the sky 
Better learn how to kneel
- 'Mysterious Ways' U2