使用SpringDataMongoDB怎么實(shí)現(xiàn)多文檔事務(wù)-創(chuàng)新互聯(lián)

使用SpringDataMongoDB怎么實(shí)現(xiàn)多文檔事務(wù)?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

為宣漢等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及宣漢網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、宣漢網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

SpringBoot 集成非常簡(jiǎn)單,配置MongoTransastionManager,目前Spring-boot-starter-data-mongodb 版本較低不支持,因此暫未驗(yàn)證,SpringBoot配置復(fù)制集參考SpringBoot相關(guān)文檔

@Configuration
static class Config extends AbstractMongoConfiguration {

  @Bean
  MongoTransactionManager transactionManager(MongoDbFactory dbFactory) { 
    return new MongoTransactionManager(dbFactory);
  }

  // ...
}

四、配置MongoTransactionManager(普通Spring項(xiàng)目)

  <context:property-placeholder location="classpath:mongo.properties" ignore-unresolvable="true"
    file-encoding="utf-8" />

  <!-- 
    重點(diǎn)配置
    1.安全配置:credentials="${mongo.user}:${mongo.password}@${mongo.database}" 
    2.單機(jī)配置: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>

  <!-- 重點(diǎn)配置 -->

  <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"/>

五、使用事務(wù)(×)

@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

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。

文章名稱:使用SpringDataMongoDB怎么實(shí)現(xiàn)多文檔事務(wù)-創(chuàng)新互聯(lián)
分享URL:http://muchs.cn/article28/pgojp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、ChatGPT品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站設(shè)計(jì)、標(biāo)簽優(yōu)化、全網(wǎng)營(yíng)銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁(yè)設(shè)計(jì)公司