WriteConcern error

725 views
Skip to first unread message

Abhi

unread,
May 9, 2014, 10:56:50 AM5/9/14
to mongod...@googlegroups.com
Hi,
I am using spring data mongodb and want to use Journaled write concern.

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg ref="mongo"/>
        <constructor-arg name="databaseName" value="mytestdb" />
        <!-- write concern is set to w=1, j=true -->
        <property name="writeConcern" value="WriteConcern.JOURNAL_SAFE"/>
    </bean>


I am getting this error:-
Exception in thread "main" org.springframework.data.mongodb.UncategorizedMongoDbException: { "serverUsed" : "barium17.nyc:27017" , "ok" : 0 , "code" : 2 , "errmsg" : "cannot use non-majority 'w' mode WriteConcern.JOURNAL_SAFE when a host is not a member of a replica set"}; nested exception is com.mongodb.CommandFailureException: { "serverUsed" : "barium17.nyc:27017" , "ok" : 0 , "code" : 2 , "errmsg" : "cannot use non-majority 'w' mode WriteConcern.JOURNAL_SAFE when a host is not a member of a replica set"}
at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:78)
at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:1800)
at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:404)
at org.springframework.data.mongodb.core.MongoTemplate.insertDBObject(MongoTemplate.java:888)
at org.springframework.data.mongodb.core.MongoTemplate.doInsert(MongoTemplate.java:708)
at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:663)
at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:654)
at springmongo.dao.impl.EmployeeDAOImpl.insert(EmployeeDAOImpl.java:43)
at springmongo.SimpleTestdataInsert.insertEmployee(SimpleTestdataInsert.java:32)
at springmongo.SimpleTestdataInsert.main(SimpleTestdataInsert.java:59)

Please explain what i am doing wrong and is there anything i am misunderstanding about journaling enabled on mongod server and j=true flag on client side.

Thanks,
Abhi

Randolph Tan

unread,
May 9, 2014, 11:10:53 AM5/9/14
to mongod...@googlegroups.com
I am not very familiar with Spring, but it appears that the 'value' parameter is being passed to the 'w' field of the write concern to the server. Perhaps there's a different parameter for passing the journal: true.

Asya Kamsky

unread,
May 10, 2014, 3:30:44 AM5/10/14
to mongodb-user

What are the versions of MongoDB, Spring, Java driver?

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/5d625323-aff2-45b1-bf5c-697cdd761fbf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jeff Yemin

unread,
May 10, 2014, 11:05:26 AM5/10/14
to mongod...@googlegroups.com
I suspect that what is happening is that the writeConcern property is of type com.mongodb.WriteConcern, and your bean definition is passing a String.  The application context does not try to translate that String into a reference to the JOURNAL_SAFE constant that you intend it to, but instead is passing the String to the WriteConcern constructor that takes a String representing the value of the "w" property of the WriteConcern.  So now you have a WriteConcern with w="WriteConcern. JOURNAL_SAFE", which is not what you want.  And when it's passed to the server, the server does not recognize it as a valid write concern.

What you probably need to do instead is create another bean representing the write concern that you want, and then reference that bean.  I haven't tried it, but something like this should work:

<bean name="journaledWriteConcern" class="com.mongodb.WriteConcern">
   <constructor-arg type="int" value="1"/>
   <constructor-arg type="int" value="0"/>
   <constructor-arg type="boolean" value="false"/>
   <constructor-arg type="boolean" value="true"/>
</bean>

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg ref="mongo"/>
        <constructor-arg name="databaseName" value="mytestdb" />
        <!-- write concern is set to w=1, j=true -->
        <property name="writeConcern" ref="journaledWriteConcern"/>
</bean>

There may be an easier way to do this, but I have not found in the Spring documentation a way to reference a static field via XML configuration.

Hope this helps.



Jeff Yemin

unread,
May 10, 2014, 11:09:13 AM5/10/14
to mongod...@googlegroups.com

Rob Moore

unread,
May 10, 2014, 11:09:54 AM5/10/14
to mongod...@googlegroups.com

Using "WriteConcern.JOURNAL_SAFE" as the value for the writeConcern as there is no property editor to translate the string to a WriteConcern object.

You probably what to use the Spring Data MongoDB XML Namespace support since it will do a lot of translation for you.  I would start reading the documentation about here:

  http://docs.spring.io/spring-data/data-mongo/docs/1.4.2.RELEASE/reference/html/mongo.core.html#mongo.mongo-xml-config

If you can't use the namespace support then you will need to manually invoke the WriteConcern constructor.  For Journaled that looks like (in Java): 
public final static WriteConcern JOURNALED = new WriteConcern( 1, 0, false, true );
That translates to:
 <property name="writeConcern">
        <bean class="com.mongodb.WriteConcern">
           <constructor-arg value="1" />
           <constructor-arg value="0" />
           <constructor-arg value="false" />
           <constructor-arg value="true" />
        </bean>
    </property>

(I didn't actually test that XML blob so there may be typos.)

There is also a way to reference the static using Spring EL but that is left as an exercise for the reader.

HTH - Rob.

On Friday, May 9, 2014 10:56:50 AM UTC-4, Abhi wrote:

Abhi

unread,
May 12, 2014, 3:10:01 AM5/12/14
to mongod...@googlegroups.com
Hi,
Thanks for the suggestions, It is working now.
I have another query, when I do WriteConcern wc = mongoTemplate.getDb().getWriteConcern(); the values of w, j and fsync are shown as 1, false and false.

But I am setting configuring mongoTemplate instance with WriteConcern.JOURNALED like this

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg ref="mongo"/>
        <constructor-arg name="databaseName" value="mytestdb" />
        <!-- write concern is set to w=1, j=true -->
        <property name="writeConcern">
            <util:constant static-field="com.mongodb.WriteConcern.JOURNALED"/>
        </property>
    </bean>

Is there something missing?

Thanks,
Abhi

Abhi

unread,
May 12, 2014, 6:25:05 AM5/12/14
to mongod...@googlegroups.com
Hi,
I forgot add I am using mongodb 2.6, java driver 2.12 and spring data mongodb 1.3.2.
Reply all
Reply to author
Forward
0 new messages