SpringDataMongoDB多⽂档事务的实现
⼀、安装MongoDB4.0.3(××)
1.1、官⽅安装⽂档
1.2、包下载地址
1.3、复制集官⽅配置
⼆、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项⽬)
<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="${plSet}"
-->
<mongo:mongo-client id="mongoClient" replica-set="${plSet}">
<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="${tTimeout}" 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.db.core.mapping.MongoMappingContext" />
<bean id="defaultMongoTypeMapper"
class="org.vert.DefaultMongoTypeMapper">
<constructor-arg name="typeKey">
<null />
</constructor-arg>
</bean>
<bean id="mappingMongoConverter"
class="org.vert.MappingMongoConverter">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mappingContext" ref="mappingContext" />
<property name="typeMapper" ref="defaultMongoTypeMapper" />
</bean>
<bean id="mongoTemplate" class="org.db.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mongoConverter" ref="mappingMongoConverter" />
</bean>
<!-- 重点配置 -->
<bean id="mongoTransactionManager" class="org.db.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.minConnectionsPerHost=10
mongo.threadsAllowedToBlockForConnectionMultiplier=4
mongo.maxWaitTime=3000
mongo.socketKeepAlive=false
mongo.socketTimeout=30000
mongo.maxConnectionIdleTime=0
mongo.maxConnectionLifeTime=0
mongo.heartbeatSocketTimeout=20000
springframework事务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之后已废弃主从模式
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论