使用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
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。
文章名称:使用SpringDataMongoDB怎么实现多文档事务-创新互联
分享URL:https://www.cdcxhl.com/article28/pgojp.html
成都网站建设公司_创新互联,为您提供网站收录、ChatGPT、品牌网站设计、网站设计、标签优化、全网营销推广
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联