DBO example with postgres causing Could not connect to: FATAL: sorry, too many clients already
Added by Pony Dazzler about 5 years ago
I have gone through several of the issues in the forum that I thought might be applicable to my situation but could not find anything.
I was attempting to run one of the dbo tutorials (a very simplified version at least) when I ran into an issue where all my available db connections get consumed (max_connection=100 all connections get used). I receive the error: "sorry, too many clients already" exception which is coming directly from postgres. It seems to happen on the "session~~mapClass(tble_name);\" section of my code. When I comment out the line and use \"session~~>execute("Select * from callcalendar.t1;");" the code runs and does not consume all the connections; downside is that session->execute does not store query results. Anyway any assistance is appreciated.
//Header File
#include <Wt/Dbo/Dbo.h>
#include <Wt/Dbo/backend/Postgres.h>
#include <Wt/Dbo/Session.h>
#include <string>
namespace dbo = Wt::Dbo;
class t1Class
{
public:
t1Class();
virtual ~t1Class();
std::string t1;
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, t1, "t1");
}
};
#endif // T1_H
//Source File
t1Class::t1Class()
{
//ctor
const char* dbconn = "CONNECTION STUFF";
const char* tble_name = "TBLENAME";
std::unique_ptr<dbo::backend::Postgres> postgres(new dbo::backend::Postgres(std::string(dbconn)));
Wt::Dbo::Session * session = new Wt::Dbo::Session();
postgres.get()->setProperty("show-queries","true"); <-- the log displays the sql 97 times!
session->setConnection(std::move(postgres));
session->mapClass<t1Class>(tble_name); <-- When I comment out this line then the connections do not get consumed and
dbo::Transaction transaction(*session);
session->execute("Select * from callcalendar.t1;");
}
//Main
#include<iostream>
#include "t1.h"
using namespace std;
int main(int argc, char **argv)
{
t1Class * test = new t1Class();
cout << "Hello World!";
return 0;
}
RESULTS IN
Dbo Exception caught: Could not connect to: FATAL: sorry, too many clients already
FATAL: sorry, too many clients already
code:
Select * from TBLENAME;
Select * from TBLENAME;
Select * from TBLENAME;
Select * from TBLENAME;
Select * from TBLENAME;
Select * from TBLENAME;
--- 100 times
Environment
GCC version 8.2.0
Wt 4.0.5
PostgreSQL 10.7 (Ubuntu 10.7-1.pgdg18.10+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 8.2.0-7ubuntu1) 8.2.0, 64-bit
Distributor ID: Ubuntu
Description: Ubuntu 18.10
Release: 18.10
Codename: cosmic
Replies (2)
More Information - Added by Pony Dazzler about 5 years ago
Interestingly enough when I run the same code using a local instance of postgres everything worked as expected. It appears the issue occurs when connecting to a remote server via ssl.
RE: DBO example with postgres causing Could not connect to: FATAL: sorry, too many clients already - Added by Pony Dazzler about 5 years ago
Please ignore; made a rookie mistake; everything is working as expected.