diff options
-rw-r--r-- | client/connpool.cpp | 7 | ||||
-rw-r--r-- | client/connpool.h | 19 | ||||
-rw-r--r-- | client/dbclient.cpp | 22 | ||||
-rw-r--r-- | client/dbclient.h | 10 | ||||
-rw-r--r-- | client/parallel.cpp | 2 |
5 files changed, 52 insertions, 8 deletions
diff --git a/client/connpool.cpp b/client/connpool.cpp index 54886090a09..2b814631bd3 100644 --- a/client/connpool.cpp +++ b/client/connpool.cpp @@ -115,6 +115,13 @@ namespace mongo { } } + ScopedDbConnection * ScopedDbConnection::steal(){ + assert( _conn ); + ScopedDbConnection * n = new ScopedDbConnection( _host , _conn ); + _conn = 0; + return n; + } + ScopedDbConnection::~ScopedDbConnection() { if ( _conn && ! _conn->isFailed() ) { /* see done() comments above for why we log this line */ diff --git a/client/connpool.h b/client/connpool.h index f5c0c866974..786ecc19f40 100644 --- a/client/connpool.h +++ b/client/connpool.h @@ -76,7 +76,7 @@ namespace mongo { clean up nicely. */ class ScopedDbConnection : boost::noncopyable { - const string host; + const string _host; DBClientBase *_conn; public: /** get the associated connection object */ @@ -96,13 +96,20 @@ namespace mongo { uassert( 13102 , "did you call done already" , _conn ); return _conn; } + + ScopedDbConnection() + : _host( "" ) , _conn(0 ){ + } /** throws UserException if can't connect */ - ScopedDbConnection(const string& _host) : - host(_host), _conn( pool.get(_host) ) { - //cout << " for: " << _host << " got conn: " << _conn << endl; + ScopedDbConnection(const string& host) + : _host(host), _conn( pool.get(host) ) { } + ScopedDbConnection(const string& host, DBClientBase* conn ) + : _host( host ) , _conn( conn ){ + } + /** Force closure of the connection. You should call this if you leave it in a bad state. Destructor will do this too, but it is verbose. */ @@ -126,10 +133,12 @@ namespace mongo { kill(); else */ - pool.release(host, _conn); + pool.release(_host, _conn); _conn = 0; } + ScopedDbConnection * steal(); + ~ScopedDbConnection(); }; diff --git a/client/dbclient.cpp b/client/dbclient.cpp index ecc5fa31a57..24552c1a9fa 100644 --- a/client/dbclient.cpp +++ b/client/dbclient.cpp @@ -25,6 +25,7 @@ #include "../util/md5.hpp" #include "../db/dbmessage.h" #include "../db/cmdline.h" +#include "connpool.h" namespace mongo { @@ -869,8 +870,16 @@ namespace mongo { return o; } + void DBClientCursor::attach( ScopedDbConnection * conn ){ + assert( ! _scopedConn ); + _scopedConn = conn->steal(); + } + + + DBClientCursor::~DBClientCursor() { DESTRUCTOR_GUARD ( + if ( cursorId && _ownCursor ) { BufBuilder b; b.append( (int)0 ); // reserved @@ -879,9 +888,20 @@ namespace mongo { Message m; m.setData( dbKillCursors , b.buf() , b.len() ); - + connector->sayPiggyBack( m ); } + + if ( _scopedConn ){ + if ( moreInCurrentBatch() ){ + log() << "warning: cursor deleted, but moreInCurrentBatch and scoped conn." << endl; + } + else { + _scopedConn->done(); + } + delete _scopedConn; + } + ); } diff --git a/client/dbclient.h b/client/dbclient.h index ca7218835cb..6084994463d 100644 --- a/client/dbclient.h +++ b/client/dbclient.h @@ -83,6 +83,7 @@ namespace mongo { }; class BSONObj; + class ScopedDbConnection; /** Represents a Mongo query expression. Typically one uses the QUERY(...) macro to construct a Query object. Examples: @@ -295,7 +296,8 @@ namespace mongo { nReturned(), pos(), data(), - _ownCursor( true ) { + _ownCursor( true ), + _scopedConn(0){ } DBClientCursor( DBConnector *_connector, const string &_ns, long long _cursorId, int _nToReturn, int options ) : @@ -309,7 +311,8 @@ namespace mongo { nReturned(), pos(), data(), - _ownCursor( true ) { + _ownCursor( true ), + _scopedConn(0){ } virtual ~DBClientCursor(); @@ -321,6 +324,8 @@ namespace mongo { */ void decouple() { _ownCursor = false; } + void attach( ScopedDbConnection * conn ); + private: friend class DBClientBase; bool init(); @@ -344,6 +349,7 @@ namespace mongo { void dataReceived(); void requestMore(); bool _ownCursor; // see decouple() + ScopedDbConnection * _scopedConn; }; /** diff --git a/client/parallel.cpp b/client/parallel.cpp index 59ec46f119a..1b82c010e38 100644 --- a/client/parallel.cpp +++ b/client/parallel.cpp @@ -69,6 +69,8 @@ namespace mongo { if ( cursor->hasResultFlag( QueryResult::ResultFlag_ShardConfigStale ) ) throw StaleConfigException( _ns , "ClusteredCursor::query" ); + + cursor->attach( &conn ); conn.done(); return cursor; |