| 1 | ############################################################################# | ||
| 2 | # | ||
| 3 | # Apache::Session::Store::Oracle | ||
| 4 | # Implements session object storage via Oracle | ||
| 5 | # Copyright(c) 1998, 1999, 2000 Jeffrey William Baker (jwbaker@acm.org) | ||
| 6 | # Distribute under the Perl License | ||
| 7 | # | ||
| 8 | ############################################################################ | ||
| 9 | |||
| 10 | package Apache::Session::Store::Oracle; | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | |||
| 14 | use DBI; | ||
| 15 | use Apache::Session::Store::DBI; | ||
| 16 | |||
| 17 | use vars qw(@ISA $VERSION); | ||
| 18 | |||
| 19 | @ISA = qw(Apache::Session::Store::DBI); | ||
| 20 | $VERSION = '1.10'; | ||
| 21 | |||
| 22 | $Apache::Session::Store::Oracle::DataSource = undef; | ||
| 23 | $Apache::Session::Store::Oracle::UserName = undef; | ||
| 24 | $Apache::Session::Store::Oracle::Password = undef; | ||
| 25 | |||
| 26 | sub connection { | ||
| 27 | my $self = shift; | ||
| 28 | my $session = shift; | ||
| 29 | |||
| 30 | return if (defined $self->{dbh}); | ||
| 31 | |||
| 32 | $self->{'table_name'} = $session->{args}->{TableName} || $Apache::Session::Store::DBI::TableName; | ||
| 33 | |||
| 34 | if (exists $session->{args}->{Handle}) { | ||
| 35 | $self->{dbh} = $session->{args}->{Handle}; | ||
| 36 | $self->{commit} = $session->{args}->{Commit}; | ||
| 37 | return; | ||
| 38 | } | ||
| 39 | |||
| 40 | my $datasource = $session->{args}->{DataSource} || | ||
| 41 | $Apache::Session::Store::Oracle::DataSource; | ||
| 42 | my $username = $session->{args}->{UserName} || | ||
| 43 | $Apache::Session::Store::Oracle::UserName; | ||
| 44 | my $password = $session->{args}->{Password} || | ||
| 45 | $Apache::Session::Store::Oracle::Password; | ||
| 46 | |||
| 47 | $self->{dbh} = DBI->connect( | ||
| 48 | $datasource, | ||
| 49 | $username, | ||
| 50 | $password, | ||
| 51 | { RaiseError => 1, AutoCommit => 0 } | ||
| 52 | ) || die $DBI::errstr; | ||
| 53 | |||
| 54 | |||
| 55 | #If we open the connection, we close the connection | ||
| 56 | $self->{disconnect} = 1; | ||
| 57 | |||
| 58 | #the programmer has to tell us what commit policy to use | ||
| 59 | $self->{commit} = $session->{args}->{Commit}; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub materialize { | ||
| 63 | my $self = shift; | ||
| 64 | my $session = shift; | ||
| 65 | |||
| 66 | $self->connection($session); | ||
| 67 | |||
| 68 | local $self->{dbh}->{RaiseError} = 1; | ||
| 69 | local $self->{dbh}->{LongReadLen} = $session->{args}->{LongReadLen} || 8*2**10; | ||
| 70 | |||
| 71 | if (!defined $self->{materialize_sth}) { | ||
| 72 | $self->{materialize_sth} = | ||
| 73 | $self->{dbh}->prepare_cached(qq{ | ||
| 74 | SELECT a_session FROM $self->{'table_name'} WHERE id = ? FOR UPDATE}); | ||
| 75 | } | ||
| 76 | |||
| 77 | $self->{materialize_sth}->bind_param(1, $session->{data}->{_session_id}); | ||
| 78 | $self->{materialize_sth}->execute; | ||
| 79 | |||
| 80 | my $results = $self->{materialize_sth}->fetchrow_arrayref; | ||
| 81 | |||
| 82 | if (!(defined $results)) { | ||
| 83 | die "Object does not exist in the data store"; | ||
| 84 | } | ||
| 85 | |||
| 86 | $self->{materialize_sth}->finish; | ||
| 87 | |||
| 88 | $session->{serialized} = $results->[0]; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub DESTROY { | ||
| 92 | my $self = shift; | ||
| 93 | |||
| 94 | if ($self->{commit}) { | ||
| 95 | $self->{dbh}->commit; | ||
| 96 | } | ||
| 97 | |||
| 98 | if ($self->{disconnect}) { | ||
| 99 | $self->{dbh}->disconnect; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | 1; | ||
| 104 | |||
| 105 | =pod | ||
| 106 | |||
| 107 | =head1 NAME | ||
| 108 | |||
| 109 | Apache::Session::Store::Oracle - Store persistent data in a Oracle database | ||
| 110 | |||
| 111 | =head1 SYNOPSIS | ||
| 112 | |||
| 113 | use Apache::Session::Store::Oracle; | ||
| 114 | |||
| 115 | my $store = new Apache::Session::Store::Oracle; | ||
| 116 | |||
| 117 | $store->insert($ref); | ||
| 118 | $store->update($ref); | ||
| 119 | $store->materialize($ref); | ||
| 120 | $store->remove($ref); | ||
| 121 | |||
| 122 | =head1 DESCRIPTION | ||
| 123 | |||
| 124 | Apache::Session::Store::Oracle fulfills the storage interface of | ||
| 125 | Apache::Session. Session data is stored in a Oracle database. | ||
| 126 | |||
| 127 | =head1 SCHEMA | ||
| 128 | |||
| 129 | To use this module, you will need at least these columns in a table | ||
| 130 | called 'sessions': | ||
| 131 | |||
| 132 | id varchar2(32) # or however long your session IDs are. | ||
| 133 | a_session long | ||
| 134 | |||
| 135 | To create this schema, you can execute this command using the sqlplus program: | ||
| 136 | |||
| 137 | CREATE TABLE sessions ( | ||
| 138 | id varchar2(32) not null primary key, | ||
| 139 | a_session long | ||
| 140 | ); | ||
| 141 | |||
| 142 | If you use some other command, ensure that there is a unique index on the | ||
| 143 | table's id column. | ||
| 144 | |||
| 145 | =head1 CONFIGURATION | ||
| 146 | |||
| 147 | The module must know what datasource, username, and password to use when | ||
| 148 | connecting to the database. These values can be set using the options hash | ||
| 149 | (see Apache::Session documentation). The options are DataSource, UserName, | ||
| 150 | and Password. | ||
| 151 | |||
| 152 | Example: | ||
| 153 | |||
| 154 | tie %hash, 'Apache::Session::Oracle', $id, { | ||
| 155 | DataSource => 'dbi:Oracle:database', | ||
| 156 | UserName => 'database_user', | ||
| 157 | Password => 'K00l' | ||
| 158 | }; | ||
| 159 | |||
| 160 | Instead, you may pass in an already-opened DBI handle to your database. | ||
| 161 | |||
| 162 | tie %hash, 'Apache::Session::Oracle', $id, { | ||
| 163 | Handle => $dbh | ||
| 164 | }; | ||
| 165 | |||
| 166 | The last option is LongReadLen, which specifies the maximum size of the session | ||
| 167 | object. If not supplied, the default maximum size is 8 KB. | ||
| 168 | |||
| 169 | =head1 AUTHOR | ||
| 170 | |||
| 171 | This modules was written by Jeffrey William Baker <jwbaker@acm.org> | ||
| 172 | |||
| 173 | A fix for the commit policy was contributed by Michael Schout <mschout@gkg.net> | ||
| 174 | |||
| 175 | =head1 SEE ALSO | ||
| 176 | |||
| 177 | L<Apache::Session>, L<Apache::Session::Store::DBI> |