Skip to content

Commit aa05d2b

Browse files
authoredDec 29, 2020
Merge pull request #11 from ktnr/master
Necessary to close db connection in normal app?
2 parents e1a730d + d0765e2 commit aa05d2b

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed
 

‎src/App.cpp

+14-5
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,24 @@ void run() {
2929

3030
auto staticController = StaticController::createShared();
3131
staticController->addEndpointsToRouter(router);
32-
32+
33+
/* Get connection handler component */
34+
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, connectionHandler);
35+
36+
/* Get connection provider component */
37+
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, connectionProvider);
38+
3339
/* create server */
40+
oatpp::network::Server server(connectionProvider,
41+
connectionHandler);
3442

35-
oatpp::network::Server server(components.serverConnectionProvider.getObject(),
36-
components.serverConnectionHandler.getObject());
37-
38-
OATPP_LOGD("Server", "Running on port %s...", components.serverConnectionProvider.getObject()->getProperty("port").toString()->c_str());
43+
OATPP_LOGD("Server", "Running on port %s...", connectionProvider->getProperty("port").toString()->c_str());
3944

4045
server.run();
46+
47+
/* stop db connection pool */
48+
OATPP_COMPONENT(std::shared_ptr<oatpp::provider::Provider<oatpp::sqlite::Connection>>, dbConnectionProvider);
49+
dbConnectionProvider->stop();
4150

4251
}
4352

‎src/DatabaseComponent.hpp

+17-8
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,38 @@
66

77
class DatabaseComponent {
88
public:
9-
9+
1010
/**
11-
* Create database client
11+
* Create database connection provider component
1212
*/
13-
OATPP_CREATE_COMPONENT(std::shared_ptr<UserDb>, userDb)([] {
13+
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::provider::Provider<oatpp::sqlite::Connection>>, dbConnectionProvider)([] {
1414

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

1818
/* Create database-specific ConnectionPool */
19-
auto connectionPool = oatpp::sqlite::ConnectionPool::createShared(connectionProvider,
20-
10 /* max-connections */,
21-
std::chrono::seconds(5) /* connection TTL */);
19+
return oatpp::sqlite::ConnectionPool::createShared(connectionProvider,
20+
10 /* max-connections */,
21+
std::chrono::seconds(5) /* connection TTL */);
22+
23+
}());
24+
25+
/**
26+
* Create database client
27+
*/
28+
OATPP_CREATE_COMPONENT(std::shared_ptr<UserDb>, userDb)([] {
29+
30+
/* Get database ConnectionProvider component */
31+
OATPP_COMPONENT(std::shared_ptr<oatpp::provider::Provider<oatpp::sqlite::Connection>>, connectionProvider);
2232

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

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

2939
}());
3040

31-
3241
};
3342

3443
#endif //CRUD_DATABASECOMPONENT_HPP

0 commit comments

Comments
 (0)
Please sign in to comment.