How to re-connect to MongoDB using C++ driver? - c++

I've a C++ function which saves a document to MongoDB using C++ driver. It takes connection reference as argument:
http://pastebin.com/jwRDhNWQ
When I restart MongoDB, I can see that new connection is being made.
However, conn.isFailed() remains true.
This maybe happening due to the fact that when I reconnect, I am using conn and not &conn
When I do use &conn as in &conn.connect("localhost");, I get error message-
error: lvalue required as unary ‘&’ operand
How do I fix this? i.e. modify the underlying connection so that conn.isFailed() becomes false when a new connection has been established?

You should enable _autoReconnect in the mongo::DBClientConnection::DBClientConnection constructor.
http://api.mongodb.org/cplusplus/current/classmongo_1_1_d_b_client_connection.html#a6a1a348024dd302572504b7bfb6e74a2
The variable _failed returned by the method isfailed() is not set until _check Connection is called. _checkConnection is not called until something is sent to the database, so as an alternative, you could call the ping command before calling _isFailed. However, the recommended fix is to enable _autoReconnect.

Related

Qt connect works, what is the equivalent disconnect method?

I have a connect that works which connects a signal in C++ to a slot in JavaScript:
Object::connect(this, &clsQtPushBtn::clicked
,[pobjScriptEng, strCall, strFile, strScript]() {
QString strScriptWithCall = static_cast<QString>(strStript)
+ static_cast<QString>(strCall) + "();";
pobjScriptEng->evaluate(strScriptWithCall);
});
In the above code:
this is an instance of my PushButton class clsQtPushBtn.
clsQtPushBtn::clicked is the address of the "clicked" signal.
pobjScriptEng is a pointer to an instance of QJSEngine*.
strCall is a JavaScript function "test".
strFile is the name of the JavaScript file "simon2.js".
strScript is the content of the JavaScript file which contains the function test().
The question is how do a make the equivalent disconnect call as I'm having difficulty with it, there are 7 options and just changing connect for disconnect does not work, I get:
no matching member function for call to 'disconnect'
The QObject::connect function returns a QMetaObject::Connection so you can pass it to QObject::disconnect.
Normally, you would just provide the same details as in the connect call (for example, source and target objects, source signal and target slot).
However, that particular connect form you're using returns a QMetaObject::Connection object which you can store for later disconnection:
auto connection = Object::connect(this, &clsQtPushBtn::clicked, blah, blah);
:
QObject::disconnect(connection);
That's most likely a better way, since it can get the information immediately rather than searching for a connection in a possibly large list (though I may be wrong about how that's implemented). It's especially better since I believe it's the only option in this case, seeing that every lambda is unique, so you couldn't use a new one to find the old connection anyway.
In addition, I've used a local variable to store it but you'd probably be better storing it into a member variable of some sort.

Getting error in mongodb connection during test case?

I am getting the following error during test condition. I did not understand where i am doing mistake.
client = MongoClient(settings.MONGO_HOST, settings.MONGO_PORT)
raise TypeError("port must be an instance of int")
TypeError: port must be an instance of int
Your settings.MONGO_PORT value is most likely a string, while it should be an integer.
You can either change it at the source, or cast it here at the call site:
client = MongoClient(settings.MONGO_HOST, int(settings.MONGO_PORT))

C++ Poco ODBC Transactions - AutoCommit mode

