Skip to content

Commit

Permalink
Issue #577: always use DBIx::Connector
Browse files Browse the repository at this point in the history
no special case when not running in a web environment
  • Loading branch information
bschmalhofer committed Nov 11, 2021
1 parent 340c463 commit 7dc70ee
Showing 1 changed file with 13 additions and 64 deletions.
77 changes: 13 additions & 64 deletions Kernel/System/DB.pm
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,8 @@ use List::Util();
# CPAN modules
use DBI;

# Set a flag indicating whether we are in a web context
my $DBIxConnectorIsUsed;

BEGIN {
$DBIxConnectorIsUsed = $ENV{GATEWAY_INTERFACE} ? 1 : 0;
}

# DBIx::Connector is not used under mod_perl
use if $DBIxConnectorIsUsed, 'DBIx::Connector';
# DBIx::Connector is used for all database connections
use DBIx::Connector;

# OTOBO modules
use Kernel::System::VariableCheck qw(:all);
Expand Down Expand Up @@ -185,37 +178,23 @@ sub new {

=head2 Connect()
to connect to a database
$DBObject->Connect();
to connect to a database. Connections are managed with DBIx::Connector.
=cut
my $ConnectSuccess = $DBObject->Connect();
sub Connect {
my $Self = shift;
Return an empty list when the connection fails.
# Primarily trust that an existing DB-connection is still alive.
# But ping the connection once in a while.
# Under PSGI we rely on DBI-Connector.
if ( !$DBIxConnectorIsUsed && $Self->{dbh} ) {
When connection succeeds than a L<DBI> database handle is returned.
my $PingTimeout = 10; # Only ping every 10 seconds (see bug#12383).
my $CurrentTime = time;
my $DatabaseHandle = $DBObject->Connect();
if ( $CurrentTime - ( $Self->{LastPingTime} // 0 ) < $PingTimeout ) {
return $Self->{dbh};
}
This feature should be used only in exceptional cases, as usually all access to the database
should be handled by the instance on L<Kernel::System::DB>.
# Ping to see if the connection is still alive.
if ( $Self->{dbh}->ping() ) {
$Self->{LastPingTime} = $CurrentTime;

return $Self->{dbh};
}
=cut

# Ping failed: cause a reconnect.
delete $Self->{dbh};
}
sub Connect {
my $Self = shift;

# debug
if ( $Self->{Debug} > 2 ) {
Expand All @@ -228,7 +207,7 @@ sub Connect {
}

# db connect
if ($DBIxConnectorIsUsed) {
{

# Attribute for callbacks. See https://metacpan.org/pod/DBI#Callbacks
my %Callbacks;
Expand Down Expand Up @@ -324,15 +303,6 @@ sub Connect {
# this method reuses an existing connection when it is still pinging
$Self->{dbh} = $Cache{$CacheKey}->dbh();
}
else {
# use a connection that is not cached
$Self->{dbh} = DBI->connect(
$Self->{DSN},
$Self->{USER},
$Self->{PW},
$Self->{Backend}->{'DB::Attribute'},
);
}

if ( !$Self->{dbh} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Expand All @@ -344,27 +314,6 @@ sub Connect {
return;
}

# In the PSGI case this is included in the connection attributes
if ( !$DBIxConnectorIsUsed ) {
if ( $Self->{Backend}->{'DB::Connect'} ) {
$Self->Do( SQL => $Self->{Backend}->{'DB::Connect'} );
}

# maybe deactivate foreign key checks
if ( $Self->{DeactivateForeignKeyChecks} ) {
my $DeactivateSQL = $Self->GetDatabaseFunction('DeactivateForeignKeyChecks');
if ($DeactivateSQL) {
$Self->Do( SQL => $DeactivateSQL );
}
}

# set utf-8 on for PostgreSQL
# Note: This is untested for the PSGI-case
if ( $Self->{Backend}->{'DB::Type'} eq 'postgresql' ) {
$Self->{dbh}->{pg_enable_utf8} = 1;
}
}

if ( $Self->{SlaveDBObject} ) {
$Self->{SlaveDBObject}->Connect();
}
Expand Down

0 comments on commit 7dc70ee

Please sign in to comment.