Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
b628409b
提交
b628409b
authored
1月 18, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add TestIndex.testConcurrentUpdate()
上级
6b67928a
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
100 行增加
和
0 行删除
+100
-0
TestIndex.java
h2/src/test/org/h2/test/db/TestIndex.java
+100
-0
没有找到文件。
h2/src/test/org/h2/test/db/TestIndex.java
浏览文件 @
b628409b
...
...
@@ -11,7 +11,9 @@ import java.sql.ResultSet;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.Random
;
import
java.util.concurrent.atomic.AtomicInteger
;
import
org.h2.api.ErrorCode
;
import
org.h2.result.SortOrder
;
...
...
@@ -44,6 +46,7 @@ public class TestIndex extends TestBase {
testHashIndexOnMemoryTable
();
testErrorMessage
();
testDuplicateKeyException
();
testConcurrentUpdate
();
testNonUniqueHashIndex
();
testRenamePrimaryKey
();
testRandomized
();
...
...
@@ -187,6 +190,103 @@ public class TestIndex extends TestBase {
stat
.
execute
(
"drop table test"
);
}
private
class
ConcurrentUpdateThread
extends
Thread
{
private
final
AtomicInteger
concurrentUpdateId
,
concurrentUpdateValue
;
private
final
PreparedStatement
psInsert
,
psDelete
;
boolean
haveDuplicateKeyException
;
ConcurrentUpdateThread
(
Connection
c
,
AtomicInteger
concurrentUpdateId
,
AtomicInteger
concurrentUpdateValue
)
throws
SQLException
{
this
.
concurrentUpdateId
=
concurrentUpdateId
;
this
.
concurrentUpdateValue
=
concurrentUpdateValue
;
psInsert
=
c
.
prepareStatement
(
"insert into test(id, value) values (?, ?)"
);
psDelete
=
c
.
prepareStatement
(
"delete from test where value = ?"
);
}
@Override
public
void
run
()
{
for
(
int
i
=
0
;
i
<
10000
;
i
++)
{
try
{
if
(
Math
.
random
()
>
0.05
)
{
psInsert
.
setInt
(
1
,
concurrentUpdateId
.
incrementAndGet
());
psInsert
.
setInt
(
2
,
concurrentUpdateValue
.
get
());
psInsert
.
executeUpdate
();
}
else
{
psDelete
.
setInt
(
1
,
concurrentUpdateValue
.
get
());
psDelete
.
executeUpdate
();
}
}
catch
(
SQLException
ex
)
{
switch
(
ex
.
getErrorCode
())
{
case
23505
:
haveDuplicateKeyException
=
true
;
break
;
case
90131
:
// Unlikely but possible
break
;
default
:
ex
.
printStackTrace
();
}
}
if
(
Math
.
random
()
>
0.95
)
concurrentUpdateValue
.
incrementAndGet
();
}
}
}
private
void
testConcurrentUpdate
()
throws
SQLException
{
Connection
c
=
getConnection
(
"index"
);
Statement
stat
=
c
.
createStatement
();
stat
.
execute
(
"create table test(id int primary key, value int)"
);
stat
.
execute
(
"create unique index idx_value_name on test(value)"
);
PreparedStatement
check
=
c
.
prepareStatement
(
"select value from test"
);
ConcurrentUpdateThread
[]
threads
=
new
ConcurrentUpdateThread
[
4
];
AtomicInteger
concurrentUpdateId
=
new
AtomicInteger
(),
concurrentUpdateValue
=
new
AtomicInteger
();
// The same connection
for
(
int
i
=
0
;
i
<
threads
.
length
;
i
++)
{
threads
[
i
]
=
new
ConcurrentUpdateThread
(
c
,
concurrentUpdateId
,
concurrentUpdateValue
);
}
testConcurrentUpdateRun
(
threads
,
check
);
// Different connections
Connection
[]
connections
=
new
Connection
[
threads
.
length
];
for
(
int
i
=
0
;
i
<
threads
.
length
;
i
++)
{
Connection
c2
=
getConnection
(
"index"
);
connections
[
i
]
=
c2
;
threads
[
i
]
=
new
ConcurrentUpdateThread
(
c2
,
concurrentUpdateId
,
concurrentUpdateValue
);
}
testConcurrentUpdateRun
(
threads
,
check
);
for
(
Connection
c2
:
connections
)
{
c2
.
close
();
}
stat
.
execute
(
"drop table test"
);
c
.
close
();
}
void
testConcurrentUpdateRun
(
ConcurrentUpdateThread
[]
threads
,
PreparedStatement
check
)
throws
SQLException
{
for
(
ConcurrentUpdateThread
t
:
threads
)
{
t
.
start
();
}
boolean
haveDuplicateKeyException
=
false
;
for
(
ConcurrentUpdateThread
t
:
threads
)
{
try
{
t
.
join
();
haveDuplicateKeyException
|=
t
.
haveDuplicateKeyException
;
}
catch
(
InterruptedException
e
)
{
}
}
assertTrue
(
"haveDuplicateKeys"
,
haveDuplicateKeyException
);
HashSet
<
Integer
>
set
=
new
HashSet
<>();
try
(
ResultSet
rs
=
check
.
executeQuery
())
{
while
(
rs
.
next
())
{
if
(!
set
.
add
(
rs
.
getInt
(
1
)))
{
fail
(
"unique index violation"
);
}
}
}
}
private
void
testNonUniqueHashIndex
()
throws
SQLException
{
reconnect
();
stat
.
execute
(
"create memory table test(id bigint, data bigint)"
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论