Can I (should I) use my DBClientConnection as a global variable?

29 views
Skip to first unread message

Therefore

unread,
Apr 21, 2013, 8:27:32 PM4/21/13
to mongo...@googlegroups.com

Writing a single-threaded MongoDB tool with the C++ driver. I always pass the DBClientConnection as a reference argument to any function that requires access to MongoDB. Can I use a global variable instead so that I don't have to pass the argument?

This came up when using Qt's QKeyEvent. This is implemented with this prototype:

   void keyPressEvent(QKeyEvent * key_event);

reimplementing:

   void QWidget::keyPressEvent ( QKeyEvent * event )

From which function I want to call a MongoDB function but I do not have access to the DBClientConnection within keyPressEvent. Hence why I'm considering making DBClientConnection a global.

If I don't pass the DBClientConnection and instead create a new connection at each function call, won't that be more significantly more overhead than with a persistent connection?

Eliot Horowitz

unread,
Apr 21, 2013, 10:33:21 PM4/21/13
to mongo...@googlegroups.com
No, you should be using ScopedDbConnection, which is backed by a
connection pool for use cases like that.
> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mongodb-dev...@googlegroups.com.
> To post to this group, send email to mongo...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mongodb-dev?hl=en-US.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
Reply all
Reply to author
Forward
0 new messages