Skip to content

Commit

Permalink
Merge pull request #11 from ktnr/master
Browse files Browse the repository at this point in the history
Necessary to close db connection in normal app?
  • Loading branch information
lganzzzo authored Dec 29, 2020
2 parents e1a730d + d0765e2 commit aa05d2b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
19 changes: 14 additions & 5 deletions src/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,24 @@ void run() {

auto staticController = StaticController::createShared();
staticController->addEndpointsToRouter(router);


/* Get connection handler component */
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, connectionHandler);

/* Get connection provider component */
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, connectionProvider);

/* create server */
oatpp::network::Server server(connectionProvider,
connectionHandler);

oatpp::network::Server server(components.serverConnectionProvider.getObject(),
components.serverConnectionHandler.getObject());

OATPP_LOGD("Server", "Running on port %s...", components.serverConnectionProvider.getObject()->getProperty("port").toString()->c_str());
OATPP_LOGD("Server", "Running on port %s...", connectionProvider->getProperty("port").toString()->c_str());

server.run();

/* stop db connection pool */
OATPP_COMPONENT(std::shared_ptr<oatpp::provider::Provider<oatpp::sqlite::Connection>>, dbConnectionProvider);
dbConnectionProvider->stop();

}

Expand Down
25 changes: 17 additions & 8 deletions src/DatabaseComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,38 @@

class DatabaseComponent {
public:

/**
* Create database client
* Create database connection provider component
*/
OATPP_CREATE_COMPONENT(std::shared_ptr<UserDb>, userDb)([] {
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::provider::Provider<oatpp::sqlite::Connection>>, dbConnectionProvider)([] {

/* Create database-specific ConnectionProvider */
auto connectionProvider = std::make_shared<oatpp::sqlite::ConnectionProvider>(DATABASE_FILE);

/* Create database-specific ConnectionPool */
auto connectionPool = oatpp::sqlite::ConnectionPool::createShared(connectionProvider,
10 /* max-connections */,
std::chrono::seconds(5) /* connection TTL */);
return oatpp::sqlite::ConnectionPool::createShared(connectionProvider,
10 /* max-connections */,
std::chrono::seconds(5) /* connection TTL */);

}());

/**
* Create database client
*/
OATPP_CREATE_COMPONENT(std::shared_ptr<UserDb>, userDb)([] {

/* Get database ConnectionProvider component */
OATPP_COMPONENT(std::shared_ptr<oatpp::provider::Provider<oatpp::sqlite::Connection>>, connectionProvider);

/* Create database-specific Executor */
auto executor = std::make_shared<oatpp::sqlite::Executor>(connectionPool);
auto executor = std::make_shared<oatpp::sqlite::Executor>(connectionProvider);

/* Create MyClient database client */
return std::make_shared<UserDb>(executor);

}());


};

#endif //CRUD_DATABASECOMPONENT_HPP

0 comments on commit aa05d2b

Please sign in to comment.