-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MariaDB connection issue #342
Comments
@ihmc3jn09hk Try to use There are some examples of ORM in the test project. |
@an-tao With
|
@an-tao Further tested with this library(see /test/connect.cpp) which results in a successful connection.
|
@an-tao Update: The error "ERROR Failed to mysql_real_connect() - MysqlConnection.cc:229" doesnt show up if direct key in the parameters in MysqlConnection.cc (e.g. Host, username, ...). |
@ihmc3jn09hk , After some inverstigations of this. I think this may be a issue of the asynchronous API of mariadb earlier version. This issue ocurrs when calling the mysql_real_connect_start() function with a hostname(or domain name). there are two solutions. 1. use IP address(127.0.0.1) instead of hostname('localhost')You shoud grant right privileges to the user as follows:
2. Or update the libmariadb to 10.4 version or newer.For example, install mariadb10.4 on ubuntu1804
For more OSs, see here |
Both solutions above have been tested and work properly. |
The synchronous API used by this library is different from the asynchronous API used by drogon. |
There are some database examples in the TFB benchmark suits |
@an-tao May I know the command 'create model' creates table(s) automatically ? Or user have to create the tables on prior??? |
This command reads the tables in the database and creates a model source file for each table. So, yes, users have to create the tables on prior. |
Problem resolved. I misunderstood the functionality of "create model". I thought it would create tables from C++ classes and maintain the OO/member variables of other classes/references relationship into DB tables. But now "create model" works the other way round. So the connection issue actually is because of the empty database. @an-tao BTW, would there be any support for NoSQL type DB? I think it is much better suit for Drogon. |
Yes, this is a no migrations approach, as it means you can deal with your databases in their own language and do the migration workflow separate from the rest of the project, being able to take full advantage of the existing workflows and tools for creating databases.
I will add support for Redis, MongoDB, but this will take time. Thank you for your suggestion. |
@an-tao Ok, the previous problem is kind of resolved. But found an interesting issue for many-to-many
The drogon_ctl produces getTo() relationship interface only. |
@ihmc3jn09hk Sorry for late reply, I'll do some testing for this and then give you an answer. |
About the relationship idea, please refer to #241 |
@an-tao Take your time. Thanks for the reference |
@ihmc3jn09hk I fixed the 'many to many' issue in PR #369 , please check. |
@an-tao Will check it out when merged |
The connection interface of Db in drogon is in non-blocking mode, this means that the connections are not established when the method returns. Maybe I should add some synchronization mechanism to this interface. |
This is what I am doing for kind of hack. Since if using the flag
|
@ihmc3jn09hk Yes, I'll fix this, sorry! |
@an-tao Trying to perform a load test to
One important difference, the concurrent requests are on a single record. |
Just tried some other thing with the for loop approach which tries to increment a valid in a record. Assuming the record to be updated is initialized as
|
@ihmc3jn09hk FooCtrl.h #pragma once
#include <drogon/HttpSimpleController.h>
#include <drogon/IOThreadStorage.h>
using namespace drogon;
class FooCtrl:public drogon::HttpSimpleController<FooCtrl>
{
public:
virtual void asyncHandleHttpRequest(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback) override;
PATH_LIST_BEGIN
//list path definitions here;
PATH_ADD("/foo",Get);
PATH_LIST_END
private:
IOThreadStorage<orm::DbClientPtr> _dbClient;
}; and FooCtrl.cc #include "FooCtrl.h"
#include "Foo.h"
#include <drogon/drogon.h>
using namespace drogon;
using namespace drogon::orm;
using namespace drogon_model::test;
void FooCtrl::asyncHandleHttpRequest(
const HttpRequestPtr &req,
std::function<void(const HttpResponsePtr &)> &&callback)
{
// write your application logic here
std::string id("foo");
auto callbackPtr =
std::make_shared<std::function<void(const HttpResponsePtr &)>>(
std::move(callback));
if (!*_dbClient)
{
*_dbClient = drogon::app().getFastDbClient();
}
Mapper<Foo> mapper(*_dbClient);
for (auto i = 0; i < 1000; ++i)
{
mapper.findOne(
Criteria(Foo::Cols::_name, CompareOperator::EQ, id),
[req, callbackPtr, this, &client = *_dbClient](Foo f) {
LOG_INFO << "Find one";
f.setAmount(f.getValueOfAmount() + 1); // Add 1 to the amount
Mapper<Foo> mapper(client);
mapper.update(
f,
[req, callbackPtr](const size_t count) {
LOG_INFO << "update " << count << " line";
if (1 == count)
{
LOG_INFO << "Updated";
}
},
[req, callbackPtr](const DrogonDbException &e) {
LOG_ERROR << e.base().what();
});
},
[req, callbackPtr](const DrogonDbException &e) {
LOG_ERROR << e.base().what();
});
}
} And about the value of the amount field, I think it's right. consider that the finding requests are send first asynchrounously, they will be executed before any updating statement, so this means all updating statements are the same. |
I still don't know why the Agree, if from my original concurrent requests case, the |
@ihmc3jn09hk I've reproduce this crashing on linux, I'll fix it. Sorry for confusing you. |
@ihmc3jn09hk I think this issue was fixed by the PR #379 , please check it out. thanks. |
@an-tao Have to say thank you first. May I have some information on what causes the crash? Would like to learn something XD. From the update in #379 , you fixed the SQL DB connection with making the connection parameters into member variables and capture the connection in All these causing the query crash? Or it is fixing the original "connection problem"? |
@ihmc3jn09hk actrually I fixed two problems In #379:
std::shared_ptr<SqlCmd> cmd = std::move(sqlCmdBuffer_.front());
sqlCmdBuffer_.pop_front();
conn->execSql(std::move(cmd->sql_),
cmd->parametersNumber_,
std::move(cmd->parameters_),
std::move(cmd->lengths_),
std::move(cmd->formats_),
std::move(cmd->callback_),
std::move(cmd->exceptionCallback_)); |
@an-tao Great thx for clarifications. I was trying to fix the bug as well and recognized there were 2 threads in 2 different classes performing the queries namely: |
Thank you for helping me find such a serious bug ^_^ |
It is best to output the log to a file. Printing the log on the standard output will seriously reduce the performance.
|
Describe the bug
Fail on the connection to MariaDB when testing the model create ctl.
Copied the default model.json into a folder named /models/testModel and run the following command.
The database is running which I can use PhpMyAdmin to access it. Created a database name "cppwebserver" and some other credentials for testing. I tried different ports (failed) and thought there was a bug in the model.json ( "passwd" to "password" ) still failed.
BTW, THANKS for the good works to the C++ community. I would like to take deeper understanding on this web-framework and compare with the Cutelyst and Wt. Are there some examples for the ORM part? Say, using the default model in the model.json.
To Reproduce
Steps to reproduce the behavior:
mkdir testModel && cp model.json testModel/
drogon_ctl create model testModel
and see errorExpected behavior
ERROR Failed to mysql_real_connect() - MysqlConnection.cc:229
Desktop (please complete the following information):
The text was updated successfully, but these errors were encountered: