您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關spring+shardingjdbc+Atomikos分布式事務配置是什么,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
分表和其他表在一個庫里,滿足分布式事務,
pom配置:
<!-- shardingjdbc --><dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-namespace</artifactId> <version>4.0.0-RC1</version></dependency><dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-core</artifactId> <version>4.0.0-RC1</version></dependency><dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-transaction-xa-core</artifactId> <version>4.0.0-RC1</version></dependency>
數據源配置:
<bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName" value="dataSource"/> <property name="xaDataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"/> <property name="xaProperties"> <props> <prop key="url">jdbc:mysql://${jdbc.host}/${jdbc.database}</prop> <prop key="user">${jdbc.username}</prop> <prop key="password">${jdbc.password}</prop> </props> </property> <property name="minPoolSize" value="20" /> <property name="maxPoolSize" value="50" /> <property name="borrowConnectionTimeout" value="30" /> <!--<property name="testQuery" value="select 1" />--> <property name="maintenanceInterval" value="60" /> <property name="maxIdleTime" value="60" /> <property name="maxLifetime" value="60"/> <property name="loginTimeout" value="60" /></bean>
<!-- 分表數據源配置,分表策略--><bean id="preciseModuloTableShardingAlgorithm" class="com.sharding.algorithm.TableShardingAlgorithm"/><sharding:standard-strategy id="tableShardingStrategy" sharding-column="order_id" precise-algorithm-ref="preciseModuloTableShardingAlgorithm"/><sharding:data-source id="shardingDataSource"> <sharding:sharding-rule data-source-names="dataSource"> <sharding:table-rules> <sharding:table-rule logic-table="t_order" table-strategy-ref="tableShardingStrategy" actual-data-nodes="dataSource.t_order${0..2}" /> </sharding:table-rules> </sharding:sharding-rule></sharding:data-source>
分別為兩個數據源配上sqlSessionFactory和MapperScannerConfigurer(為了省事只配了一個)
<bean id="shardingSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="shardingDataSource"/> <property name="mapperLocations" value="classpath:sharding/mapper/*.xml"/> <property name="configLocation" value="classpath:spring/mybatis-config.xml"/> <property name="typeAliasesPackage" value="sharding.entity" /></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="sharding.mapper"/> <property name="sqlSessionFactoryBeanName" value="shardingSessionFactory"/></bean>
分布式事務配置:
<!-- 分布式事務 --><bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close"> <property name="forceShutdown" value="true"/></bean><bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp"> <property name="transactionTimeout" value="300"/></bean><bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="transactionManager" ref="atomikosTransactionManager"/> <property name="userTransaction" ref="atomikosUserTransaction"/></bean><tx:annotation-driven transaction-manager="transactionManager"/>
分表策略:
public final class TableShardingAlgorithm implements PreciseShardingAlgorithm<Long> { @Override public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Long> shardingValue) {for (String each : availableTargetNames) {if (each.endsWith(shardingValue.getValue() % 2 + "")) {return each; } }throw new UnsupportedOperationException(); } }
以上就是spring+shardingjdbc+Atomikos分布式事務配置是什么,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。