提交 14a7e40a 编写于 作者: G gaohongtao 提交者: gaohongtao

fixed #172 adjust Spring and Yaml config for id generator

上级 7bd1a975
......@@ -34,6 +34,7 @@ import com.dangdang.ddframe.rdb.sharding.config.common.api.config.TableRuleConfi
import com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm.ClosureDatabaseShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm.ClosureTableShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.config.common.internal.parser.InlineParser;
import com.dangdang.ddframe.rdb.sharding.id.generator.IdGenerator;
import com.dangdang.ddframe.rdb.sharding.router.strategy.MultipleKeysShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.router.strategy.ShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.router.strategy.ShardingStrategy;
......@@ -83,7 +84,11 @@ public final class ShardingRuleBuilder {
public ShardingRule build() {
DataSourceRule dataSourceRule = buildDataSourceRule();
Collection<TableRule> tableRules = buildTableRules(dataSourceRule);
return ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(tableRules).bindingTableRules(buildBindingTableRules(tableRules))
com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule.ShardingRuleBuilder shardingRuleBuilder = ShardingRule.builder().dataSourceRule(dataSourceRule);
if (!Strings.isNullOrEmpty(shardingRuleConfig.getIdGeneratorClass())) {
shardingRuleBuilder.idGenerator(loadClass(shardingRuleConfig.getIdGeneratorClass(), IdGenerator.class));
}
return shardingRuleBuilder.tableRules(tableRules).bindingTableRules(buildBindingTableRules(tableRules))
.databaseShardingStrategy(buildShardingStrategy(shardingRuleConfig.getDefaultDatabaseStrategy(), DatabaseShardingStrategy.class))
.tableShardingStrategy(buildShardingStrategy(shardingRuleConfig.getDefaultTableStrategy(), TableShardingStrategy.class)).build();
}
......@@ -109,11 +114,22 @@ public final class ShardingRuleBuilder {
if (!Strings.isNullOrEmpty(tableRuleConfig.getDataSourceNames())) {
tableRuleBuilder.dataSourceNames(new InlineParser(tableRuleConfig.getDataSourceNames()).evaluate());
}
buildAutoIncrementColumn(tableRuleBuilder, tableRuleConfig);
result.add(tableRuleBuilder.build());
}
return result;
}
private void buildAutoIncrementColumn(final TableRule.TableRuleBuilder tableRuleBuilder, final TableRuleConfig tableRuleConfig) {
for (Entry<String, String> each : tableRuleConfig.getAutoIncrementColumns().entrySet()) {
if (null == each.getValue()) {
tableRuleBuilder.autoIncrementColumns(each.getKey());
} else {
tableRuleBuilder.autoIncrementColumns(each.getKey(), loadClass(each.getValue(), IdGenerator.class));
}
}
}
private Collection<BindingTableRule> buildBindingTableRules(final Collection<TableRule> tableRules) {
Collection<BindingTableRule> result = new ArrayList<>(shardingRuleConfig.getBindingTables().size());
for (BindingTableRuleConfig each : shardingRuleConfig.getBindingTables()) {
......@@ -174,4 +190,13 @@ public final class ShardingRuleBuilder {
return returnClass.isAssignableFrom(DatabaseShardingStrategy.class) ? (T) new DatabaseShardingStrategy(shardingColumns, (MultipleKeysDatabaseShardingAlgorithm) shardingAlgorithm)
: (T) new TableShardingStrategy(shardingColumns, (MultipleKeysTableShardingAlgorithm) shardingAlgorithm);
}
@SuppressWarnings("unchecked")
private <T> Class<? extends T> loadClass(final String className, final Class<T> superClass) {
try {
return (Class<? extends T>) Class.forName(className);
} catch (final ClassNotFoundException ex) {
throw new IllegalArgumentException(ex);
}
}
}
......@@ -17,14 +17,14 @@
package com.dangdang.ddframe.rdb.sharding.config.common.api.config;
import lombok.Getter;
import lombok.Setter;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import lombok.Getter;
import lombok.Setter;
/**
* 分片规则配置.
......@@ -46,4 +46,6 @@ public class ShardingRuleConfig {
private StrategyConfig defaultDatabaseStrategy;
private StrategyConfig defaultTableStrategy;
private String idGeneratorClass;
}
......@@ -20,6 +20,9 @@ package com.dangdang.ddframe.rdb.sharding.config.common.api.config;
import lombok.Getter;
import lombok.Setter;
import java.util.HashMap;
import java.util.Map;
/**
* 表规则配置.
*
......@@ -38,4 +41,6 @@ public class TableRuleConfig {
private StrategyConfig databaseStrategy;
private StrategyConfig tableStrategy;
private Map<String, String> autoIncrementColumns = new HashMap<>();
}
......@@ -68,6 +68,18 @@ public final class ShardingRuleBuilderTest {
shardingRuleConfig.setDefaultDatabaseStrategy(getDatabaseStrategyConfig("xxx.Algorithm"));
new ShardingRuleBuilder(shardingRuleConfig).build();
}
@Test(expected = IllegalArgumentException.class)
public void assertBuildFailureWhenAutoIncrementClassNotExisted() {
ShardingRuleConfig shardingRuleConfig = new ShardingRuleConfig();
shardingRuleConfig.setDataSource(createDataSourceMap());
shardingRuleConfig.setDefaultDataSourceName("ds_0");
shardingRuleConfig.setTables(createTableRuleConfigMap());
shardingRuleConfig.setIdGeneratorClass("not.existed");
shardingRuleConfig.setBindingTables(Collections.singletonList(createBindingTableRule("t_order", "t_order_item")));
shardingRuleConfig.setDefaultDatabaseStrategy(getDatabaseStrategyConfig(SingleAlgorithm.class.getName()));
shardingRuleConfig.setDefaultTableStrategy(getTableStrategyConfigForAlgorithmClass());
new ShardingRuleBuilder(shardingRuleConfig).build();
}
@Test
public void assertBuildSuccess() {
......@@ -75,6 +87,7 @@ public final class ShardingRuleBuilderTest {
shardingRuleConfig.setDataSource(createDataSourceMap());
shardingRuleConfig.setDefaultDataSourceName("ds_0");
shardingRuleConfig.setTables(createTableRuleConfigMap());
shardingRuleConfig.setIdGeneratorClass("com.dangdang.ddframe.rdb.sharding.config.common.fixture.IncrementIdGenerator");
shardingRuleConfig.setBindingTables(Collections.singletonList(createBindingTableRule("t_order", "t_order_item")));
shardingRuleConfig.setDefaultDatabaseStrategy(getDatabaseStrategyConfig(SingleAlgorithm.class.getName()));
shardingRuleConfig.setDefaultTableStrategy(getTableStrategyConfigForAlgorithmClass());
......@@ -144,6 +157,10 @@ public final class ShardingRuleBuilderTest {
result.setDataSourceNames("ds_${0..1}");
result.setDatabaseStrategy(getDatabaseStrategyConfig(SingleAlgorithm.class.getName()));
result.setTableStrategy(getTableStrategyConfigForExpression());
Map<String, String> autoIncrementColumnMap = new HashMap<>();
autoIncrementColumnMap.put("order_id", null);
autoIncrementColumnMap.put("order_item_id", "com.dangdang.ddframe.rdb.sharding.config.common.fixture.DecrementIdGenerator");
result.setAutoIncrementColumns(autoIncrementColumnMap);
return result;
}
......
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package com.dangdang.ddframe.rdb.sharding.config.common.fixture;
import com.dangdang.ddframe.rdb.sharding.id.generator.IdGenerator;
import java.util.concurrent.atomic.AtomicInteger;
abstract class AbstractNumberIdGenerator implements IdGenerator{
final AtomicInteger sequence = new AtomicInteger(100);
@Override
public void initContext(final String tableName, final String columnName) {
}
}
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package com.dangdang.ddframe.rdb.sharding.config.common.fixture;
public class DecrementIdGenerator extends AbstractNumberIdGenerator{
@Override
public Object generateId() {
return sequence.decrementAndGet();
}
}
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package com.dangdang.ddframe.rdb.sharding.config.common.fixture;
public class IncrementIdGenerator extends AbstractNumberIdGenerator{
@Override
public Object generateId() {
return sequence.incrementAndGet();
}
}
......@@ -61,4 +61,12 @@ public final class ShardingJdbcDataSourceBeanDefinitionParserTag {
public static final String DEFAULT_DATABASE_STRATEGY_ATTRIBUTE = "default-database-strategy";
public static final String DEFAULT_TABLE_STRATEGY_ATTRIBUTE = "default-table-strategy";
public static final String AUTO_INCREMENT_COLUMN = "auto-increment-column";
public static final String COLUMN_NAME = "column-name";
public static final String COLUMN_ID_GENERATOR_CLASS = "column-id-generator-class";
public static final String ID_GENERATOR_CLASS = "id-generator-class";
}
......@@ -65,9 +65,17 @@ public class ShardingJdbcDataSourceBeanDefinitionParser extends AbstractBeanDefi
factory.addPropertyValue("bindingTables", parseBindingTablesConfig(shardingRuleElement));
factory.addPropertyValue("defaultDatabaseStrategy", parseDefaultDatabaseStrategyConfig(shardingRuleElement));
factory.addPropertyValue("defaultTableStrategy", parseDefaultTableStrategyConfig(shardingRuleElement));
parseIdGenerator(factory, shardingRuleElement);
return factory.getBeanDefinition();
}
private void parseIdGenerator(final BeanDefinitionBuilder factory, final Element element) {
String idGeneratorClass = element.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.ID_GENERATOR_CLASS);
if (!Strings.isNullOrEmpty(idGeneratorClass)) {
factory.addPropertyValue("idGeneratorClass", idGeneratorClass);
}
}
private Map<String, BeanDefinition> parseDataSources(final Element element, final ParserContext parserContext) {
List<String> dataSources = Splitter.on(",").trimResults().splitToList(element.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.DATA_SOURCES_TAG));
Map<String, BeanDefinition> result = new ManagedMap<>(dataSources.size());
......@@ -116,6 +124,16 @@ public class ShardingJdbcDataSourceBeanDefinitionParser extends AbstractBeanDefi
if (!Strings.isNullOrEmpty(tableStrategy)) {
factory.addPropertyReference("tableStrategy", tableStrategy);
}
List<Element> autoIncrementColumns = DomUtils.getChildElementsByTagName(tableElement, ShardingJdbcDataSourceBeanDefinitionParserTag.AUTO_INCREMENT_COLUMN);
if (null == autoIncrementColumns || autoIncrementColumns.isEmpty()) {
return factory.getBeanDefinition();
}
Map<String, String> autoIncrementColumnMap = new ManagedMap<>(autoIncrementColumns.size());
for (Element each : autoIncrementColumns) {
autoIncrementColumnMap.put(each.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.COLUMN_NAME),
Strings.emptyToNull(each.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.COLUMN_ID_GENERATOR_CLASS)));
}
factory.addPropertyValue("autoIncrementColumns", autoIncrementColumnMap);
return factory.getBeanDefinition();
}
......
......@@ -25,6 +25,7 @@
</xsd:sequence>
<xsd:attribute name="data-sources" type="xsd:string" use="required" />
<xsd:attribute name="default-data-source" type="xsd:string" use="optional" />
<xsd:attribute name="id-generator-class" type="xsd:string" use="optional" />
</xsd:complexType>
</xsd:element>
<xsd:element name="table-rules">
......@@ -36,6 +37,9 @@
</xsd:element>
<xsd:element name="table-rule">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="auto-increment-column" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="logic-table" type="xsd:string" use="required" />
<xsd:attribute name="dynamic" type="xsd:string" use="optional" />
<xsd:attribute name="actual-tables" type="xsd:string" use="optional" />
......@@ -44,6 +48,12 @@
<xsd:attribute name="table-strategy" type="xsd:string" use="optional" />
</xsd:complexType>
</xsd:element>
<xsd:element name="auto-increment-column">
<xsd:complexType>
<xsd:attribute name="column-name" type="xsd:string" use="required"/>
<xsd:attribute name="column-id-generator-class" type="xsd:string" use="optional"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="binding-table-rules">
<xsd:complexType>
<xsd:sequence>
......
......@@ -17,6 +17,7 @@
package com.dangdang.ddframe.rdb.sharding;
import com.dangdang.ddframe.rdb.sharding.spring.AutoIncrementDBUnitTest;
import com.dangdang.ddframe.rdb.sharding.spring.cases.WithoutNamespaceDefaultStrategyTest;
import com.dangdang.ddframe.rdb.sharding.spring.cases.WithoutNamespaceTest;
import com.dangdang.ddframe.rdb.sharding.spring.cases.namespace.WithNamespaceAlgorithmClassAndPropsTest;
......@@ -45,7 +46,8 @@ import org.junit.runners.Suite.SuiteClasses;
WithoutNamespaceDefaultStrategyTest.class,
WithNamespaceDifferentTablesTest.class,
WithNamespaceForIndicatedDataSourceNamesTest.class,
WithNamespaceForMasterSlaveTest.class
WithNamespaceForMasterSlaveTest.class,
AutoIncrementDBUnitTest.class,
})
public class AllSpringTests {
}
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package com.dangdang.ddframe.rdb.sharding.spring;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@ContextConfiguration(locations = "classpath:META-INF/rdb/namespace/withNamespaceAutoIncrementColumns.xml")
public class AutoIncrementDBUnitTest extends AbstractSpringDBUnitTest{
@Test
public void test() throws SQLException {
try (Connection connection = getShardingDataSource().getConnection();
Statement statement = connection.createStatement()) {
statement.execute("INSERT INTO `t_order` (`user_id`, `status`) VALUES (1, 'init')", Statement.RETURN_GENERATED_KEYS);
assertTrue(statement.getGeneratedKeys().next());
assertEquals(statement.getGeneratedKeys().getLong(1), 101L);
statement.execute("INSERT INTO `t_order_item` (`order_id`, `user_id`, `status`) VALUES (101, 1, 'init')", Statement.RETURN_GENERATED_KEYS);
assertTrue(statement.getGeneratedKeys().next());
assertEquals(statement.getGeneratedKeys().getLong(1), 99L);
}
}
}
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package com.dangdang.ddframe.rdb.sharding.spring.fixture;
import com.dangdang.ddframe.rdb.sharding.id.generator.IdGenerator;
import java.util.concurrent.atomic.AtomicInteger;
abstract class AbstractNumberIdGenerator implements IdGenerator{
final AtomicInteger sequence = new AtomicInteger(100);
@Override
public void initContext(final String tableName, final String columnName) {
}
}
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package com.dangdang.ddframe.rdb.sharding.spring.fixture;
public class DecrementIdGenerator extends AbstractNumberIdGenerator{
@Override
public Object generateId() {
return sequence.decrementAndGet();
}
}
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package com.dangdang.ddframe.rdb.sharding.spring.fixture;
public class IncrementIdGenerator extends AbstractNumberIdGenerator{
@Override
public Object generateId() {
return sequence.incrementAndGet();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 1999-2015 dangdang.com.
~ <p>
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~ </p>
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:rdb="http://www.dangdang.com/schema/ddframe/rdb"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.dangdang.com/schema/ddframe/rdb
http://www.dangdang.com/schema/ddframe/rdb/rdb.xsd
">
<context:property-placeholder location="classpath:conf/rdb/conf.properties" ignore-unresolvable="true"/>
<bean id="dbtbl_0" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:mem:dbtbl_0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<bean id="dbtbl_1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:mem:dbtbl_1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<rdb:strategy id="databaseStrategy" sharding-columns="user_id" algorithm-class="com.dangdang.ddframe.rdb.sharding.spring.algorithm.SingleKeyModuloDatabaseShardingAlgorithm"/>
<rdb:strategy id="tableStrategy" sharding-columns="order_id" algorithm-class="com.dangdang.ddframe.rdb.sharding.spring.algorithm.SingleKeyModuloTableShardingAlgorithm"/>
<rdb:data-source id="shardingDataSource">
<rdb:sharding-rule data-sources="dbtbl_0,dbtbl_1" default-data-source="dbtbl_0" id-generator-class="com.dangdang.ddframe.rdb.sharding.spring.fixture.IncrementIdGenerator">
<rdb:table-rules>
<rdb:table-rule logic-table="t_order" actual-tables="t_order_${0..3}" database-strategy="databaseStrategy" table-strategy="tableStrategy">
<rdb:auto-increment-column column-name="order_id"/>
</rdb:table-rule>
<rdb:table-rule logic-table="t_order_item" actual-tables="t_order_item_${0..3}" database-strategy="databaseStrategy" table-strategy="tableStrategy">
<rdb:auto-increment-column column-name="order_item_id" column-id-generator-class="com.dangdang.ddframe.rdb.sharding.spring.fixture.DecrementIdGenerator"/>
</rdb:table-rule>
</rdb:table-rules>
</rdb:sharding-rule>
</rdb:data-source>
</beans>
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package com.dangdang.ddframe.rdb.sharding.config.yaml.fixture;
import com.dangdang.ddframe.rdb.sharding.id.generator.IdGenerator;
import java.util.concurrent.atomic.AtomicInteger;
abstract class AbstractNumberIdGenerator implements IdGenerator{
final AtomicInteger sequence = new AtomicInteger(100);
@Override
public void initContext(final String tableName, final String columnName) {
}
}
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package com.dangdang.ddframe.rdb.sharding.config.yaml.fixture;
public class DecrementIdGenerator extends AbstractNumberIdGenerator{
@Override
public Object generateId() {
return sequence.decrementAndGet();
}
}
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package com.dangdang.ddframe.rdb.sharding.config.yaml.fixture;
public class IncrementIdGenerator extends AbstractNumberIdGenerator{
@Override
public Object generateId() {
return sequence.incrementAndGet();
}
}
......@@ -13,6 +13,8 @@ dataSource:
maxActive: 100
defaultDataSourceName: db0
idGeneratorClass: com.dangdang.ddframe.rdb.sharding.config.yaml.fixture.IncrementIdGenerator
tables:
config:
......@@ -21,6 +23,8 @@ tables:
t_order:
actualTables: t_order_${0..1}
autoIncrementColumns:
order_id:
databaseStrategy: &db001
shardingColumns: order_id
algorithmClassName: com.dangdang.ddframe.rdb.sharding.config.yaml.algorithm.SingleAlgorithm
......@@ -30,6 +34,8 @@ tables:
t_order_item:
actualTables: t_order_item_${0..1}
autoIncrementColumns:
order_item_id: com.dangdang.ddframe.rdb.sharding.config.yaml.fixture.DecrementIdGenerator
#绑定表中其余的表的策略与第一张表的策略相同
databaseStrategy: *db001
tableStrategy: *table001
......
......@@ -129,7 +129,10 @@ public class ShardingStatement extends AbstractStatementAdapter {
try {
return generateExecutor(sql).executeUpdate(autoGeneratedKeys);
} finally {
generatedKeyContext.setAutoGeneratedKeys(autoGeneratedKeys);
if (null != generatedKeyContext) {
generatedKeyContext.setAutoGeneratedKeys(autoGeneratedKeys);
}
clearRouteContext();
}
}
......@@ -139,7 +142,9 @@ public class ShardingStatement extends AbstractStatementAdapter {
try {
return generateExecutor(sql).executeUpdate(columnIndexes);
} finally {
generatedKeyContext.setColumnIndexes(columnIndexes);
if (null != generatedKeyContext) {
generatedKeyContext.setColumnIndexes(columnIndexes);
}
clearRouteContext();
}
}
......@@ -149,7 +154,9 @@ public class ShardingStatement extends AbstractStatementAdapter {
try {
return generateExecutor(sql).executeUpdate(columnNames);
} finally {
generatedKeyContext.setColumnNames(columnNames);
if (null != generatedKeyContext) {
generatedKeyContext.setColumnNames(columnNames);
}
clearRouteContext();
}
}
......@@ -168,7 +175,9 @@ public class ShardingStatement extends AbstractStatementAdapter {
try {
return generateExecutor(sql).execute(autoGeneratedKeys);
} finally {
generatedKeyContext.setAutoGeneratedKeys(autoGeneratedKeys);
if (null != generatedKeyContext) {
generatedKeyContext.setAutoGeneratedKeys(autoGeneratedKeys);
}
clearRouteContext();
}
}
......@@ -178,7 +187,9 @@ public class ShardingStatement extends AbstractStatementAdapter {
try {
return generateExecutor(sql).execute(columnIndexes);
} finally {
generatedKeyContext.setColumnIndexes(columnIndexes);
if (null != generatedKeyContext) {
generatedKeyContext.setColumnIndexes(columnIndexes);
}
clearRouteContext();
}
}
......@@ -188,7 +199,9 @@ public class ShardingStatement extends AbstractStatementAdapter {
try {
return generateExecutor(sql).execute(columnNames);
} finally {
generatedKeyContext.setColumnNames(columnNames);
if (null != generatedKeyContext) {
generatedKeyContext.setColumnNames(columnNames);
}
clearRouteContext();
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册