Reputation: 1743
I configured Spring with MongoDB on my local machine, without replication, and everything works fine. I also have a replica set that works fine.
Now I tried to add the replica set, but the reads/writes still go to my local machine!
This is my configuration, host1-3 are virtual machines:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">
<mongo:repositories base-package="my.repositories" />
<mongo:mongo replica-set="host1:27017,host2:27017,host3:27017" />
<mongo:db-factory dbname="my_db" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
Anyone has ideas/suggestions? Thanks!
Edit: Ok so I found the problem, just needed to add mongo-ref to the factory. So the correct configuration is:
<mongo:mongo id="mongo" replica-set="host1:27017,host2:27017,host3:27017" />
<mongo:db-factory dbname="my_db" mongo-ref="mongo" />
Upvotes: 2
Views: 3495
Reputation: 1743
Ok so I found the problem, just needed to add mongo-ref to the factory. So the correct configuration is:
<mongo:mongo id="mongo" replica-set="host1:27017,host2:27017,host3:27017">
<mongo:db-factory dbname="my_db" mongo-ref="mongo" />
Upvotes: 4
Reputation: 160
Writes will always go to the primary. Reads go to the primary by default unless you change the Read Preference in your use of the Java driver.
Upvotes: 0