Skip to content

Commit

Permalink
sample(DBLogger): Verify validity of database session on startup.
Browse files Browse the repository at this point in the history
  • Loading branch information
matejk committed Nov 13, 2024
1 parent 99e0ea9 commit 94ebcfa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Data/samples/DBLogger/src/DBLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ class DBLogger: public ServerApplication

Poco::Data::Session session (_inserter.connector(), _inserter.connectionString());

if (!session.isGood() || !session.isConnected())
{
throw Poco::Data::NotConnectedException("Can't create demo database.");
}

session << ("DROP TABLE IF EXISTS "s + _tableName), now;
const auto create {
"CREATE TABLE "s + _tableName +
Expand Down Expand Up @@ -128,7 +133,7 @@ class DBLogger: public ServerApplication
_inserter.stop();

logger().information(
"Created %z messages, processed %z messages in %Ld ms.",
"Created %z messages, processed %z files in %Ld ms.",
_created, _inserter.totalProcessed(), (_startTime.elapsed() / 1000)
);

Expand Down Expand Up @@ -205,7 +210,7 @@ class DBLogger: public ServerApplication

void handleCreateDemoMessages(const std::string& name, const std::string& value)
{
config().setString("DBLogger.demo", "true");
config().setBool("DBLogger.demo", true);
}
void displayHelp()
{
Expand Down
7 changes: 7 additions & 0 deletions Data/samples/DBLogger/src/SQLLogInserter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include "SQLLogInserter.h"

#include "Poco/Data/DataException.h"

#include <filesystem>
#include <fstream>
#include <iostream>
Expand All @@ -23,6 +25,11 @@ void SQLLogInserter::start()
{
_dataSession = std::make_shared<Poco::Data::Session>(_connector, _connectionString);

if (!_dataSession->isGood() || !_dataSession->isConnected())
{
throw Poco::Data::NotConnectedException("Can't connect to database.");
}

_active = true;
_doneProcessing = false;

Expand Down

0 comments on commit 94ebcfa

Please sign in to comment.