I am currently attempting to use transactions in my C++ app, but I have a problem with the ODBC's auto commit mode.
I am using the POCO libaries to create a connection to a PostgreSQL database on the same machine. Currently, I can send data to this database as single statements, but I cannot get my head around how to use Poco's transaction libraries to be able to send this data more quickly.
As I have several thousand records to insert, and so continuing to use single insert statements is extrememly slow and inpractical - So I am trying to use Poco's transaction to speed this up a bit (a fair bit).
The error I am encountering is a theoretically a simple one - Poco is throwing the following error:
'Invalid access: Session is in auto commit mode.'
I understand, as a result of this, I should somehow set "auto commit" to false - as it only allows me to commit data to the database line by line, rather than as a single transaction.
The problem is how I set this.
Currently, I have a session created from Session.h, that looks alot like this:
session = new Poco::Data::Session(
"ODBC",
connection_data.str()
);
Where connection data is a simple stringstream with the login information, password, database, server and "Driver={PostgreSQL ANSI};" to tell ODBC to utilize PostgreSQL's driver.
I have tried just setting a property "autocommit" to false through the session's setFeature or setProperty settings, this, of course, was to no avail. (it was more of a ditch attempt at this point).
session->setFeature("AUTOCOMMIT", false);
Looking around, I saw a possible alternative method by creating a ODBC sessionImpl directly from ODBC/session/SessionImpl.h instead of using this generic method above, and then creating a new session object from this.
The benefits of this are that ODBC's sessionImpl has references to autocommit mode in the header, which would suggest it would be able to handle this:
void autoCommit(const std::string&, bool val);
/// Sets autocommit property for the session.
However, having not used sessionImpl before, I cannot garuntee if this will work or if can can get this to work with the limited documentation available.
I am using C++ 03 (Not 11), with Visual Studio 2015
Poco 1.7.5
Boost (Where needed)
Would any one know the correct way of setting this feature (above) or a alternative method to achieving this?
edit: Looking at the source of poco, at:
https://github.com/pocoproject/poco/blob/develop/Data/ODBC/src/SessionImpl.cpp#L153
The property seems be named autoCommit, and looking at
https://github.com/pocoproject/poco/blob/develop/Data/include/Poco/Data/AbstractSessionImpl.h#L120
the case of the property names seem to matter. So, does it help if you use session->setFeature("autoCommit", false);?
Cant you just call session->begin(); and session->end(); on the corresponding Session object?
What is returned by session->canTransact()?
According to the doc begin() will start a new transaction, the doc does not mention any property that needs to be set before or after.
See: https://pocoproject.org/docs/Poco.Data.Session.html
Also faced a similar issue.
First of all before begin() need:
m_ses.setFeature("autoCommit", false);
m_ses.begin();
And the second issue is that this feature stays "autoCommit" in false for all other sessions. So don't forget for the next session call
session.setFeature("autoCommit", true);

SOAP:1.027 SRT: Serialization / Deserialization failed in ABAP Proxy

I have to create a consumer proxy in SAP, the proxy generation is OK (or no errors were reported), but when i tried yo consume the proxy (SE80), i have the next error:
SOAP:1.027 SRT: Serialization / Deserialization failed
System expected a value for the type g.
If i continue, i have the response, but when i tried to call the customer service in a report, i have the error and i can't continue.
In a report, when i tried yo consume the proxy using this code, i have the same error, and i don't have response:
CREATE OBJECT proxy
EXPORTING
logical_port_name = 'LOGICAL_01'.
CALL METHOD proxy->proccess_check_status_invoice
EXPORTING
process_check_status_invoice = input
IMPORTING
process_check_status_invoice_r = output.
Whow can i solve this error?
Thanks,
Please use srt_util and verify the execution error with a trace of that proxy. The error log will specify which field and values are not allowed during the transformation.
SOAP:1.027 SRT: Serialization / Deserialization failed errors are due to incompatible data types, in my experience, most of the times are dates since ABAP datum and the standard differ and must be transformed.
Type g is usually the constant for the TYPEKIND of STRING. My guess is you are binding values that are CHARs instead of the STRING datatype.

disconnecting from mongoDb with C++ driver

i'm sure this must be really simple or i'm missing the point, but how do you disconnect from Mongo using the C++ driver and DBClientConnection? DBClient has a public member of 'connect' but no disconnect/kill/drop etc that I can find.
There is some talk (in stack overflow and on the web) of using ScopedDBConnection which does seem to be able to allow me to drop my connection - but there are very few examples of how it would be used - or info on when I should use that class over the DBClientConnection class.
Any ideas?
If you're using a DBClientConnection, it has one connection, and you aren't supposed to disconnect/reconnect. I guess it kills the connection when it calls the destructors. You can set it up to automatically reconnect so you can keep using it if it loses its connection.
If you want to have connection pooling and multiple connections, you want to use ScopedDBConnection. You can see some examples here: https://github.com/mongodb/mongo/blob/master/src/mongo/client/model.cpp
Here's the gist:
ScopedDbConnection conn("localhost");
mongo::BSONObjBuilder obj;
obj.append( "name" , "asd" );
conn->insert("test.test", obj);
conn.done();
Basically, you can do anything with conn that you can do with a DBClientConnection, but when you're done you call done().