温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用SpringDataMongoDB怎么实现多文档事务

发布时间:2021-05-24 18:16:08 来源:亿速云 阅读:274 作者:Leah 栏目:编程语言

使用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

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI