Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
5a2c152e
提交
5a2c152e
authored
9月 24, 2008
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Linked tables that point to the same database now share the connection.
上级
272423cb
隐藏空白字符变更
内嵌
并排
正在显示
8 个修改的文件
包含
118 行增加
和
67 行删除
+118
-67
Database.java
h2/src/main/org/h2/engine/Database.java
+18
-0
LinkedIndex.java
h2/src/main/org/h2/index/LinkedIndex.java
+64
-56
TableLink.java
h2/src/main/org/h2/table/TableLink.java
+21
-7
ObjectUtils.java
h2/src/main/org/h2/util/ObjectUtils.java
+10
-0
TestAll.java
h2/src/test/org/h2/test/TestAll.java
+0
-2
TestBase.java
h2/src/test/org/h2/test/TestBase.java
+1
-1
TestLinkedTable.java
h2/src/test/org/h2/test/db/TestLinkedTable.java
+3
-0
TestListener.java
h2/src/test/org/h2/test/db/TestListener.java
+1
-1
没有找到文件。
h2/src/main/org/h2/engine/Database.java
浏览文件 @
5a2c152e
...
...
@@ -49,6 +49,7 @@ import org.h2.table.IndexColumn;
import
org.h2.table.MetaTable
;
import
org.h2.table.Table
;
import
org.h2.table.TableData
;
import
org.h2.table.TableLinkConnection
;
import
org.h2.table.TableView
;
import
org.h2.tools.DeleteDbFiles
;
import
org.h2.tools.Server
;
...
...
@@ -160,6 +161,7 @@ public class Database implements DataHandler {
private
boolean
autoServerMode
;
private
Object
reserveMemory
;
private
Server
server
;
private
HashMap
linkConnections
;
public
Database
(
String
name
,
ConnectionInfo
ci
,
String
cipher
)
throws
SQLException
{
this
.
compareMode
=
new
CompareMode
(
null
,
null
,
0
);
...
...
@@ -2053,5 +2055,21 @@ public class Database implements DataHandler {
public
void
freeReserveMemory
()
{
reserveMemory
=
null
;
}
/**
* Open a new connection or get an existing connection to another database.
*
* @param driver the database driver or null
* @param url the database URL
* @param user the user name
* @param password the password
* @return the connection
*/
public
TableLinkConnection
getLinkConnection
(
String
driver
,
String
url
,
String
user
,
String
password
)
throws
SQLException
{
if
(
linkConnections
==
null
)
{
linkConnections
=
new
HashMap
();
}
return
TableLinkConnection
.
open
(
linkConnections
,
driver
,
url
,
user
,
password
);
}
}
h2/src/main/org/h2/index/LinkedIndex.java
浏览文件 @
5a2c152e
...
...
@@ -67,19 +67,21 @@ public class LinkedIndex extends BaseIndex {
}
buff
.
append
(
')'
);
String
sql
=
buff
.
toString
();
try
{
PreparedStatement
prep
=
link
.
getPreparedStatement
(
sql
);
for
(
int
i
=
0
,
j
=
0
;
i
<
row
.
getColumnCount
();
i
++)
{
Value
v
=
row
.
getValue
(
i
);
if
(
v
!=
null
&&
v
!=
ValueNull
.
INSTANCE
)
{
v
.
set
(
prep
,
j
+
1
);
j
++;
synchronized
(
link
.
getConnection
())
{
try
{
PreparedStatement
prep
=
link
.
getPreparedStatement
(
sql
);
for
(
int
i
=
0
,
j
=
0
;
i
<
row
.
getColumnCount
();
i
++)
{
Value
v
=
row
.
getValue
(
i
);
if
(
v
!=
null
&&
v
!=
ValueNull
.
INSTANCE
)
{
v
.
set
(
prep
,
j
+
1
);
j
++;
}
}
prep
.
executeUpdate
();
rowCount
++;
}
catch
(
SQLException
e
)
{
throw
wrapException
(
sql
,
e
);
}
prep
.
executeUpdate
();
rowCount
++;
}
catch
(
SQLException
e
)
{
throw
wrapException
(
sql
,
e
);
}
}
...
...
@@ -114,27 +116,29 @@ public class LinkedIndex extends BaseIndex {
}
buff
.
insert
(
0
,
"SELECT * FROM "
+
targetTableName
+
" T"
);
String
sql
=
buff
.
toString
();
try
{
PreparedStatement
prep
=
link
.
getPreparedStatement
(
sql
);
int
j
=
0
;
for
(
int
i
=
0
;
first
!=
null
&&
i
<
first
.
getColumnCount
();
i
++)
{
Value
v
=
first
.
getValue
(
i
);
if
(
v
!=
null
)
{
v
.
set
(
prep
,
j
+
1
);
j
++;
synchronized
(
link
.
getConnection
())
{
try
{
PreparedStatement
prep
=
link
.
getPreparedStatement
(
sql
);
int
j
=
0
;
for
(
int
i
=
0
;
first
!=
null
&&
i
<
first
.
getColumnCount
();
i
++)
{
Value
v
=
first
.
getValue
(
i
);
if
(
v
!=
null
)
{
v
.
set
(
prep
,
j
+
1
);
j
++;
}
}
}
for
(
int
i
=
0
;
last
!=
null
&&
i
<
last
.
getColumnCount
();
i
++)
{
Value
v
=
last
.
getValue
(
i
);
if
(
v
!=
null
)
{
v
.
set
(
prep
,
j
+
1
)
;
j
++;
for
(
int
i
=
0
;
last
!=
null
&&
i
<
last
.
getColumnCount
();
i
++)
{
Value
v
=
last
.
getValue
(
i
);
if
(
v
!=
null
)
{
v
.
set
(
prep
,
j
+
1
);
j
++
;
}
}
ResultSet
rs
=
prep
.
executeQuery
();
return
new
LinkedCursor
(
table
,
rs
,
session
);
}
catch
(
SQLException
e
)
{
throw
wrapException
(
sql
,
e
);
}
ResultSet
rs
=
prep
.
executeQuery
();
return
new
LinkedCursor
(
table
,
rs
,
session
);
}
catch
(
SQLException
e
)
{
throw
wrapException
(
sql
,
e
);
}
}
...
...
@@ -202,19 +206,21 @@ public class LinkedIndex extends BaseIndex {
}
}
String
sql
=
buff
.
toString
();
try
{
PreparedStatement
prep
=
link
.
getPreparedStatement
(
sql
);
for
(
int
i
=
0
,
j
=
0
;
i
<
row
.
getColumnCount
();
i
++)
{
Value
v
=
row
.
getValue
(
i
);
if
(!
isNull
(
v
))
{
v
.
set
(
prep
,
j
+
1
);
j
++;
synchronized
(
link
.
getConnection
())
{
try
{
PreparedStatement
prep
=
link
.
getPreparedStatement
(
sql
);
for
(
int
i
=
0
,
j
=
0
;
i
<
row
.
getColumnCount
();
i
++)
{
Value
v
=
row
.
getValue
(
i
);
if
(!
isNull
(
v
))
{
v
.
set
(
prep
,
j
+
1
);
j
++;
}
}
int
count
=
prep
.
executeUpdate
();
rowCount
-=
count
;
}
catch
(
SQLException
e
)
{
throw
wrapException
(
sql
,
e
);
}
int
count
=
prep
.
executeUpdate
();
rowCount
-=
count
;
}
catch
(
SQLException
e
)
{
throw
wrapException
(
sql
,
e
);
}
}
...
...
@@ -251,25 +257,27 @@ public class LinkedIndex extends BaseIndex {
}
}
String
sql
=
buff
.
toString
();
try
{
int
j
=
1
;
PreparedStatement
prep
=
link
.
getPreparedStatement
(
sql
);
for
(
int
i
=
0
;
i
<
newRow
.
getColumnCount
();
i
++)
{
newRow
.
getValue
(
i
).
set
(
prep
,
j
);
j
++;
}
for
(
int
i
=
0
;
i
<
oldRow
.
getColumnCount
();
i
++)
{
Value
v
=
oldRow
.
getValue
(
i
);
if
(!
isNull
(
v
))
{
v
.
set
(
prep
,
j
);
synchronized
(
link
.
getConnection
())
{
try
{
int
j
=
1
;
PreparedStatement
prep
=
link
.
getPreparedStatement
(
sql
);
for
(
int
i
=
0
;
i
<
newRow
.
getColumnCount
();
i
++)
{
newRow
.
getValue
(
i
).
set
(
prep
,
j
);
j
++;
}
for
(
int
i
=
0
;
i
<
oldRow
.
getColumnCount
();
i
++)
{
Value
v
=
oldRow
.
getValue
(
i
);
if
(!
isNull
(
v
))
{
v
.
set
(
prep
,
j
);
j
++;
}
}
int
count
=
prep
.
executeUpdate
();
// this has no effect but at least it allows to debug the update count
rowCount
=
rowCount
+
count
-
count
;
}
catch
(
SQLException
e
)
{
throw
wrapException
(
sql
,
e
);
}
int
count
=
prep
.
executeUpdate
();
// this has no effect but at least it allows to debug the update count
rowCount
=
rowCount
+
count
-
count
;
}
catch
(
SQLException
e
)
{
throw
wrapException
(
sql
,
e
);
}
}
...
...
h2/src/main/org/h2/table/TableLink.java
浏览文件 @
5a2c152e
...
...
@@ -6,7 +6,6 @@
*/
package
org
.
h2
.
table
;
import
java.sql.Connection
;
import
java.sql.DatabaseMetaData
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
...
...
@@ -40,7 +39,7 @@ import org.h2.value.DataType;
public
class
TableLink
extends
Table
{
private
String
driver
,
url
,
user
,
password
,
originalSchema
,
originalTable
,
qualifiedTableName
;
private
Connection
conn
;
private
TableLink
Connection
conn
;
private
HashMap
prepared
=
new
HashMap
();
private
final
ObjectArray
indexes
=
new
ObjectArray
();
private
final
boolean
emitUpdates
;
...
...
@@ -77,8 +76,20 @@ public class TableLink extends Table {
}
private
void
connect
()
throws
SQLException
{
conn
=
JdbcUtils
.
getConnection
(
driver
,
url
,
user
,
password
);
DatabaseMetaData
meta
=
conn
.
getMetaData
();
conn
=
database
.
getLinkConnection
(
driver
,
url
,
user
,
password
);
synchronized
(
conn
)
{
try
{
readMetaData
();
}
catch
(
SQLException
e
)
{
conn
.
close
();
conn
=
null
;
throw
e
;
}
}
}
private
void
readMetaData
()
throws
SQLException
{
DatabaseMetaData
meta
=
conn
.
getConnection
().
getMetaData
();
storesLowerCase
=
meta
.
storesLowerCaseIdentifiers
();
storesMixedCase
=
meta
.
storesMixedCaseIdentifiers
();
supportsMixedCaseIdentifiers
=
meta
.
supportsMixedCaseIdentifiers
();
...
...
@@ -129,7 +140,7 @@ public class TableLink extends Table {
// check if the table is accessible
Statement
stat
=
null
;
try
{
stat
=
conn
.
createStatement
();
stat
=
conn
.
getConnection
().
createStatement
();
rs
=
stat
.
executeQuery
(
"SELECT * FROM "
+
qualifiedTableName
+
" T WHERE 1=0"
);
if
(
columnList
.
size
()
==
0
)
{
// alternative solution
...
...
@@ -359,7 +370,7 @@ public class TableLink extends Table {
}
PreparedStatement
prep
=
(
PreparedStatement
)
prepared
.
get
(
sql
);
if
(
prep
==
null
)
{
prep
=
conn
.
prepareStatement
(
sql
);
prep
=
conn
.
getConnection
().
prepareStatement
(
sql
);
prepared
.
put
(
sql
,
prep
);
}
return
prep
;
...
...
@@ -399,7 +410,6 @@ public class TableLink extends Table {
database
.
removeMeta
(
session
,
getId
());
driver
=
null
;
url
=
user
=
password
=
originalTable
=
null
;
conn
=
null
;
prepared
=
null
;
invalidate
();
}
...
...
@@ -457,4 +467,8 @@ public class TableLink extends Table {
this
.
readOnly
=
readOnly
;
}
public
TableLinkConnection
getConnection
()
{
return
conn
;
}
}
h2/src/main/org/h2/util/ObjectUtils.java
浏览文件 @
5a2c152e
...
...
@@ -195,4 +195,14 @@ public class ObjectUtils {
}
}
/**
* Calculate the hash code of the given object. The object may be null.
*
* @param the object
* @return the hash code, or 0 if the object is null
*/
public
static
int
hashCode
(
Object
o
)
{
return
o
==
null
?
0
:
o
.
hashCode
();
}
}
h2/src/test/org/h2/test/TestAll.java
浏览文件 @
5a2c152e
...
...
@@ -276,8 +276,6 @@ java org.h2.test.TestAll timer
test on linux
main methods for the tests and make that work
TestMVCC:
Concurrent update in table test: another transaction has updated or
deleted the same row when exactly does it occur in other databases
...
...
h2/src/test/org/h2/test/TestBase.java
浏览文件 @
5a2c152e
...
...
@@ -41,7 +41,7 @@ public abstract class TestBase {
*/
protected
TestAll
config
;
pr
ivate
long
start
;
pr
otected
long
start
;
/**
* Get the test directory for this test.
...
...
h2/src/test/org/h2/test/db/TestLinkedTable.java
浏览文件 @
5a2c152e
...
...
@@ -73,6 +73,7 @@ public class TestLinkedTable extends TestBase {
sa
.
execute
(
"CREATE SCHEMA P"
);
sa
.
execute
(
"CREATE TABLE P.TEST(X INT)"
);
sa
.
execute
(
"INSERT INTO TEST VALUES(1)"
);
sa
.
execute
(
"INSERT INTO P.TEST VALUES(2)"
);
try
{
sb
.
execute
(
"CREATE LINKED TABLE T(NULL, 'jdbc:h2:mem:one', 'sa', 'sa', 'TEST')"
);
fail
();
...
...
@@ -81,6 +82,8 @@ public class TestLinkedTable extends TestBase {
}
sb
.
execute
(
"CREATE LINKED TABLE T(NULL, 'jdbc:h2:mem:one', 'sa', 'sa', 'PUBLIC', 'TEST')"
);
sb
.
execute
(
"CREATE LINKED TABLE T2(NULL, 'jdbc:h2:mem:one', 'sa', 'sa', 'P', 'TEST')"
);
assertSingleValue
(
sb
,
"SELECT * FROM T"
,
1
);
assertSingleValue
(
sb
,
"SELECT * FROM T2"
,
2
);
sa
.
execute
(
"DROP ALL OBJECTS"
);
sb
.
execute
(
"DROP ALL OBJECTS"
);
ca
.
close
();
...
...
h2/src/test/org/h2/test/db/TestListener.java
浏览文件 @
5a2c152e
...
...
@@ -21,7 +21,7 @@ import org.h2.util.JdbcUtils;
*/
public
class
TestListener
extends
TestBase
implements
DatabaseEventListener
{
private
long
last
,
start
;
private
long
last
;
private
int
lastState
=
-
1
;
private
String
url
;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论