Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
76a65674
Shardingsphere
项目概览
apache
/
Shardingsphere
通知
56
Star
3
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Shardingsphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
76a65674
编写于
6月 02, 2016
作者:
T
terrymanu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix #95
上级
735744b4
变更
12
隐藏空白更改
内联
并排
Showing
12 changed file
with
45 addition
and
38 deletion
+45
-38
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/hint/HintManagerHolder.java
...dangdang/ddframe/rdb/sharding/hint/HintManagerHolder.java
+0
-11
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/MasterSlaveDataSource.java
...dang/ddframe/rdb/sharding/jdbc/MasterSlaveDataSource.java
+17
-2
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/ShardingConnection.java
...angdang/ddframe/rdb/sharding/jdbc/ShardingConnection.java
+8
-0
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/adapter/AbstractConnectionAdapter.java
.../rdb/sharding/jdbc/adapter/AbstractConnectionAdapter.java
+4
-4
sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/integrate/masterslave/AbstractShardingMasterSlaveDBUnitTest.java
...te/masterslave/AbstractShardingMasterSlaveDBUnitTest.java
+1
-0
sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/integrate/masterslave/pstatement/ShardingMasterSlaveForPStatementWithDMLTest.java
...tatement/ShardingMasterSlaveForPStatementWithDMLTest.java
+1
-1
sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/integrate/masterslave/statement/ShardingMasterSlaveForStatementWithDMLTest.java
...statement/ShardingMasterSlaveForStatementWithDMLTest.java
+1
-1
sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/executor/PreparedStatementExecutorTest.java
.../rdb/sharding/executor/PreparedStatementExecutorTest.java
+1
-1
sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/hint/HintManagerHolderTest.java
...dang/ddframe/rdb/sharding/hint/HintManagerHolderTest.java
+0
-16
sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/jdbc/MasterSlaveDataSourceTest.java
.../ddframe/rdb/sharding/jdbc/MasterSlaveDataSourceTest.java
+9
-0
sharding-jdbc-doc/content/post/master_slave.md
sharding-jdbc-doc/content/post/master_slave.md
+1
-1
sharding-jdbc-doc/content/post/release_notes.md
sharding-jdbc-doc/content/post/release_notes.md
+2
-1
未找到文件。
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/hint/HintManagerHolder.java
浏览文件 @
76a65674
...
...
@@ -81,17 +81,6 @@ public final class HintManagerHolder {
return
null
!=
HINT_MANAGER_HOLDER
.
get
()
&&
HINT_MANAGER_HOLDER
.
get
().
isMasterRouteOnly
();
}
/**
* 设置数据库操作只路由至主库.
*/
public
static
void
setMasterRouteOnly
()
{
if
(
null
!=
HINT_MANAGER_HOLDER
.
get
())
{
HINT_MANAGER_HOLDER
.
get
().
setMasterRouteOnly
();
}
else
{
HintManager
.
getInstance
().
setMasterRouteOnly
();
}
}
/**
* 清理线索分片管理器的本地线程持有者.
*/
...
...
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/MasterSlaveDataSource.java
浏览文件 @
76a65674
...
...
@@ -38,6 +38,14 @@ import java.util.List;
@RequiredArgsConstructor
public
final
class
MasterSlaveDataSource
extends
AbstractDataSourceAdapter
{
private
static
final
ThreadLocal
<
Boolean
>
DML_FLAG
=
new
ThreadLocal
<
Boolean
>()
{
@Override
protected
Boolean
initialValue
()
{
return
false
;
}
};
private
final
String
name
;
private
final
DataSource
masterDataSource
;
...
...
@@ -53,8 +61,8 @@ public final class MasterSlaveDataSource extends AbstractDataSourceAdapter {
* @return 主或从节点的数据源
*/
public
DataSource
getDataSource
(
final
SQLStatementType
sqlStatementType
)
{
if
(
SQLStatementType
.
SELECT
!=
sqlStatementType
||
HintManagerHolder
.
isMasterRouteOnly
())
{
HintManagerHolder
.
setMasterRouteOnly
(
);
if
(
SQLStatementType
.
SELECT
!=
sqlStatementType
||
DML_FLAG
.
get
()
||
HintManagerHolder
.
isMasterRouteOnly
())
{
DML_FLAG
.
set
(
true
);
return
masterDataSource
;
}
return
slaveLoadBalanceStrategy
.
getDataSource
(
name
,
slaveDataSources
);
...
...
@@ -79,4 +87,11 @@ public final class MasterSlaveDataSource extends AbstractDataSourceAdapter {
public
Connection
getConnection
()
throws
SQLException
{
throw
new
UnsupportedOperationException
(
"Master slave data source cannot support get connection directly."
);
}
/**
* 重置更新标记.
*/
public
static
void
resetDMLFlag
()
{
DML_FLAG
.
remove
();
}
}
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/ShardingConnection.java
浏览文件 @
76a65674
...
...
@@ -18,6 +18,7 @@
package
com.dangdang.ddframe.rdb.sharding.jdbc
;
import
com.codahale.metrics.Timer.Context
;
import
com.dangdang.ddframe.rdb.sharding.hint.HintManagerHolder
;
import
com.dangdang.ddframe.rdb.sharding.jdbc.adapter.AbstractConnectionAdapter
;
import
com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext
;
import
com.dangdang.ddframe.rdb.sharding.parser.result.router.SQLStatementType
;
...
...
@@ -132,4 +133,11 @@ public final class ShardingConnection extends AbstractConnectionAdapter {
public
Collection
<
Connection
>
getConnections
()
{
return
connectionMap
.
values
();
}
@Override
public
void
close
()
throws
SQLException
{
super
.
close
();
HintManagerHolder
.
clear
();
MasterSlaveDataSource
.
resetDMLFlag
();
}
}
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/adapter/AbstractConnectionAdapter.java
浏览文件 @
76a65674
...
...
@@ -17,15 +17,15 @@
package
com.dangdang.ddframe.rdb.sharding.jdbc.adapter
;
import
com.dangdang.ddframe.rdb.sharding.jdbc.unsupported.AbstractUnsupportedOperationConnection
;
import
com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext
;
import
java.sql.Connection
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.SQLWarning
;
import
java.util.Collection
;
import
com.dangdang.ddframe.rdb.sharding.jdbc.unsupported.AbstractUnsupportedOperationConnection
;
import
com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext
;
/**
* 数据库连接适配类.
*
...
...
@@ -75,7 +75,7 @@ public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOpera
}
@Override
public
final
void
close
()
throws
SQLException
{
public
void
close
()
throws
SQLException
{
for
(
Connection
each
:
getConnections
())
{
each
.
close
();
}
...
...
sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/integrate/masterslave/AbstractShardingMasterSlaveDBUnitTest.java
浏览文件 @
76a65674
...
...
@@ -47,6 +47,7 @@ public abstract class AbstractShardingMasterSlaveDBUnitTest extends AbstractDBUn
@After
public
void
reset
()
throws
NoSuchFieldException
,
IllegalAccessException
{
HintManagerHolder
.
clear
();
MasterSlaveDataSource
.
resetDMLFlag
();
}
@Override
...
...
sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/integrate/masterslave/pstatement/ShardingMasterSlaveForPStatementWithDMLTest.java
浏览文件 @
76a65674
...
...
@@ -181,7 +181,7 @@ public final class ShardingMasterSlaveForPStatementWithDMLTest extends AbstractS
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
for
(
int
j
=
0
;
j
<
10
;
j
++)
{
assertDataSet
(
String
.
format
(
"integrate/dataset/masterslave/expect/%s/master_%s.xml"
,
expectedDataSetPattern
,
i
),
shardingDataSource
.
getConnection
().
getConnection
(
String
.
format
(
"ms_%s"
,
i
),
SQLStatementType
.
SELEC
T
),
shardingDataSource
.
getConnection
().
getConnection
(
String
.
format
(
"ms_%s"
,
i
),
SQLStatementType
.
INSER
T
),
String
.
format
(
"t_order_%s"
,
j
),
String
.
format
(
"SELECT * FROM `t_order_%s` WHERE `status`=?"
,
j
),
status
);
}
}
...
...
sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/integrate/masterslave/statement/ShardingMasterSlaveForStatementWithDMLTest.java
浏览文件 @
76a65674
...
...
@@ -84,7 +84,7 @@ public final class ShardingMasterSlaveForStatementWithDMLTest extends AbstractSh
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
for
(
int
j
=
0
;
j
<
10
;
j
++)
{
assertDataSet
(
String
.
format
(
"integrate/dataset/masterslave/expect/%s/master_%s.xml"
,
expectedDataSetPattern
,
i
),
shardingDataSource
.
getConnection
().
getConnection
(
String
.
format
(
"ms_%s"
,
i
),
SQLStatementType
.
SELEC
T
),
shardingDataSource
.
getConnection
().
getConnection
(
String
.
format
(
"ms_%s"
,
i
),
SQLStatementType
.
INSER
T
),
String
.
format
(
"t_order_%s"
,
j
),
String
.
format
(
"SELECT * FROM `t_order_%s` WHERE `status`=?"
,
j
),
status
);
}
}
...
...
sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/executor/PreparedStatementExecutorTest.java
浏览文件 @
76a65674
...
...
@@ -121,7 +121,7 @@ public final class PreparedStatementExecutorTest {
SQLException
exp
=
new
SQLException
();
when
(
preparedStatement
.
executeQuery
()).
thenThrow
(
exp
);
PreparedStatementExecutor
actual
=
new
PreparedStatementExecutor
(
executorEngine
,
Collections
.
singleton
(
wrapper
));
assertThat
(
actual
.
executeQuery
(),
is
(
Collections
.
singletonList
((
(
ResultSet
)
null
)
)));
assertThat
(
actual
.
executeQuery
(),
is
(
Collections
.
singletonList
((
ResultSet
)
null
)));
verify
(
preparedStatement
).
executeQuery
();
verify
(
eventCaller
,
times
(
2
)).
verifyDataSource
(
"ds_0"
);
verify
(
eventCaller
,
times
(
2
)).
verifySQL
(
"SELECT * FROM dual"
);
...
...
sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/hint/HintManagerHolderTest.java
浏览文件 @
76a65674
...
...
@@ -83,22 +83,6 @@ public final class HintManagerHolderTest {
assertFalse
(
HintManagerHolder
.
isUseShardingHint
());
}
@Test
public
void
assertSetMasterRouteOnlyWithHint
()
{
HintManagerHolder
.
setMasterRouteOnly
();
assertTrue
(
HintManagerHolder
.
isMasterRouteOnly
());
assertFalse
(
HintManagerHolder
.
isUseShardingHint
());
}
@Test
public
void
assertSetMasterRouteOnlyWithoutHint
()
{
hintManager
.
close
();
assertFalse
(
HintManagerHolder
.
isMasterRouteOnly
());
HintManagerHolder
.
setMasterRouteOnly
();
assertTrue
(
HintManagerHolder
.
isMasterRouteOnly
());
assertFalse
(
HintManagerHolder
.
isUseShardingHint
());
}
@Test
public
void
assertClear
()
{
hintManager
.
addDatabaseShardingValue
(
"logicTable"
,
"shardingColumn"
,
1
);
...
...
sharding-jdbc-core/src/test/java/com/dangdang/ddframe/rdb/sharding/jdbc/MasterSlaveDataSourceTest.java
浏览文件 @
76a65674
...
...
@@ -50,6 +50,7 @@ public final class MasterSlaveDataSourceTest {
@After
public
void
reset
()
{
HintManagerHolder
.
clear
();
MasterSlaveDataSource
.
resetDMLFlag
();
}
@Test
...
...
@@ -121,4 +122,12 @@ public final class MasterSlaveDataSourceTest {
public
void
assertGetConnection
()
throws
SQLException
{
masterSlaveDataSource
.
getConnection
();
}
@Test
public
void
assertResetDMLFlag
()
{
assertThat
(
masterSlaveDataSource
.
getDataSource
(
SQLStatementType
.
INSERT
),
is
(
masterDataSource
));
assertThat
(
masterSlaveDataSource
.
getDataSource
(
SQLStatementType
.
SELECT
),
is
(
masterDataSource
));
MasterSlaveDataSource
.
resetDMLFlag
();
assertThat
(
masterSlaveDataSource
.
getDataSource
(
SQLStatementType
.
SELECT
),
is
(
slaveDataSource
));
}
}
sharding-jdbc-doc/content/post/master_slave.md
浏览文件 @
76a65674
...
...
@@ -10,7 +10,7 @@ weight = 6
## 支持项
1.
提供了一主多从的读写分离配置,可配合分库分表使用。
1.
同一
`HintManager`
生命周期内,如有写入操作,以后的读操作均从主库读取,用于保证同一线程中的
数据一致性。
1.
同一
线程且同一数据库连接内,如有写入操作,以后的读操作均从主库读取,用于保证
数据一致性。
1.
`Spring`
命名空间。
1.
基于
`Hint`
的强制主库路由。
...
...
sharding-jdbc-doc/content/post/release_notes.md
浏览文件 @
76a65674
...
...
@@ -11,11 +11,12 @@ weight = 1
### 功能提升
1.
[
ISSUE #91
](
https://github.com/dangdangdotcom/sharding-jdbc/issues/91
)
开放对Statement.getGeneratedKeys的支持,可返回原生的数据库自增主键
1.
[
ISSUE #9
1
](
https://github.com/dangdangdotcom/sharding-jdbc/issues/92
)
查询类DQL语句事件发送
1.
[
ISSUE #9
2
](
https://github.com/dangdangdotcom/sharding-jdbc/issues/92
)
查询类DQL语句事件发送
### 缺陷修正
1.
[
ISSUE #89
](
https://github.com/dangdangdotcom/sharding-jdbc/issues/89
)
读写分离和分片的hint一起使用导致冲突
1.
[
ISSUE #95
](
https://github.com/dangdangdotcom/sharding-jdbc/issues/95
)
同一线程内写入操作后的读操作均从主库读取改为同一线程且同一连接内
## 1.3.0
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录