EnsEMBL/DBSQL/ProxyDBConnection.pm untested

filter: Statementstmt 0.0% 0 / 101 Branchbran 0.0% 0 / 46 Conditioncond n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 172 CC 24 SCAR 38.0
CC
24
Cov
0%
switch_database
132.0
new
20.0
check_reconnection
20.0
CRAP 44.5
1 =head1 LICENSE
2  
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5  
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9  
10 http://www.apache.org/licenses/LICENSE-2.0
11  
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17  
18 =cut
19  
20  
21 =head1 CONTACT
22  
23 Please email comments or questions to the public Ensembl
24 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25  
26 Questions may also be sent to the Ensembl help desk at
27 <http://www.ensembl.org/Help/Contact>.
28  
29 =cut
30  
31 =head1 NAME
32  
33 Bio::EnsEMBL::DBSQL::ProxyDBConnection - Database connection wrapper allowing
34 for one backing connection to be used for multiple DBs
35  
36 =head1 SYNOPSIS
37  
38 my $dbc = Bio::EnsEMBL::DBSQL::DBConnection->new(-HOST => 'host', -PORT => 3306, -USER => 'user');
39 my $p_h_dbc = Bio::EnsEMBL::DBSQL::ProxyDBConnection->new(-DBC => $dbc, -DBNAME => 'human');
40 my $p_m_dbc = Bio::EnsEMBL::DBSQL::ProxyDBConnection->new(-DBC => $dbc, -DBNAME => 'mouse');
41
42 # With a 10 minute timeout reconnection in milliseconds
43 my $p_h_rc_dbc = Bio::EnsEMBL::DBSQL::ProxyDBConnection->new(-DBC => $dbc, -DBNAME => 'human', -RECONNECT_INTERVAL => (10*60*1000));
44  
45 =head1 DESCRIPTION
46  
47 This class is used to maintain one active connection to a database whilst it
48 appears to be working against multiple schemas. It does this by checking the
49 currently connected database before performing any query which could require
50 a database change such as prepare.
51  
52 This class is only intended for internal use so please do not use unless
53 you are aware of what it will do and what the consequences of its usage are.
54  
55 =head1 METHODS
56  
57 =cut
58  
59 package Bio::EnsEMBL::DBSQL::ProxyDBConnection;
60 $Bio::EnsEMBL::DBSQL::ProxyDBConnection::VERSION = '114.0.0_50'; # TRIAL
61 $Bio::EnsEMBL::DBSQL::ProxyDBConnection::VERSION = '114.0.050';
62 use strict;
63 use warnings;
64  
65 use base qw/Bio::EnsEMBL::Utils::Proxy/;
66  
67 use Bio::EnsEMBL::Utils::Argument qw/rearrange/;
68 use Bio::EnsEMBL::Utils::Exception qw/warning throw/;
69 use Bio::EnsEMBL::Utils::SqlHelper;
70  
71 use Time::HiRes qw/time/;
72  
73 sub new {
74 my ($class, @args) = @_;
75 my ($dbc, $dbname, $reconnect_interval) = rearrange([qw/DBC DBNAME RECONNECT_INTERVAL/], @args);
76 throw "No DBConnection -DBC given" unless $dbc;
77 throw "No database name -DBNAME given" unless $dbname;
78 my $self = $class->SUPER::new($dbc);
79 $self->dbname($dbname);
80 if($reconnect_interval) {
81 $self->reconnect_interval($reconnect_interval);
82 $self->_last_used();
83 }
84 return $self;
85 }
86  
87 =head2 switch_database
88  
89 Description : Performs a switch of the backing DBConnection if the currently
90 connected database is not the same as the database this proxy
91 wants to connect to. It currently supports MySQL, Oracle and
92 Postges switches is untested with all bar MySQL. If it
93 cannot do a live DB/schema switch then it will disconnect
94 the connection and then wait for the next process to
95 connect therefore switching the DB.
96 Exceptions : None but will warn if you attempt to switch a DB with
97 active kids attached to the proxied database handle.
98  
99 =cut
100  
101 sub switch_database {
102 my ($self) = @_;
103 my $proxy = $self->__proxy();
104 my $backing_dbname = $proxy->dbname();
105 my $dbname = $self->dbname();
106
107 my $switch = 0;
108 if(defined $dbname) {
109 if(defined $backing_dbname) {
110 $switch = ($dbname ne $backing_dbname) ? 1 : 0;
111 }
112 else {
113 $switch = 1;
114 }
115 }
116 else {
117 $switch = 1 if defined $backing_dbname;
118 }
119
120 if($switch) {
121 $proxy->dbname($dbname);
122 if($proxy->connected()) {
123 my $kids = $proxy->db_handle()->{Kids};
124 my $driver = lc($proxy->driver());
125 #Edit to add other DB switching strategies on a per driver basis
126 if($driver eq 'mysql') {
127 $proxy->do('use '.$dbname);
128 }
129 elsif($driver eq 'oracle') {
130 $proxy->do('ALTER SESSION SET CURRENT_SCHEMA = '.$dbname);
131 }
132 elsif($driver eq 'pg') {
133 $proxy->do('set search_path to '.$dbname);
134 }
135 else {
136 if($kids > 0) {
137 warning "Attempting a database switch from '$backing_dbname' to '$dbname' with $kids active handle(s). Check your logic or do not use a ProxyDBConnection";
138 }
139 $proxy->disconnect_if_idle();
140 }
141 }
142 }
143
144 return $switch;
145 }
146  
147 =head2 check_reconnection
148  
149 Description : Looks to see if the last time we used the backing DBI
150 connection was greater than the reconnect_interval()
151 provided at construction or runtime. If enought time has
152 elapsed then a reconnection is attempted. We do not
153 attempt a reconnection if:
154
155 - No reconnect_interval was set
156 - The connection was not active
157
158 Exceptions : None apart from those raised from the reconnect() method
159 from DBConnection
160 =cut
161  
162 sub check_reconnection {
163 my ($self) = @_;
164 #Return early if we had no reconnection interval
165 return unless $self->{reconnect_interval};
166
167 my $proxy = $self->__proxy();
168
169 #Only attempt it if we were connected; otherwise we can just skip
170 if($proxy->connected()) {
171 if($self->_require_reconnect()) {
172 $proxy->reconnect();
173 }
174 $self->_last_used();
175 }
176 return;
177 }
178  
179 # Each time this is called we record the current time in seconds
180 # to be used by the _require_reconnect() method
181 sub _last_used {
182 my ($self) = @_;
183 $self->{_last_used} = int(time()*1000);
184 return;
185 }
186  
187 # Uses the _last_used() time and the current reconnect_interval() to decide
188 # if the connection has been unused for long enough that we should attempt
189 # a reconnect
190 sub _require_reconnect {
191 my ($self) = @_;
192 my $interval = $self->reconnect_interval();
193 return unless $interval;
194 my $last_used = $self->{_last_used};
195 my $time_elapsed = int(time()*1000) - $last_used;
196 return $time_elapsed > $interval ? 1 : 0;
197 }
198  
199 =head2 reconnect_interval
200  
201 Arg[1] : Integer reconnection interval in milliseconds
202 Description : Accessor for the reconnection interval expressed in milliseconds
203 Returntype : Int miliseconds for a reconnection interval
204  
205 =cut
206  
207 sub reconnect_interval {
208 my ($self, $reconnect_interval) = @_;
209 $self->{'reconnect_interval'} = $reconnect_interval if defined $reconnect_interval;
210 return $self->{'reconnect_interval'};
211 }
212  
213 =head2 dbname
214  
215 Arg[1] : String DB name
216 Description : Accessor for the name of the database we should use whenever
217 a DBConnection request is made via this class
218 Returntype : String the name of the database which we should use
219 Exceptions : None
220  
221 =cut
222  
223 sub dbname {
224 my ($self, $dbname) = @_;
225 $self->{'dbname'} = $dbname if defined $dbname;
226 return $self->{'dbname'};
227 }
228  
229 my %SWITCH_METHODS = map { $_ => 1 } qw/
230 connect
231 db_handle
232 do
233 prepare
234 reconnect
235 work_with_db_handle
236 /;
237  
238  
239 # Manual override of the SqlHelper accessor to ensure it always gets the Proxy
240 sub sql_helper {
241 my ($self) = @_;
242 if(! exists $self->{_sql_helper}) {
243 my $helper = Bio::EnsEMBL::Utils::SqlHelper->new(-DB_CONNECTION => $self);
244 $self->{_sql_helper} = $helper;
245 }
246 return $self->{_sql_helper};
247 }
248  
249 sub __resolver {
250 my ($self, $package, $method) = @_;
251 if($self->__proxy()->can($method)) {
252 if($SWITCH_METHODS{$method}) {
253 return sub {
254 my ($local_self, @args) = @_;
255 $local_self->check_reconnection();
256 $local_self->switch_database();
257 $local_self->_last_used();
258 return $local_self->__proxy()->$method(@args);
259 };
260 }
261 else {
262 return sub {
263 my ($local_self, @args) = @_;
264 return $local_self->__proxy()->$method(@args);
265 };
266 }
267 }
268 return;
269 }
270  
271 1;