forked from apache/shardingsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix apache#118 firstly execute DQL statement then execute DML stateme…
…nt, DML statement is executing in slave db
- Loading branch information
gaohongtao
committed
Oct 18, 2016
1 parent
4b71cae
commit 1a20287
Showing
6 changed files
with
174 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
...dbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/jdbc/ShardingConnectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* 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.jdbc; | ||
|
||
import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule; | ||
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule; | ||
import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule; | ||
import com.dangdang.ddframe.rdb.sharding.fixture.TestDataSource; | ||
import com.dangdang.ddframe.rdb.sharding.parser.result.router.SQLStatementType; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertNotSame; | ||
import static org.junit.Assert.assertSame; | ||
|
||
|
||
public class ShardingConnectionTest { | ||
|
||
private static final DataSource MASTER_DATA_SOURCE = new TestDataSource("test_ds_master"); | ||
|
||
private static final DataSource SLAVE_DATA_SOURCE = new TestDataSource("test_ds_slave"); | ||
|
||
private static final MasterSlaveDataSource MASTER_SLAVE_DATA_SOURCE = new MasterSlaveDataSource("test_ds", MASTER_DATA_SOURCE, Collections.singletonList(SLAVE_DATA_SOURCE)); | ||
|
||
private static final String DS_NAME = "default"; | ||
|
||
private ShardingConnection connection; | ||
|
||
@BeforeClass | ||
public static void init() { | ||
((TestDataSource) SLAVE_DATA_SOURCE).setThrowExceptionWhenClosing(true); | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
Map<String, DataSource> dataSourceMap = new HashMap<>(1); | ||
dataSourceMap.put(DS_NAME, MASTER_SLAVE_DATA_SOURCE); | ||
DataSourceRule dataSourceRule = new DataSourceRule(dataSourceMap); | ||
ShardingRule rule = new ShardingRule.ShardingRuleBuilder().dataSourceRule(dataSourceRule).tableRules(Collections.singleton(new TableRule.TableRuleBuilder("test").dataSourceRule(dataSourceRule).build())).build(); | ||
ShardingContext sc = new ShardingContext(rule, null, null); | ||
connection = new ShardingConnection(sc); | ||
} | ||
|
||
@After | ||
public void clear() { | ||
try { | ||
connection.close(); | ||
} catch (SQLException ignored) { | ||
} | ||
} | ||
|
||
@Test | ||
public void getConnectionSelectThenUpdate() throws Exception { | ||
assertNotSame(connection.getConnection(DS_NAME, SQLStatementType.SELECT), connection.getConnection(DS_NAME, SQLStatementType.UPDATE)); | ||
} | ||
|
||
@Test | ||
public void getConnectionUpdateThenSelect() throws Exception { | ||
assertSame(connection.getConnection(DS_NAME, SQLStatementType.UPDATE), connection.getConnection(DS_NAME, SQLStatementType.SELECT)); | ||
} | ||
|
||
@Test | ||
public void getConnectionBothSelect() throws Exception { | ||
assertSame(connection.getConnection(DS_NAME, SQLStatementType.SELECT), connection.getConnection(DS_NAME, SQLStatementType.SELECT)); | ||
} | ||
|
||
@Test | ||
public void getConnectionBothUpdate() throws Exception { | ||
assertSame(connection.getConnection(DS_NAME, SQLStatementType.UPDATE), connection.getConnection(DS_NAME, SQLStatementType.UPDATE)); | ||
} | ||
|
||
@Test | ||
public void getConnectionMixed() throws Exception { | ||
Connection slaveConnection = connection.getConnection(DS_NAME, SQLStatementType.SELECT); | ||
Connection masterConnection = connection.getConnection(DS_NAME, SQLStatementType.UPDATE); | ||
assertNotSame(slaveConnection, masterConnection); | ||
assertNotSame(slaveConnection, connection.getConnection(DS_NAME, SQLStatementType.SELECT)); | ||
assertNotSame(slaveConnection, connection.getConnection(DS_NAME, SQLStatementType.UPDATE)); | ||
assertSame(masterConnection, connection.getConnection(DS_NAME, SQLStatementType.SELECT)); | ||
assertSame(masterConnection, connection.getConnection(DS_NAME, SQLStatementType.UPDATE)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters