mongodb 插入数据是报错The connection may have been used since this write, cannot obtain a result

WARNING: Master switching from cntv.mongos2.service:10088 to cntv.mongos1.service:10088
Nov 22, 2011 3:04:10 PM com.mongodb.DBTCPConnector _set
WARNING: Master switching from cntv.mongos2.service:10088 to cntv.mongos1.service:10088
java.lang.IllegalStateException: The connection may have been used since this write, cannot obtain a result
        at com.mongodb.WriteResult.getLastError(WriteResult.java:98)
        at com.mongodb.WriteResult.getLastError(WriteResult.java:72)
        at com.mongodb.WriteResult.getField(WriteResult.java:136)
        at com.mongodb.WriteResult.getError(WriteResult.java:115)
        at cn.cntv.store.service.impl.MongoStoreProcessService.process(MongoStoreProcessService.java:139)
        at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.tuscany.sca.implementation.spring.SpringInvoker.doInvoke(SpringInvoker.java:100)
        at org.apache.tuscany.sca.implementation.spring.SpringInvoker.invoke(SpringInvoker.java:116)
        at org.apache.tuscany.sca.core.databinding.wire.PassByValueInterceptor.invoke(PassByValueInterceptor.java:112)
        at org.apache.tuscany.sca.core.invocation.RuntimeWireInvoker.invoke(RuntimeWireInvoker.java:129)
        at org.apache.tuscany.sca.core.invocation.RuntimeWireInvoker.invoke(RuntimeWireInvoker.java:104)
        at org.apache.tuscany.sca.core.invocation.RuntimeWireInvoker.invoke(RuntimeWireInvoker.java:98)
        at org.apache.tuscany.sca.core.assembly.RuntimeWireImpl.invoke(RuntimeWireImpl.java:155)
        at scallop.sca.binding.rmi.provider.RMIServiceBindingProvider.invokeTarget(RMIServiceBindingProvider.java:149)
        at scallop.sca.binding.rmi.provider.RMIServiceBindingProvider$1.intercept(RMIServiceBindingProvider.java:111)
        at $java.rmi.server.UnicastRemoteObject$$EnhancerByCGLIB$$268f75c1.process(<generated>)
        at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:305)
        at sun.rmi.transport.Transport$1.run(Transport.java:159)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
        at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:619)
java.lang.IllegalStateException: The connection may have been used since this write, cannot obtain a result
        at com.mongodb.WriteResult.getLastError(WriteResult.java:98)
        at com.mongodb.WriteResult.getLastError(WriteResult.java:72)
        at com.mongodb.WriteResult.getField(WriteResult.java:136)
        at com.mongodb.WriteResult.getError(WriteResult.java:115)
        at cn.cntv.store.service.impl.MongoStoreProcessService.process(MongoStoreProcessService.java:139)
        at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.tuscany.sca.implementation.spring.SpringInvoker.doInvoke(SpringInvoker.java:100)
        at org.apache.tuscany.sca.implementation.spring.SpringInvoker.invoke(SpringInvoker.java:116)
        at org.apache.tuscany.sca.core.databinding.wire.PassByValueInterceptor.invoke(PassByValueInterceptor.java:112)
        at org.apache.tuscany.sca.core.invocation.RuntimeWireInvoker.invoke(RuntimeWireInvoker.java:129)
        at org.apache.tuscany.sca.core.invocation.RuntimeWireInvoker.invoke(RuntimeWireInvoker.java:104)
        at org.apache.tuscany.sca.core.invocation.RuntimeWireInvoker.invoke(RuntimeWireInvoker.java:98)
        at org.apache.tuscany.sca.core.assembly.RuntimeWireImpl.invoke(RuntimeWireImpl.java:155)
        at scallop.sca.binding.rmi.provider.RMIServiceBindingProvider.invokeTarget(RMIServiceBindingProvider.java:149)
        at scallop.sca.binding.rmi.provider.RMIServiceBindingProvider$1.intercept(RMIServiceBindingProvider.java:111)
        at $java.rmi.server.UnicastRemoteObject$$EnhancerByCGLIB$$268f75c1.process(<generated>)
        at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:305)
        at sun.rmi.transport.Transport$1.run(Transport.java:159)

 

 

解决方法:

mongo = new Mongo(replicaSetSeeds);
  mongo.setWriteConcern(WriteConcern.SAFE );

加入这行之后就没有报错了。一切都和谐了

 来自网上:

As I said in my first response to this thread, you should really not 
be calling getLastError yourself but should be using the 
WriteConcern.Safe in your write methods or set at the 
collection/db/mongo level. If you do need to see the results of that 
getLastError response then using the cache version will mean you don't 
need to contact the server, ever. The difference between the two is 
wether a call to the server will be made (and if you can change the 
WriteConcern options). 
 

地址:http://groups.google.com/group/mongodb-user/browse_thread/thread/c73c3409bf37d556/651f6cacdef74a0b?#651f6cacdef74a0b

 

问题又来了:mongodb 丢数据

采用分片+副本集的方式。插入200W左右的数据总会丢10条左右的数据。原因不明。

将完全级别调高也不行。最后去掉一个分片。只用一个分片。就不丢数据了。

由于时间紧,先这么用。后续在研究改正。

另外一个人也遇到这个问题:http://blog.sina.com.cn/s/blog_56545fd30100twza.html