您好,登錄后才能下訂單哦!
使用SpringDataMongoDB怎么實現多文檔事務?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
SpringBoot 集成非常簡單,配置MongoTransastionManager,目前Spring-boot-starter-data-mongodb 版本較低不支持,因此暫未驗證,SpringBoot配置復制集參考SpringBoot相關文檔
@Configuration static class Config extends AbstractMongoConfiguration { @Bean MongoTransactionManager transactionManager(MongoDbFactory dbFactory) { return new MongoTransactionManager(dbFactory); } // ... }
四、配置MongoTransactionManager(普通Spring項目)
<context:property-placeholder location="classpath:mongo.properties" ignore-unresolvable="true" file-encoding="utf-8" /> <!-- 重點配置 1.安全配置:credentials="${mongo.user}:${mongo.password}@${mongo.database}" 2.單機配置:host="${mongo.host}" port="${mongo.port}" 3.副本集配置:replica-set="${mongo.replSet}" --> <mongo:mongo-client id="mongoClient" replica-set="${mongo.replSet}"> <mongo:client-options min-connections-per-host="${mongo.minConnectionsPerHost}" write-concern="SAFE" read-preference="NEAREST" threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}" connect-timeout="${mongo.connectTimeout}" max-wait-time="${mongo.maxWaitTime}" socket-keep-alive="${mongo.socketKeepAlive}" socket-timeout="${mongo.socketTimeout}" max-connection-idle-time="${mongo.maxConnectionIdleTime}" max-connection-life-time="${mongo.maxConnectionLifeTime}" heartbeat-socket-timeout="${mongo.heartbeatSocketTimeout}" heartbeat-connect-timeout="${mongo.heartbeatConnectTimeout}" min-heartbeat-frequency="${mongo.minHeartbeatFrequency}" heartbeat-frequency="${mongo.heartbeatFrequency}" /> </mongo:mongo-client> <mongo:db-factory id="mongoDbFactory" dbname="${mongo.database}" mongo-ref="mongoClient" /> <bean id="mappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" /> <bean id="defaultMongoTypeMapper" class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper"> <constructor-arg name="typeKey"> <null /> </constructor-arg> </bean> <bean id="mappingMongoConverter" class="org.springframework.data.mongodb.core.convert.MappingMongoConverter"> <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /> <constructor-arg name="mappingContext" ref="mappingContext" /> <property name="typeMapper" ref="defaultMongoTypeMapper" /> </bean> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /> <constructor-arg name="mongoConverter" ref="mappingMongoConverter" /> </bean> <!-- 重點配置 --> <bean id="mongoTransactionManager" class="org.springframework.data.mongodb.MongoTransactionManager"> <constructor-arg name="dbFactory" ref="mongoDbFactory"/> </bean> <tx:annotation-driven transaction-manager="mongoTransactionManager" proxy-target-class="true"/>
五、使用事務(×)
@Service public class StateService { @Autowired private MongoTemplate teplate; @Transactional void someBusinessFunction(Step step) { template.insert(step); log.debug(1/0); }; });
六、Mongo.properties 配置
mongo.host=192.168.1.66 mongo.port=27017 mongo.replSet=192.168.1.66:2000,192.168.1.66:2001,192.168.1.66:2002 mongo.minConnectionsPerHost=10 mongo.threadsAllowedToBlockForConnectionMultiplier=4 mongo.connectTimeout=30000 mongo.maxWaitTime=3000 mongo.socketKeepAlive=false mongo.socketTimeout=30000 mongo.maxConnectionIdleTime=0 mongo.maxConnectionLifeTime=0 mongo.heartbeatSocketTimeout=20000 mongo.heartbeatConnectTimeout=10000 mongo.minHeartbeatFrequency=3000 mongo.heartbeatFrequency=5000 mongo.database=labsta
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。