mongodb - Error Handling with Spring Data and Mongo Java Driver -
i'm having trouble understanding how correctly receive , handle db constraint errors when interacting mongodb.
i'm using:
- mongodb java driver - 2.11
- spring-data-mongodb - 1.2.1.release
- spring-framework-core - 3.1.0.release
i'm using mongotemplate.insert , giving "persistentteam" object represents football team. custom generating document's _id field per business rules. it's possible algorithm generating _id match existing object, in case want fall error handling.
with default configuration, if insert object duplicate id no error returned db. contradicts mongo documentation, says default mongo driver configuration "acknowledged" should raise exception in case: http://docs.mongodb.org/manual/release-notes/drivers-write-concern/ looked @ 2.11 mongo driver code , sure enough, in mongo.java default write concern writeconcern.normal (equivalent unacknowledged). fine, i'll work around.
edit: additional notes on code/documentation disparity: mongo java driver 2.10.0 introduced mongoclient.java replaces mongo.java. however, spring-framework-core 3.1.0.release not integrate new class. why i'm not getting expected default.
i updated mongo factory:
<bean id="mongo" class="org.springframework.data.mongodb.core.mongofactorybean"> <property name="host" value="${db.url}" /> <property name="writeconcern" value="acknowledged" /> </bean>
now when attempt insert object duplicate primary key, see following error:
{"com.mongodb.mongoexception$duplicatekey: { \"serverused\" : \"localhost/127.0.0.1:27017\" , \"err\" : \"e11000 duplicate key error index: sports.persistentteam.$_id_ dup key: { : 5019964551640473690 }\" , \"code\" : 11000 , \"n\" : 0 , \"connectionid\" : 21 , \"ok\" : 1.0}\n \tat com.mongodb.commandresult.getexception(commandresult.java:74)\n \tat com.mongodb.commandresult.throwonerror(commandresult.java:110)\n \tat com.mongodb.dbtcpconnector._checkwriteerror(dbtcpconnector.java:102)\n \tat com.mongodb.dbtcpconnector.say(dbtcpconnector.java:142)\n \tat com.mongodb.dbtcpconnector.say(dbtcpconnector.java:115)\n \tat com.mongodb.dbapilayer$mycollection.insert(dbapilayer.java:248)\n \tat com.mongodb.dbapilayer$mycollection.insert(dbapilayer.java:204)\n \tat com.mongodb.dbcollection.insert(dbcollection.java:76)\n \tat com.mongodb.dbcollection.insert(dbcollection.java:60)\n \tat com.mongodb.dbcollection.insert(dbcollection.java:105)\n \tat org.springframework.data.mongodb.core.mongotemplate$8.doincollection(mongotemplate.java:835)\n \tat org.springframework.data.mongodb.core.mongotemplate.execute(mongotemplate.java:388)\n \tat org.springframework.data.mongodb.core.mongotemplate.insertdbobject(mongotemplate.java:830)\n \tat org.springframework.data.mongodb.core.mongotemplate.doinsert(mongotemplate.java:659)\n \tat org.springframework.data.mongodb.core.mongotemplate.insert(mongotemplate.java:613)\n \tat org.springframework.data.mongodb.core.mongotemplate.insert(mongotemplate.java:604)\n \tat com.example.persistence.dao.mongodataservicedao.create(mongodataservicedao.java:96)\n \tat com.example.persistence.persistenceimpl.createobject(persistenceimpl.java:944)\n }
ok, great! it's working. not really. when try insert should succeed, error:
{ com.mongodb.writeconcernexception: { \"serverused\" : \"localhost/127.0.0.1:27017\" , \"n\" : 0 , \"connectionid\" : 20 , \"wnote\" : \"no replication has been enabled, w=\\\"acknowledged\\\" won't work\" , \"err\" : \"norepl\" , \"ok\" : 1.0} \n\tat com.mongodb.commandresult.getexception(commandresult.java:77)\n \tat com.mongodb.commandresult.throwonerror(commandresult.java:110)\n \tat com.mongodb.dbtcpconnector._checkwriteerror(dbtcpconnector.java:102)\n \tat com.mongodb.dbtcpconnector.say(dbtcpconnector.java:142)\n \tat com.mongodb.dbtcpconnector.say(dbtcpconnector.java:115)\n \tat com.mongodb.dbapilayer$mycollection.insert(dbapilayer.java:248)\n \tat com.mongodb.dbapilayer$mycollection.insert(dbapilayer.java:204)\n \tat com.mongodb.dbcollection.insert(dbcollection.java:76)\n \tat com.mongodb.dbcollection.insert(dbcollection.java:60)\n \tat com.mongodb.dbcollection.insert(dbcollection.java:105)\n \tat org.springframework.data.mongodb.core.mongotemplate$8.doincollection(mongotemplate.java:835)\n \tat org.springframework.data.mongodb.core.mongotemplate.execute(mongotemplate.java:388)\n \tat org.springframework.data.mongodb.core.mongotemplate.insertdbobject(mongotemplate.java:830)\n \tat org.springframework.data.mongodb.core.mongotemplate.doinsert(mongotemplate.java:659)\n \tat org.springframework.data.mongodb.core.mongotemplate.insert(mongotemplate.java:613)\n \tat org.springframework.data.mongodb.core.mongotemplate.insert(mongotemplate.java:604)\n \tat com.example.mongo.persistence.dao.mongodataservicedao.create(mongodataservicedao.java:96)\n \tat com.example.persistence.persistenceimpl.createobject(persistenceimpl.java:944)\n }
so bit confusing. error tells me can't use acknowledged without replication. documentaton doesn't this. there other writeconcern modes explicitly state check writes replicas...acknowledged should verify write primary. , after that, write succeeded. do here? bury second exception , forget it?
this because you're using string variant of writeconcern
.
that way, driver regards replica tags set write concern. hence exception.
use int variant writeconcern
constructor , give value of 1 (as in {w=1}
) the value acknowledgment. anyway, since it's default write concern, can skip altogether.
Comments
Post a Comment