Skip to content
This repository has been archived by the owner on Jul 24, 2021. It is now read-only.

Commit

Permalink
persist the ro db connection only for the request
Browse files Browse the repository at this point in the history
On staging, we were seeing lots of:
"DBIx::Class::Storage::DBI::_dbh_execute(): DBI Exception: DBD::Pg::st execute failed: no connection to the server.."
due to an AutoCommit=0 connection disconnecting (and we cannot automatially reconnect to this type of connection).
  • Loading branch information
karenetheridge committed Nov 6, 2019
1 parent 1846910 commit 01d9c1e
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/Conch/Plugin/Database.pm
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ See L</schema>; can be used interchangeably with it.
=head2 ro_schema
Provides (guaranteed) read-only access to the database via L<DBIx::Class>. Returns a
L<Conch::DB> object that persists for the lifetime of the application.
L<Conch::DB> object that persists for the lifetime of the request (B<not the application>).
Note that because of the use of C<< AutoCommit => 0 >>, database errors must be explicitly
cleared with C<< ->txn_rollback >>; see L<DBD::Pg/"ReadOnly-(boolean)">.
=cut

my $_ro_schema;
$app->helper(ro_schema => sub {
$app->helper(ro_schema => sub ($c) {
my $_ro_schema = $c->stash('ro_schema');
if ($_ro_schema) {
# clear the transaction of any errors, which accumulate because we have
# AutoCommit => 0 for this connection. Otherwise, we will get:
Expand All @@ -84,6 +84,8 @@ cleared with C<< ->txn_rollback >>; see L<DBD::Pg/"ReadOnly-(boolean)">.
AutoCommit => 0,
},
);
$c->stash('ro_schema', $_ro_schema);
return $_ro_schema;
});

=head2 db_<table>s, db_ro_<table>s
Expand Down

0 comments on commit 01d9c1e

Please sign in to comment.