zookeeper python接口实例详解
342
2023-01-19
SpringDataMongoDB多文档事务的实现
一、安装MongoDB4.0.3()
1.1、官方安装文档
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/
1.2、tar.gz包下载地址
https://mongodb.com/download-center/community?jmp=docs
1.3、复制集官方配置
https://docs.mongodb.com/manual/administration/replica-set-member-configuration/
二、SpringProject版本依赖()
Spring:5.1.1.RELEASE 以上
SpringDataMongoDB:2.1.1.RELEASE 以上
三、配置MongoTransactionManager(SpringBoot使用方式)
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项目)
file-encoding="utf-8" /> 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-ref="mongoClient" /> class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" /> class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper"> class="org.springframework.data.mongodb.core.convert.MappingMongoConverter"> 五、使用事务() @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 七、最后是重点() 7.1、必须使用复制集,单节点不支持,mongodb4.2 会支持分片集事务,复制集安装请自行研究 7.2、使用事务之前(@Transaction),数据库和文档必须提前创建,如何解决呢?且看下篇 7.3、Spring版本和SpringDataMongodb版本必须遵循 @SpringProject版本依赖 7.4、注意,此处的复制集不是4.0版本之前的主从复制集群,4.0之后已废弃主从模式
file-encoding="utf-8" />
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-ref="mongoClient" /> class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" /> class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper"> class="org.springframework.data.mongodb.core.convert.MappingMongoConverter"> 五、使用事务() @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 七、最后是重点() 7.1、必须使用复制集,单节点不支持,mongodb4.2 会支持分片集事务,复制集安装请自行研究 7.2、使用事务之前(@Transaction),数据库和文档必须提前创建,如何解决呢?且看下篇 7.3、Spring版本和SpringDataMongodb版本必须遵循 @SpringProject版本依赖 7.4、注意,此处的复制集不是4.0版本之前的主从复制集群,4.0之后已废弃主从模式
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-ref="mongoClient" /> class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" /> class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper"> class="org.springframework.data.mongodb.core.convert.MappingMongoConverter"> 五、使用事务() @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 七、最后是重点() 7.1、必须使用复制集,单节点不支持,mongodb4.2 会支持分片集事务,复制集安装请自行研究 7.2、使用事务之前(@Transaction),数据库和文档必须提前创建,如何解决呢?且看下篇 7.3、Spring版本和SpringDataMongodb版本必须遵循 @SpringProject版本依赖 7.4、注意,此处的复制集不是4.0版本之前的主从复制集群,4.0之后已废弃主从模式
mongo-ref="mongoClient" />
class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" /> class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper"> class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
五、使用事务()
@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
七、最后是重点()
7.1、必须使用复制集,单节点不支持,mongodb4.2 会支持分片集事务,复制集安装请自行研究
7.2、使用事务之前(@Transaction),数据库和文档必须提前创建,如何解决呢?且看下篇
7.3、Spring版本和SpringDataMongodb版本必须遵循 @SpringProject版本依赖
7.4、注意,此处的复制集不是4.0版本之前的主从复制集群,4.0之后已废弃主从模式
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~