google-code-export / morphia

Automatically exported from code.google.com/p/morphia
1 stars 0 forks source link

Morphia ignored MongoException when system.index error happened #236

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
   env:  { mongo-java-driver : "version 2.3" , morphia:"0.98" ,mongo:"0.6.4" , os: "ubuntu10.10" , jdk: "sun jdk 1.6"}

   Here is the thing:
   1.  we got a collection ,like this:

        class Test {
            @Id
            String _id;

            @index(unique=true)
            String name;

            @index()
            String lname;
        }

   2.  we got some records on db , like :
       {_id : "id-string" , name:"support" , lname:"whatever"},
       {_id : "id-string" , name:"support1" , lname:"whatever2"},
       {_id : "id-string" , name:"support2" , lname:"whatever3"}
       after query { name:"support" and lname :"morphia"} ,of cause there is no result,then I try to save an Object { name:"support", lname :"morphia"}
       theoretically I will got an error: some like "duplicate key error index."  , but it won't happen.

   3. after debug , I locate the code on : 
                DatastoreImpl#throwOnError(WriteConcern wc, WriteResult wr) ,     line:773
            if ( wc == null && wr.getLastConcern() == null) {
            CommandResult cr = wr.getLastError();
            if (cr != null && cr.getErrorMessage() != null && cr.getErrorMessage().length() > 0)
                cr.throwOnError();
        }
      here is some things I concern.
        3.1.      check com.mongodb.CommandResult#getErrorMessage, only get error by key "errmsg", which is not enough , it should look like 
              com.mongodb.ServerError#getMsg( BSONObject o , String def ) , which will get error info from keys [ "$err" , "err" , "errmsg"]
              this will cause when Exception happened , but won't go through cr.throwOnError(), and just ignore the exception ,which we thought
              everything is going well , but actually the whole WriteConcern has "getLastError", and just wait for something,which won't save 
              any records (for collection Test ,other coll is cool) anymore.

        3.2. here is the debug info about err message get from lastError.
              { "err" : "E11000 duplicate key error index: *_db.email_addr.$name_1  dup key: { : \"support\" }" , "code" : 11000 , "n" : 0 , "ok" : 1.0}

        3.3  CommandResult#throwOnError() , it only throw MongoException when CommandResult#ok() == false, as code: 
                   if ( ! ok() ){
                      throw getException();
                   }
               but just like 3.2, when index duplication key error , the message is still "ok" ,which still won't throw MongoException.

        3.4  Is CommandResult#getErrorMessage thread safe? 
              I have a very weired thing,which have no idea what's going on.
                                 protected void throwOnError(WriteConcern wc, WriteResult wr) {
                                        logger.debug("add this log , this method will entered,but without this ? won't ");         ------- 1
                                if ( wc == null && wr.getLastConcern() == null) {
                                                logger.debug("add this log , it doesn't matter. ");                                             ------- 2
                                CommandResult cr = wr.getLastError();
                                if (cr != null && cr.getErrorMessage() != null && cr.getErrorMessage().length() > 0)
                            cr.throwOnError();
                                }
                                 }
              Only I add logger on "1" , the method will enter , but 2 , it's won't happen. any idea?

Original issue reported on code.google.com by gogomat...@gmail.com on 1 Mar 2011 at 10:17

GoogleCodeExporter commented 9 years ago
I believe this is fixed in newer versions of the driver. Can you test with 
2.5.x? Also, a test would be good since I'm not sure exactly where the error 
is, if it still exists.

Original comment by scotthernandez on 3 Apr 2011 at 4:51

GoogleCodeExporter commented 9 years ago

Original comment by scotthernandez on 3 Apr 2011 at 4:54

GoogleCodeExporter commented 9 years ago

Original comment by scotthernandez on 12 Nov 2011 at 7:39