diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOptionsFactoryBean.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOptionsFactoryBean.java index 74b86554f..dca66ac7a 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOptionsFactoryBean.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOptionsFactoryBean.java @@ -16,13 +16,15 @@ package org.springframework.data.document.mongodb; import com.mongodb.MongoOptions; + import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; /** - * A factory bean for consruction a MongoOptions instance + * A factory bean for construction of a MongoOptions instance * * @author Graeme Rocher + * @Author Mark Pollack */ public class MongoOptionsFactoryBean implements FactoryBean, InitializingBean { @@ -53,12 +55,50 @@ public class MongoOptionsFactoryBean implements FactoryBean, Initi * socket timeout. 0 is default and infinite */ private int socketTimeout = MONGO_OPTIONS.socketTimeout; + + + /** + * This controls whether or not to have socket keep alive + * turned on (SO_KEEPALIVE). + * + * defaults to false + */ + public boolean socketKeepAlive = MONGO_OPTIONS.socketKeepAlive; /** * this controls whether or not on a connect, the system retries automatically */ private boolean autoConnectRetry = MONGO_OPTIONS.autoConnectRetry; + /** + * This specifies the number of servers to wait for on the write operation, and exception raising behavior. + * + * Defaults to 0. + */ + private int writeNumber; + + /** + * This controls timeout for write operations in milliseconds. + * + * Defaults to 0 (indefinite). Greater than zero is number of milliseconds to wait. + */ + private int writeTimeout; + + /** + * This controls whether or not to fsync. + * + * Defaults to false. + */ + private boolean writeFsync; + + /** + * Specifies if the driver is allowed to read from secondaries + * or slaves. + * + * Defaults to false + */ + private boolean slaveOk = MONGO_OPTIONS.slaveOk; + /** * number of connections allowed per host will block if run out */ @@ -95,13 +135,61 @@ public class MongoOptionsFactoryBean implements FactoryBean, Initi public void setSocketTimeout(int socketTimeout) { this.socketTimeout = socketTimeout; } + + /** + * This controls whether or not to have socket keep alive + * @param socketKeepAlive + */ + public void setSocketKeepAlive(boolean socketKeepAlive) { + this.socketKeepAlive = socketKeepAlive; + } + + /** + * This specifies the number of servers to wait for on the write operation, and exception raising behavior. + * The 'w' option to the getlasterror command. Defaults to 0. + *
    + *
  • -1 = don't even report network errors
  • + *
  • 0 = default, don't call getLastError by default
  • + *
  • 1 = basic, call getLastError, but don't wait for slaves
  • + *
  • 2+= wait for slaves
  • + *
+ * @param writeNumber the number of servers to wait for on the write operation, and exception raising behavior. + */ + public void setWriteNumber(int writeNumber) { + this.writeNumber = writeNumber; + } /** + * This controls timeout for write operations in milliseconds. The 'wtimeout' option to the getlasterror command. + * + * @param writeTimeout Defaults to 0 (indefinite). Greater than zero is number of milliseconds to wait. + */ + public void setWriteTimeout(int writeTimeout) { + this.writeTimeout = writeTimeout; + } + + /** + * This controls whether or not to fsync. The 'fsync' option to the getlasterror command. Defaults to false. + * @param writeFsync to fsync on write (true), otherwise false. + */ + public void setWriteFsync(boolean writeFsync) { + this.writeFsync = writeFsync; + } + + /** * this controls whether or not on a connect, the system retries automatically */ public void setAutoConnectRetry(boolean autoConnectRetry) { this.autoConnectRetry = autoConnectRetry; } + + /** + * Specifies if the driver is allowed to read from secondaries or slaves. Defaults to false. + * @param slaveOk true if the driver should read from secondaries or slaves. + */ + public void setSlaveOk(boolean slaveOk) { + this.slaveOk = slaveOk; + } public void afterPropertiesSet() { MONGO_OPTIONS.connectionsPerHost = connectionsPerHost; @@ -109,8 +197,12 @@ public class MongoOptionsFactoryBean implements FactoryBean, Initi MONGO_OPTIONS.maxWaitTime = maxWaitTime; MONGO_OPTIONS.connectTimeout = connectTimeout; MONGO_OPTIONS.socketTimeout = socketTimeout; - MONGO_OPTIONS.autoConnectRetry = autoConnectRetry; - + MONGO_OPTIONS.socketKeepAlive = socketKeepAlive; + MONGO_OPTIONS.autoConnectRetry = autoConnectRetry; + MONGO_OPTIONS.slaveOk = slaveOk; + MONGO_OPTIONS.w = writeNumber; + MONGO_OPTIONS.wtimeout = writeTimeout; + MONGO_OPTIONS.fsync = writeFsync; } public MongoOptions getObject() { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/config/MongoParser.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/config/MongoParser.java index 2b9989a3a..33c020b14 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/config/MongoParser.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/config/MongoParser.java @@ -64,13 +64,20 @@ public class MongoParser extends AbstractSingleBeanDefinitionParser { BeanDefinitionBuilder optionsDefBuilder = BeanDefinitionBuilder .genericBeanDefinition(MongoOptionsFactoryBean.class); - setPropertyValue(optionsElement, optionsDefBuilder, "connectionsPerHost", "connectionsPerHost"); - setPropertyValue(optionsElement, optionsDefBuilder, "threadsAllowedToBlockForConnectionMultiplier", + setPropertyValue(optionsElement, optionsDefBuilder, "connections-per-host", "connectionsPerHost"); + setPropertyValue(optionsElement, optionsDefBuilder, "threads-allowed-to-block-for-connection-multiplier", "threadsAllowedToBlockForConnectionMultiplier"); - setPropertyValue(optionsElement, optionsDefBuilder, "maxWaitTime", "maxWaitTime"); - setPropertyValue(optionsElement, optionsDefBuilder, "connectTimeout", "connectTimeout"); - setPropertyValue(optionsElement, optionsDefBuilder, "socketTimeout", "socketTimeout"); - setPropertyValue(optionsElement, optionsDefBuilder, "autoConnectRetry", "autoConnectRetry"); + setPropertyValue(optionsElement, optionsDefBuilder, "max-wait-time", "maxWaitTime"); + setPropertyValue(optionsElement, optionsDefBuilder, "connect-timeout", "connectTimeout"); + setPropertyValue(optionsElement, optionsDefBuilder, "socket-timeout", "socketTimeout"); + setPropertyValue(optionsElement, optionsDefBuilder, "socket-keep-alive", "socketKeepAlive"); + setPropertyValue(optionsElement, optionsDefBuilder, "auto-connect-retry", "autoConnectRetry"); + setPropertyValue(optionsElement, optionsDefBuilder, "write-number", "writeNumber"); + setPropertyValue(optionsElement, optionsDefBuilder, "write-timeout", "writeTimeout"); + setPropertyValue(optionsElement, optionsDefBuilder, "write-fsync", "writeFsync"); + setPropertyValue(optionsElement, optionsDefBuilder, "slave-ok", "slaveOk"); + + mongoBuilder.addPropertyValue("mongoOptions", optionsDefBuilder.getBeanDefinition()); return true; @@ -92,4 +99,5 @@ public class MongoParser extends AbstractSingleBeanDefinitionParser { builder.addPropertyValue(propertyName, attr); } } + } \ No newline at end of file diff --git a/spring-data-mongodb/src/main/resources/org/springframework/data/document/mongodb/config/spring-mongo-1.0.xsd b/spring-data-mongodb/src/main/resources/org/springframework/data/document/mongodb/config/spring-mongo-1.0.xsd index 6a8e9c7e3..60b62aacf 100644 --- a/spring-data-mongodb/src/main/resources/org/springframework/data/document/mongodb/config/spring-mongo-1.0.xsd +++ b/spring-data-mongodb/src/main/resources/org/springframework/data/document/mongodb/config/spring-mongo-1.0.xsd @@ -297,14 +297,14 @@ The host to connect to a MongoDB server. Default is localhost - + - + - + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/config/MongoNamespaceTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/config/MongoNamespaceTests.java index f6c78418b..f42b9614e 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/config/MongoNamespaceTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/config/MongoNamespaceTests.java @@ -69,7 +69,16 @@ public class MongoNamespaceTests { assertEquals(8, mongoOpts.connectionsPerHost); assertEquals(1000, mongoOpts.connectTimeout); assertEquals(1500, mongoOpts.maxWaitTime); - assertEquals(false, mongoOpts.autoConnectRetry); + assertEquals(true, mongoOpts.autoConnectRetry); + assertEquals(1500, mongoOpts.socketTimeout); + assertEquals(4, mongoOpts.threadsAllowedToBlockForConnectionMultiplier); + assertEquals(true, mongoOpts.socketKeepAlive); + assertEquals(true, mongoOpts.fsync); + assertEquals(true, mongoOpts.slaveOk); + assertEquals(1, mongoOpts.getWriteConcern().getW()); + assertEquals(0, mongoOpts.getWriteConcern().getWtimeout()); + assertEquals(true, mongoOpts.getWriteConcern().fsync()); + } @SuppressWarnings({ "unchecked" }) diff --git a/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/config/MongoNamespaceTests-context.xml b/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/config/MongoNamespaceTests-context.xml index 2168283d4..719b43142 100644 --- a/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/config/MongoNamespaceTests-context.xml +++ b/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/config/MongoNamespaceTests-context.xml @@ -13,10 +13,17 @@ + connections-per-host="${mongo.connectionsPerHost}" + threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}" + connect-timeout="${mongo.connectTimeout}" + max-wait-time="${mongo.maxWaitTime}" + auto-connect-retry="${mongo.autoConnectRetry}" + socket-keep-alive="${mongo.socketKeepAlive}" + socket-timeout="${mongo.socketTimeout}" + slave-ok="${mongo.slaveOk}" + write-number="1" + write-timeout="0" + write-fsync="true"/> diff --git a/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/config/mongo.properties b/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/config/mongo.properties index c06b91b85..ef9d7c736 100644 --- a/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/config/mongo.properties +++ b/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/config/mongo.properties @@ -3,4 +3,10 @@ mongo.port=27017 mongo.connectionsPerHost=8 mongo.connectTimeout=1000 mongo.maxWaitTime=1500 -mongo.autoConnectRetry=false \ No newline at end of file +mongo.autoConnectRetry=true +mongo.socketTimeout=1500 +mongo.threadsAllowedToBlockForConnectionMultiplier=4 +mongo.socketKeepAlive=true +mongo.fsync=true +mongo.slaveOk=true + diff --git a/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/repository/PersonRepositoryIntegrationTests-context.xml b/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/repository/PersonRepositoryIntegrationTests-context.xml index 64942ebfe..eefabe9d8 100644 --- a/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/repository/PersonRepositoryIntegrationTests-context.xml +++ b/spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/repository/PersonRepositoryIntegrationTests-context.xml @@ -5,7 +5,7 @@ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd"> - +