Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
f509ff65
提交
f509ff65
authored
18 年前
作者:
Thomas Mueller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
--no commit message
--no commit message
上级
2a22d077
显示空白字符变更
内嵌
并排
正在显示
10 个修改的文件
包含
1479 行增加
和
0 行删除
+1479
-0
JdbcConnectionListener.java
h2/src/main/org/h2/jdbcx/JdbcConnectionListener.java
+17
-0
JdbcDataSource.java
h2/src/main/org/h2/jdbcx/JdbcDataSource.java
+149
-0
JdbcDataSourceFactory.java
h2/src/main/org/h2/jdbcx/JdbcDataSourceFactory.java
+53
-0
JdbcXAConnection.java
h2/src/main/org/h2/jdbcx/JdbcXAConnection.java
+261
-0
JdbcXid.java
h2/src/main/org/h2/jdbcx/JdbcXid.java
+79
-0
InternalException.java
h2/src/main/org/h2/message/InternalException.java
+19
-0
Message.java
h2/src/main/org/h2/message/Message.java
+386
-0
Trace.java
h2/src/main/org/h2/message/Trace.java
+105
-0
TraceObject.java
h2/src/main/org/h2/message/TraceObject.java
+183
-0
TraceSystem.java
h2/src/main/org/h2/message/TraceSystem.java
+227
-0
没有找到文件。
h2/src/main/org/h2/jdbcx/JdbcConnectionListener.java
0 → 100644
浏览文件 @
f509ff65
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
jdbcx
;
import
java.sql.SQLException
;
import
org.h2.jdbc.JdbcConnection
;
public
interface
JdbcConnectionListener
{
// TODO pooled connection: make sure fatalErrorOccured is called in the right situations
void
fatalErrorOccured
(
JdbcConnection
conn
,
SQLException
e
)
throws
SQLException
;
void
closed
(
JdbcConnection
conn
);
}
This diff is collapsed.
Click to expand it.
h2/src/main/org/h2/jdbcx/JdbcDataSource.java
0 → 100644
浏览文件 @
f509ff65
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
jdbcx
;
import
java.io.PrintWriter
;
import
java.io.Serializable
;
import
java.sql.Connection
;
import
java.sql.SQLException
;
import
java.util.Properties
;
import
javax.naming.NamingException
;
import
javax.naming.Reference
;
import
javax.naming.Referenceable
;
import
javax.naming.StringRefAddr
;
import
javax.sql.ConnectionPoolDataSource
;
import
javax.sql.DataSource
;
import
javax.sql.PooledConnection
;
import
javax.sql.XAConnection
;
import
javax.sql.XADataSource
;
import
org.h2.jdbc.JdbcConnection
;
import
org.h2.message.TraceObject
;
/**
* A data source for H2 database connections
*
* @author Tom
*/
public
class
JdbcDataSource
extends
TraceObject
implements
XADataSource
,
DataSource
,
ConnectionPoolDataSource
,
Serializable
,
Referenceable
{
private
static
final
long
serialVersionUID
=
1288136338451857771L
;
private
JdbcDataSourceFactory
factory
;
private
int
timeout
;
private
PrintWriter
logWriter
;
private
String
user
;
private
String
password
;
private
String
url
;
public
JdbcDataSource
()
{
this
.
factory
=
new
JdbcDataSourceFactory
();
int
id
=
getNextId
(
TraceObject
.
DATASOURCE
);
setTrace
(
factory
.
getTrace
(),
TraceObject
.
DATASOURCE
,
id
);
}
public
int
getLoginTimeout
()
throws
SQLException
{
debugCodeCall
(
"getLoginTimeout"
);
return
timeout
;
}
public
void
setLoginTimeout
(
int
timeout
)
throws
SQLException
{
debugCodeCall
(
"setLoginTimeout"
,
timeout
);
this
.
timeout
=
timeout
;
}
public
PrintWriter
getLogWriter
()
throws
SQLException
{
debugCodeCall
(
"getLogWriter"
);
return
logWriter
;
}
public
void
setLogWriter
(
PrintWriter
out
)
throws
SQLException
{
debugCodeCall
(
"setLogWriter(out)"
);
logWriter
=
out
;
}
public
Connection
getConnection
()
throws
SQLException
{
debugCodeCall
(
"getConnection"
);
return
getJdbcConnection
(
user
,
password
);
}
public
Connection
getConnection
(
String
user
,
String
password
)
throws
SQLException
{
debugCode
(
"getConnection("
+
quote
(
user
)+
", "
+
quote
(
password
)+
");"
);
return
getJdbcConnection
(
user
,
password
);
}
public
JdbcConnection
getJdbcConnection
(
String
user
,
String
password
)
throws
SQLException
{
debugCode
(
"getJdbcConnection("
+
quote
(
user
)+
", "
+
quote
(
password
)+
");"
);
Properties
info
=
new
Properties
();
info
.
setProperty
(
"user"
,
user
);
info
.
setProperty
(
"password"
,
password
);
return
new
JdbcConnection
(
url
,
info
);
}
public
String
getURL
()
{
debugCodeCall
(
"getURL"
);
return
url
;
}
public
void
setURL
(
String
url
)
{
debugCodeCall
(
"setURL"
,
url
);
this
.
url
=
url
;
}
public
String
getPassword
()
{
debugCodeCall
(
"getPassword"
);
return
password
;
}
public
void
setPassword
(
String
password
)
{
debugCodeCall
(
"setPassword"
,
password
);
this
.
password
=
password
;
}
public
String
getUser
()
{
debugCodeCall
(
"getUser"
);
return
user
;
}
public
void
setUser
(
String
user
)
{
debugCodeCall
(
"setUser"
,
user
);
this
.
user
=
user
;
}
public
Reference
getReference
()
throws
NamingException
{
debugCodeCall
(
"getReference"
);
String
factoryClassName
=
JdbcDataSourceFactory
.
class
.
getName
();
Reference
ref
=
new
Reference
(
getClass
().
getName
(),
factoryClassName
,
null
);
ref
.
add
(
new
StringRefAddr
(
"url"
,
getURL
()));
ref
.
add
(
new
StringRefAddr
(
"user"
,
getUser
()));
ref
.
add
(
new
StringRefAddr
(
"password"
,
password
));
return
ref
;
}
public
XAConnection
getXAConnection
()
throws
SQLException
{
debugCodeCall
(
"getXAConnection"
);
int
id
=
getNextId
(
XA_DATASOURCE
);
return
new
JdbcXAConnection
(
factory
,
id
,
url
,
user
,
password
);
}
public
XAConnection
getXAConnection
(
String
user
,
String
password
)
throws
SQLException
{
debugCode
(
"getXAConnection("
+
quote
(
user
)+
", "
+
quote
(
password
)+
");"
);
int
id
=
getNextId
(
XA_DATASOURCE
);
return
new
JdbcXAConnection
(
factory
,
id
,
url
,
user
,
password
);
}
public
PooledConnection
getPooledConnection
()
throws
SQLException
{
debugCodeCall
(
"getPooledConnection"
);
return
getXAConnection
();
}
public
PooledConnection
getPooledConnection
(
String
user
,
String
password
)
throws
SQLException
{
debugCode
(
"getPooledConnection("
+
quote
(
user
)+
", "
+
quote
(
password
)+
");"
);
return
getXAConnection
(
user
,
password
);
}
}
This diff is collapsed.
Click to expand it.
h2/src/main/org/h2/jdbcx/JdbcDataSourceFactory.java
0 → 100644
浏览文件 @
f509ff65
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
jdbcx
;
import
java.util.Hashtable
;
import
javax.naming.Context
;
import
javax.naming.Name
;
import
javax.naming.Reference
;
import
javax.naming.spi.ObjectFactory
;
import
org.h2.engine.Constants
;
import
org.h2.message.Trace
;
import
org.h2.message.TraceSystem
;
public
class
JdbcDataSourceFactory
implements
ObjectFactory
{
private
static
TraceSystem
traceSystem
;
private
Trace
trace
;
static
{
traceSystem
=
new
TraceSystem
(
"h2datasource"
+
Constants
.
SUFFIX_TRACE_FILE
);
traceSystem
.
setLevelFile
(
TraceSystem
.
DEBUG
);
}
public
JdbcDataSourceFactory
()
{
trace
=
traceSystem
.
getTrace
(
"JDBCX"
);
}
public
synchronized
Object
getObjectInstance
(
Object
obj
,
Name
name
,
Context
nameCtx
,
Hashtable
environment
)
throws
Exception
{
trace
.
debug
(
"getObjectInstance obj="
+
obj
+
" name="
+
name
+
" nameCtx="
+
nameCtx
+
" environment="
+
environment
);
Reference
ref
=
(
Reference
)
obj
;
if
(
ref
.
getClassName
().
equals
(
JdbcDataSource
.
class
.
getName
()))
{
JdbcDataSource
dataSource
=
new
JdbcDataSource
();
dataSource
.
setURL
((
String
)
ref
.
get
(
"url"
).
getContent
());
dataSource
.
setUser
((
String
)
ref
.
get
(
"user"
).
getContent
());
dataSource
.
setPassword
((
String
)
ref
.
get
(
"password"
).
getContent
());
return
dataSource
;
}
return
null
;
}
TraceSystem
getTraceSystem
()
{
return
traceSystem
;
}
Trace
getTrace
()
{
return
trace
;
}
}
This diff is collapsed.
Click to expand it.
h2/src/main/org/h2/jdbcx/JdbcXAConnection.java
0 → 100644
浏览文件 @
f509ff65
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
jdbcx
;
import
java.sql.Connection
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.util.ArrayList
;
import
java.util.Properties
;
import
javax.sql.ConnectionEvent
;
import
javax.sql.ConnectionEventListener
;
import
javax.sql.XAConnection
;
import
javax.transaction.xa.XAException
;
import
javax.transaction.xa.XAResource
;
import
javax.transaction.xa.Xid
;
import
org.h2.jdbc.JdbcConnection
;
import
org.h2.message.TraceObject
;
import
org.h2.util.ByteUtils
;
public
class
JdbcXAConnection
extends
TraceObject
implements
XAConnection
,
JdbcConnectionListener
,
XAResource
{
private
JdbcDataSourceFactory
factory
;
private
String
url
,
user
,
password
;
private
JdbcConnection
conn
;
private
ArrayList
listeners
=
new
ArrayList
();
private
Xid
currentTransaction
;
JdbcXAConnection
(
JdbcDataSourceFactory
factory
,
int
id
,
String
url
,
String
user
,
String
password
)
{
this
.
factory
=
factory
;
setTrace
(
factory
.
getTrace
(),
TraceObject
.
XA_DATASOURCE
,
id
);
this
.
url
=
url
;
this
.
user
=
user
;
this
.
password
=
password
;
}
public
XAResource
getXAResource
()
throws
SQLException
{
debugCodeCall
(
"getXAResource"
);
return
this
;
}
public
void
close
()
throws
SQLException
{
debugCodeCall
(
"close"
);
if
(
conn
!=
null
)
{
conn
.
closeConnection
();
conn
=
null
;
}
}
public
Connection
getConnection
()
throws
SQLException
{
debugCodeCall
(
"getConnection"
);
close
();
Properties
info
=
new
Properties
();
info
.
setProperty
(
"user"
,
user
);
info
.
setProperty
(
"password"
,
password
);
conn
=
new
JdbcConnection
(
url
,
info
);
return
conn
;
}
public
void
addConnectionEventListener
(
ConnectionEventListener
listener
)
{
debugCode
(
"addConnectionEventListener(listener)"
);
listeners
.
add
(
listener
);
conn
.
setJdbcConnectionListener
(
this
);
}
public
void
removeConnectionEventListener
(
ConnectionEventListener
listener
)
{
debugCode
(
"removeConnectionEventListener(listener)"
);
listeners
.
remove
(
listener
);
}
public
void
fatalErrorOccured
(
JdbcConnection
conn
,
SQLException
e
)
throws
SQLException
{
debugCode
(
"fatalErrorOccured(conn, e)"
);
for
(
int
i
=
0
;
i
<
listeners
.
size
();
i
++)
{
ConnectionEventListener
listener
=
(
ConnectionEventListener
)
listeners
.
get
(
i
);
ConnectionEvent
event
=
new
ConnectionEvent
(
this
,
e
);
listener
.
connectionErrorOccurred
(
event
);
}
close
();
}
public
void
closed
(
JdbcConnection
conn
)
{
debugCode
(
"closed(conn)"
);
for
(
int
i
=
0
;
i
<
listeners
.
size
();
i
++)
{
ConnectionEventListener
listener
=
(
ConnectionEventListener
)
listeners
.
get
(
i
);
ConnectionEvent
event
=
new
ConnectionEvent
(
this
);
listener
.
connectionClosed
(
event
);
}
}
public
int
getTransactionTimeout
()
throws
XAException
{
debugCodeCall
(
"getTransactionTimeout"
);
return
0
;
}
public
boolean
setTransactionTimeout
(
int
seconds
)
throws
XAException
{
debugCodeCall
(
"setTransactionTimeout"
,
seconds
);
return
false
;
}
public
boolean
isSameRM
(
XAResource
xares
)
throws
XAException
{
debugCode
(
"isSameRM(xares)"
);
return
xares
==
this
;
}
public
Xid
[]
recover
(
int
flag
)
throws
XAException
{
debugCodeCall
(
"recover"
,
quoteFlags
(
flag
));
checkOpen
();
try
{
Statement
stat
=
conn
.
createStatement
();
ResultSet
rs
=
stat
.
executeQuery
(
"SELECT * FROM INFORMATION_SCHEMA.IN_DOUBT ORDER BY ID"
);
ArrayList
list
=
new
ArrayList
();
while
(
rs
.
next
())
{
String
tid
=
rs
.
getString
(
"TRANSACTION"
);
int
id
=
getNextId
(
XID
);
Xid
xid
=
new
JdbcXid
(
factory
,
id
,
tid
);
list
.
add
(
xid
);
}
Xid
[]
result
=
new
Xid
[
list
.
size
()];
list
.
toArray
(
result
);
return
result
;
}
catch
(
SQLException
e
)
{
getTrace
().
debug
(
"throw XAException.XAER_OUTSIDE"
,
e
);
throw
new
XAException
(
XAException
.
XAER_OUTSIDE
);
}
}
private
void
checkOpen
()
throws
XAException
{
if
(
conn
==
null
)
{
getTrace
().
debug
(
"conn==null"
);
throw
new
XAException
(
XAException
.
XAER_OUTSIDE
);
}
}
public
int
prepare
(
Xid
xid
)
throws
XAException
{
debugCode
(
"prepare("
+
quoteXid
(
xid
)+
")"
);
checkOpen
();
if
(
currentTransaction
!=
xid
)
{
getTrace
().
debug
(
"throw XAException.XAER_INVAL"
);
throw
new
XAException
(
XAException
.
XAER_INVAL
);
}
try
{
conn
.
createStatement
().
execute
(
"PREPARE COMMIT"
);
}
catch
(
SQLException
e
)
{
throw
convertException
(
e
);
}
getTrace
().
debug
(
"return TMSUCCESS"
);
return
TMSUCCESS
;
}
public
void
forget
(
Xid
xid
)
throws
XAException
{
debugCode
(
"forget("
+
quoteXid
(
xid
)+
")"
);
// TODO
}
public
void
rollback
(
Xid
xid
)
throws
XAException
{
debugCode
(
"rollback("
+
quoteXid
(
xid
)+
")"
);
try
{
conn
.
rollback
();
}
catch
(
SQLException
e
)
{
throw
convertException
(
e
);
}
getTrace
().
debug
(
"rolled back"
);
}
public
void
end
(
Xid
xid
,
int
flags
)
throws
XAException
{
debugCode
(
"end("
+
quoteXid
(
xid
)+
", "
+
quoteFlags
(
flags
)+
")"
);
if
(
flags
==
TMSUSPEND
)
{
return
;
}
if
(
currentTransaction
!=
xid
)
{
getTrace
().
debug
(
"throw XAException.XAER_OUTSIDE"
);
throw
new
XAException
(
XAException
.
XAER_OUTSIDE
);
}
getTrace
().
debug
(
"currentTransaction=null"
);
currentTransaction
=
null
;
}
private
String
quoteFlags
(
int
flags
)
{
StringBuffer
buff
=
new
StringBuffer
();
if
((
flags
&
XAResource
.
TMENDRSCAN
)
!=
0
)
{
buff
.
append
(
"|XAResource.TMENDRSCAN"
);
}
if
((
flags
&
XAResource
.
TMFAIL
)
!=
0
)
{
buff
.
append
(
"|XAResource.TMFAIL"
);
}
if
((
flags
&
XAResource
.
TMJOIN
)
!=
0
)
{
buff
.
append
(
"|XAResource.TMJOIN"
);
}
if
((
flags
&
XAResource
.
TMONEPHASE
)
!=
0
)
{
buff
.
append
(
"|XAResource.TMONEPHASE"
);
}
if
((
flags
&
XAResource
.
TMRESUME
)
!=
0
)
{
buff
.
append
(
"|XAResource.TMRESUME"
);
}
if
((
flags
&
XAResource
.
TMSTARTRSCAN
)
!=
0
)
{
buff
.
append
(
"|XAResource.TMSTARTRSCAN"
);
}
if
((
flags
&
XAResource
.
TMSUCCESS
)
!=
0
)
{
buff
.
append
(
"|XAResource.TMSUCCESS"
);
}
if
((
flags
&
XAResource
.
TMSUSPEND
)
!=
0
)
{
buff
.
append
(
"|XAResource.TMSUSPEND"
);
}
if
(
buff
.
length
()
==
0
)
{
buff
.
append
(
"|XAResource.TMNOFLAGS"
);
}
return
buff
.
toString
().
substring
(
1
);
}
private
String
quoteXid
(
Xid
xid
)
{
StringBuffer
buff
=
new
StringBuffer
();
buff
.
append
(
"\"f:"
);
buff
.
append
(
xid
.
getFormatId
());
buff
.
append
(
",bq:"
);
buff
.
append
(
ByteUtils
.
convertBytesToString
(
xid
.
getBranchQualifier
()));
buff
.
append
(
",gxid:"
);
buff
.
append
(
ByteUtils
.
convertBytesToString
(
xid
.
getGlobalTransactionId
()));
buff
.
append
(
",c:"
);
buff
.
append
(
xid
.
getClass
().
getName
());
buff
.
append
(
"\""
);
return
buff
.
toString
();
}
public
void
start
(
Xid
xid
,
int
flags
)
throws
XAException
{
debugCode
(
"start("
+
quoteXid
(
xid
)+
", "
+
quoteFlags
(
flags
)+
")"
);
if
(
flags
==
TMRESUME
)
{
return
;
}
if
(
currentTransaction
!=
null
)
{
getTrace
().
debug
(
"throw XAException.XAER_NOTA"
);
throw
new
XAException
(
XAException
.
XAER_NOTA
);
}
try
{
conn
.
setAutoCommit
(
false
);
}
catch
(
SQLException
e
)
{
throw
convertException
(
e
);
}
getTrace
().
debug
(
"currentTransaction=xid"
);
currentTransaction
=
xid
;
}
private
XAException
convertException
(
SQLException
e
)
{
getTrace
().
debug
(
"throw XAException("
+
e
.
getMessage
()+
")"
);
return
new
XAException
(
e
.
getMessage
());
}
public
void
commit
(
Xid
xid
,
boolean
onePhase
)
throws
XAException
{
debugCode
(
"commit("
+
quoteXid
(
xid
)+
", "
+
onePhase
+
")"
);
try
{
conn
.
commit
();
}
catch
(
SQLException
e
)
{
throw
convertException
(
e
);
}
getTrace
().
debug
(
"committed"
);
}
}
This diff is collapsed.
Click to expand it.
h2/src/main/org/h2/jdbcx/JdbcXid.java
0 → 100644
浏览文件 @
f509ff65
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
jdbcx
;
import
java.sql.SQLException
;
import
java.util.StringTokenizer
;
import
javax.transaction.xa.Xid
;
import
org.h2.message.Message
;
import
org.h2.message.TraceObject
;
import
org.h2.util.ByteUtils
;
public
class
JdbcXid
extends
TraceObject
implements
Xid
{
private
static
final
String
PREFIX
=
"XID"
;
private
int
formatId
;
private
byte
[]
branchQualifier
;
private
byte
[]
globalTransactionId
;
JdbcXid
(
JdbcDataSourceFactory
factory
,
int
id
,
String
tid
)
throws
SQLException
{
setTrace
(
factory
.
getTrace
(),
TraceObject
.
XID
,
id
);
try
{
StringTokenizer
tokenizer
=
new
StringTokenizer
(
tid
,
"_"
);
String
prefix
=
tokenizer
.
nextToken
();
if
(!
PREFIX
.
equals
(
prefix
))
{
throw
Message
.
getSQLException
(
Message
.
WRONG_XID_FORMAT_1
,
tid
);
}
formatId
=
Integer
.
parseInt
(
tokenizer
.
nextToken
());
branchQualifier
=
ByteUtils
.
convertStringToBytes
(
tokenizer
.
nextToken
());
globalTransactionId
=
ByteUtils
.
convertStringToBytes
(
tokenizer
.
nextToken
());
}
catch
(
Exception
e
)
{
throw
Message
.
getSQLException
(
Message
.
WRONG_XID_FORMAT_1
,
tid
);
}
}
// private JdbcXid(JdbcDataSourceFactory factory, int id, Xid xid) {
// setTrace(factory.getTrace(), TraceObject.XID, id);
// this.formatId = xid.getFormatId();
// this.branchQualifier = clone(xid.getBranchQualifier());
// this.globalTransactionId = clone(xid.getGlobalTransactionId());
// }
public
String
getAsString
()
{
StringBuffer
buff
=
new
StringBuffer
(
PREFIX
);
buff
.
append
(
'_'
);
buff
.
append
(
formatId
);
buff
.
append
(
'_'
);
buff
.
append
(
ByteUtils
.
convertBytesToString
(
branchQualifier
));
buff
.
append
(
'_'
);
buff
.
append
(
ByteUtils
.
convertBytesToString
(
globalTransactionId
));
return
buff
.
toString
();
}
// private byte[] clone(byte[] data) {
// byte[] d2 = new byte[data.length];
// System.arraycopy(data, 0, d2, 0, data.length);
// return d2;
// }
public
int
getFormatId
()
{
debugCodeCall
(
"getFormatId"
);
return
formatId
;
}
public
byte
[]
getBranchQualifier
()
{
debugCodeCall
(
"getBranchQualifier"
);
return
branchQualifier
;
}
public
byte
[]
getGlobalTransactionId
()
{
debugCodeCall
(
"getGlobalTransactionId"
);
return
globalTransactionId
;
}
}
This diff is collapsed.
Click to expand it.
h2/src/main/org/h2/message/InternalException.java
0 → 100644
浏览文件 @
f509ff65
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
message
;
public
class
InternalException
extends
RuntimeException
{
private
static
final
long
serialVersionUID
=
-
5369631382082604330L
;
private
Exception
cause
;
public
InternalException
(
Exception
e
)
{
cause
=
e
;
}
public
Exception
getOriginalCause
()
{
return
cause
;
}
}
This diff is collapsed.
Click to expand it.
h2/src/main/org/h2/message/Message.java
0 → 100644
浏览文件 @
f509ff65
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
message
;
import
java.io.ByteArrayInputStream
;
import
java.io.IOException
;
import
java.lang.reflect.InvocationTargetException
;
import
java.sql.SQLException
;
import
java.text.MessageFormat
;
import
java.util.Properties
;
import
org.h2.jdbc.JdbcSQLException
;
import
org.h2.util.Resources
;
import
org.h2.util.StringUtils
;
/**
* @author Thomas
*/
public
class
Message
{
private
static
final
Properties
MESSAGES
=
new
Properties
();
static
{
// TODO multilanguage messages
// String language = Locale.getDefault().getLanguage();
try
{
byte
[]
messages
=
Resources
.
get
(
"/org/h2/res/messages.properties"
);
if
(
messages
!=
null
)
{
MESSAGES
.
load
(
new
ByteArrayInputStream
(
messages
));
}
}
catch
(
IOException
e
)
{
TraceSystem
.
traceThrowable
(
e
);
}
}
/**
* Gets the SQL Exception object for a specific SQLState. Supported
* sqlstates are:
*
* @param sqlstate -
* the SQL State
* @param param -
* the parameter of the message
* @return the SQLException object
*/
public
static
JdbcSQLException
getSQLException
(
int
sqlstate
,
String
p1
)
{
return
getSQLException
(
sqlstate
,
new
String
[]
{
p1
},
null
);
}
public
static
String
translate
(
String
key
,
String
[]
param
)
{
String
message
=
MESSAGES
.
getProperty
(
key
);
if
(
message
==
null
)
{
message
=
"(Message "
+
key
+
" not found)"
;
}
if
(
param
!=
null
)
{
Object
[]
o
=
param
;
message
=
MessageFormat
.
format
(
message
,
o
);
}
return
message
;
}
public
static
JdbcSQLException
getSQLException
(
int
errorCode
,
String
[]
param
,
Throwable
cause
)
{
String
sqlstate
=
getState
(
errorCode
);
String
message
=
translate
(
sqlstate
,
param
);
return
new
JdbcSQLException
(
message
,
sqlstate
,
errorCode
,
cause
);
}
public
static
SQLException
getSyntaxError
(
String
sql
,
int
index
)
{
sql
=
StringUtils
.
addAsterix
(
sql
,
index
);
return
Message
.
getSQLException
(
Message
.
SYNTAX_ERROR_1
,
sql
);
}
public
static
SQLException
getSyntaxError
(
String
sql
,
int
index
,
String
expected
)
{
sql
=
StringUtils
.
addAsterix
(
sql
,
index
);
return
Message
.
getSQLException
(
Message
.
SYNTAX_ERROR_2
,
new
String
[]{
sql
,
expected
},
null
);
}
/**
* Gets the SQL Exception object for a specific SQLState.
*
* @param sqlstate -
* the SQL State
* @return the SQLException object
*/
public
static
JdbcSQLException
getSQLException
(
int
sqlstate
)
{
return
getSQLException
(
sqlstate
,
null
);
}
public
static
JdbcSQLException
getUnsupportedException
()
{
return
getSQLException
(
Message
.
FEATURE_NOT_SUPPORTED
);
}
public
static
JdbcSQLException
getInvalidValueException
(
String
value
,
String
param
)
{
return
getSQLException
(
Message
.
INVALID_VALUE_2
,
new
String
[]{
value
,
param
},
null
);
}
public
static
Error
getInternalError
(
String
s
)
{
Error
e
=
new
Error
(
s
);
TraceSystem
.
traceThrowable
(
e
);
return
e
;
}
public
static
Error
getInternalError
(
String
s
,
Exception
e
)
{
Error
e2
=
new
Error
(
s
,
e
);
TraceSystem
.
traceThrowable
(
e2
);
return
e2
;
}
private
static
String
getState
(
int
errorCode
)
{
switch
(
errorCode
)
{
// 02: no data
case
NO_DATA_AVAILABLE:
return
"02000"
;
// 07: dynamic SQL error
case
INVALID_PARAMETER_COUNT_1:
return
"07001"
;
// 08: connection exception
case
ERROR_OPENING_DATABASE:
return
"08000"
;
case
WRONG_USER_OR_PASSWORD:
return
"08004"
;
// 21: cardinality violation
case
COLUMN_COUNT_DOES_NOT_MATCH:
return
"21S02"
;
// 22: data exception
case
NUMERIC_VALUE_OUT_OF_RANGE:
return
"22003"
;
case
DIVISION_BY_ZERO_1:
return
"22012"
;
case
LIKE_ESCAPE_ERROR_1:
return
"22025"
;
// 23: integrity constraint violation
case
CHECK_CONSTRAINT_VIOLATED_1:
return
"23000"
;
case
DUPLICATE_KEY_1:
return
"23001"
;
// integrity constraint violation
// 3B: savepoint exception
// 42: syntax error or access rule violation
case
SYNTAX_ERROR_1:
return
"42000"
;
case
SYNTAX_ERROR_2:
return
"42001"
;
case
TABLE_OR_VIEW_ALREADY_EXISTS_1:
return
"42S01"
;
case
TABLE_OR_VIEW_NOT_FOUND_1:
return
"42S02"
;
case
INDEX_ALREADY_EXISTS_1:
return
"42S11"
;
case
INDEX_NOT_FOUND_1:
return
"42S12"
;
case
DUPLICATE_COLUMN_NAME_1:
return
"42S21"
;
case
COLUMN_NOT_FOUND_1:
return
"42S22"
;
case
SETTING_NOT_FOUND_1:
return
"42S32"
;
// 0A: feature not supported
// HZ: remote database access
//
case
GENERAL_ERROR_1:
return
"HY000"
;
case
UNKNOWN_DATA_TYPE_1:
return
"HY004"
;
case
FEATURE_NOT_SUPPORTED:
return
"HYC00"
;
case
LOCK_TIMEOUT_1:
return
"HYT00"
;
}
return
""
+
errorCode
;
}
// 02: no data
public
static
final
int
NO_DATA_AVAILABLE
=
2000
;
// 07: dynamic SQL error
public
static
final
int
INVALID_PARAMETER_COUNT_1
=
7001
;
// 08: connection exception
public
static
final
int
ERROR_OPENING_DATABASE
=
8000
;
public
static
final
int
WRONG_USER_OR_PASSWORD
=
8004
;
// 21: cardinality violation
public
static
final
int
COLUMN_COUNT_DOES_NOT_MATCH
=
21002
;
// 22: data exception
public
static
final
int
NUMERIC_VALUE_OUT_OF_RANGE
=
22003
;
public
static
final
int
DIVISION_BY_ZERO_1
=
22012
;
public
static
final
int
LIKE_ESCAPE_ERROR_1
=
22025
;
// 23: integrity constraint violation
public
static
final
int
CHECK_CONSTRAINT_VIOLATED_1
=
23000
;
public
static
final
int
DUPLICATE_KEY_1
=
23001
;
// integrity constraint violation
// 3B: savepoint exception
// 42: syntax error or access rule violation
public
static
final
int
SYNTAX_ERROR_1
=
42000
;
public
static
final
int
SYNTAX_ERROR_2
=
42001
;
public
static
final
int
TABLE_OR_VIEW_ALREADY_EXISTS_1
=
42101
;
public
static
final
int
TABLE_OR_VIEW_NOT_FOUND_1
=
42102
;
public
static
final
int
INDEX_ALREADY_EXISTS_1
=
42111
;
public
static
final
int
INDEX_NOT_FOUND_1
=
42112
;
public
static
final
int
DUPLICATE_COLUMN_NAME_1
=
42121
;
public
static
final
int
COLUMN_NOT_FOUND_1
=
42122
;
public
static
final
int
SETTING_NOT_FOUND_1
=
42132
;
// 0A: feature not supported
// HZ: remote database access
//
public
static
final
int
GENERAL_ERROR_1
=
50000
;
public
static
final
int
UNKNOWN_DATA_TYPE_1
=
50004
;
public
static
final
int
FEATURE_NOT_SUPPORTED
=
50100
;
public
static
final
int
LOCK_TIMEOUT_1
=
50200
;
public
static
final
int
FUNCTION_MUST_RETURN_RESULT_SET_1
=
90000
;
public
static
final
int
METHOD_NOT_ALLOWED_FOR_QUERY
=
90001
;
public
static
final
int
METHOD_ONLY_ALLOWED_FOR_QUERY
=
90002
;
public
static
final
int
HEX_STRING_ODD_1
=
90003
;
public
static
final
int
HEX_STRING_WRONG_1
=
90004
;
public
static
final
int
VALUE_TOO_LONG_1
=
90005
;
public
static
final
int
NULL_NOT_ALLOWED
=
90006
;
public
static
final
int
OBJECT_CLOSED
=
90007
;
public
static
final
int
INVALID_VALUE_2
=
90008
;
public
static
final
int
DATE_CONSTANT_1
=
90009
;
public
static
final
int
TIME_CONSTANT_1
=
90010
;
public
static
final
int
TIMESTAMP_CONSTANT_1
=
90011
;
public
static
final
int
PARAMETER_NOT_SET_1
=
90012
;
public
static
final
int
DATABASE_NOT_FOUND_1
=
90013
;
public
static
final
int
PARSE_ERROR_1
=
90014
;
public
static
final
int
SUM_OR_AVG_ON_WRONG_DATATYPE_1
=
90015
;
public
static
final
int
MUST_GROUP_BY_COLUMN_1
=
90016
;
public
static
final
int
SECOND_PRIMARY_KEY
=
90017
;
public
static
final
int
TRACE_CONNECTION_NOT_CLOSED
=
90018
;
public
static
final
int
CANT_DROP_CURRENT_USER
=
90019
;
public
static
final
int
DATABASE_ALREADY_OPEN_1
=
90020
;
public
static
final
int
DATA_CONVERSION_ERROR_1
=
90021
;
public
static
final
int
FUNCTION_NOT_FOUND_1
=
90022
;
public
static
final
int
COLUMN_MUST_NOT_BE_NULLABLE_1
=
90023
;
public
static
final
int
FILE_RENAME_FAILED_2
=
90024
;
public
static
final
int
FILE_DELETE_FAILED_1
=
90025
;
public
static
final
int
SERIALIZATION_FAILED
=
90026
;
public
static
final
int
DESERIALIZATION_FAILED
=
90027
;
public
static
final
int
IO_EXCEPTION_1
=
90028
;
public
static
final
int
NOT_ON_UPDATABLE_ROW
=
90029
;
public
static
final
int
FILE_CORRUPTED_1
=
90030
;
public
static
final
int
CONNECTION_NOT_CLOSED
=
90031
;
public
static
final
int
USER_NOT_FOUND_1
=
90032
;
public
static
final
int
USER_ALREADY_EXISTS_1
=
90033
;
public
static
final
int
LOG_FILE_ERROR_1
=
90034
;
public
static
final
int
SEQUENCE_ALREADY_EXISTS_1
=
90035
;
public
static
final
int
SEQUENCE_NOT_FOUND_1
=
90036
;
public
static
final
int
VIEW_NOT_FOUND_1
=
90037
;
public
static
final
int
VIEW_ALREADY_EXISTS_1
=
90038
;
public
static
final
int
VALUE_TOO_LARGE_FOR_PRECISION_1
=
90039
;
public
static
final
int
ADMIN_RIGHTS_REQUIRED
=
90040
;
public
static
final
int
TRIGGER_ALREADY_EXISTS_1
=
90041
;
public
static
final
int
TRIGGER_NOT_FOUND_1
=
90042
;
public
static
final
int
ERROR_CREATING_TRIGGER_OBJECT_2
=
90043
;
public
static
final
int
ERROR_EXECUTING_TRIGGER_2
=
90044
;
public
static
final
int
CONSTRAINT_ALREADY_EXISTS_1
=
90045
;
public
static
final
int
URL_FORMAT_ERROR_2
=
90046
;
public
static
final
int
DRIVER_VERSION_ERROR_2
=
90047
;
public
static
final
int
FILE_VERSION_ERROR_1
=
90048
;
public
static
final
int
FILE_ENCRYPTION_ERROR_1
=
90049
;
public
static
final
int
WRONG_PASSWORD_FORMAT
=
90050
;
public
static
final
int
STATEMENT_WAS_CANCELLED
=
90051
;
public
static
final
int
SUBQUERY_IS_NOT_SINGLE_COLUMN
=
90052
;
public
static
final
int
SCALAR_SUBQUERY_CONTAINS_MORE_THAN_ONE_ROW
=
90053
;
public
static
final
int
INVALID_USE_OF_AGGREGATE_FUNCTION_1
=
90054
;
public
static
final
int
UNSUPPORTED_CIPHER
=
90055
;
public
static
final
int
NO_DEFAULT_SET_1
=
90056
;
public
static
final
int
CONSTRAINT_NOT_FOUND_1
=
90057
;
public
static
final
int
DUPLICATE_TABLE_ALIAS
=
90058
;
public
static
final
int
AMBIGUOUS_COLUMN_NAME_1
=
90059
;
public
static
final
int
UNSUPPORTED_LOCK_METHOD_1
=
90060
;
public
static
final
int
EXCEPTION_OPENING_PORT_1
=
90061
;
public
static
final
int
FILE_CREATION_FAILED_1
=
90062
;
public
static
final
int
SAVEPOINT_IS_INVALID_1
=
90063
;
public
static
final
int
SAVEPOINT_IS_UNNAMED
=
90064
;
public
static
final
int
SAVEPOINT_IS_NAMED
=
90065
;
public
static
final
int
DUPLICATE_PROPERTY_1
=
90066
;
public
static
final
int
CONNECTION_BROKEN
=
90067
;
public
static
final
int
ORDER_BY_NOT_IN_RESULT
=
90068
;
public
static
final
int
ROLE_ALREADY_EXISTS_1
=
90069
;
public
static
final
int
ROLE_NOT_FOUND_1
=
90070
;
public
static
final
int
USER_OR_ROLE_NOT_FOUND_1
=
90071
;
public
static
final
int
ROLES_AND_RIGHT_CANNOT_BE_MIXED
=
90072
;
public
static
final
int
RIGHT_NOT_FOUND
=
90073
;
public
static
final
int
ROLE_ALREADY_GRANTED_1
=
90074
;
public
static
final
int
COLUMN_IS_PART_OF_INDEX_1
=
90075
;
public
static
final
int
FUNCTION_ALIAS_ALREADY_EXISTS_1
=
90076
;
public
static
final
int
FUNCTION_ALIAS_NOT_FOUND_1
=
90077
;
public
static
final
int
SCHEMA_ALREADY_EXISTS_1
=
90078
;
public
static
final
int
SCHEMA_NOT_FOUND_1
=
90079
;
public
static
final
int
SCHEMA_NAME_MUST_MATCH
=
90080
;
public
static
final
int
COLUMN_CONTAINS_NULL_VALUES_1
=
90081
;
public
static
final
int
SEQUENCE_BELONGS_TO_A_TABLE_1
=
90082
;
public
static
final
int
COLUMN_MAY_BE_REFERENCED_1
=
90083
;
public
static
final
int
CANT_DROP_LAST_COLUMN
=
90084
;
public
static
final
int
INDEX_BELONGS_TO_CONSTRAINT_1
=
90085
;
public
static
final
int
CLASS_NOT_FOUND_1
=
90086
;
public
static
final
int
METHOD_NOT_FOUND_1
=
90087
;
public
static
final
int
UNKNOWN_MODE_1
=
90088
;
public
static
final
int
COLLATION_CHANGE_WITH_DATA_TABLE_1
=
90089
;
public
static
final
int
SCHEMA_CAN_NOT_BE_DROPPED_1
=
90090
;
public
static
final
int
ROLE_CAN_NOT_BE_DROPPED_1
=
90091
;
public
static
final
int
UNSUPPORTED_JAVA_VERSION
=
90092
;
public
static
final
int
CLUSTER_ERROR_DATABASE_RUNS_ALONE
=
90093
;
public
static
final
int
CLUSTER_ERROR_DATABASE_RUNS_CLUSTERED_1
=
90094
;
public
static
final
int
STRING_FORMAT_ERROR_1
=
90095
;
public
static
final
int
NOT_ENOUGH_RIGHTS_FOR_1
=
90096
;
public
static
final
int
DATABASE_IS_READ_ONLY
=
90097
;
public
static
final
int
SIMULATED_POWER_OFF
=
90098
;
public
static
final
int
ERROR_SETTING_DATABASE_EVENT_LISTENER
=
90099
;
public
static
final
int
NO_DISK_SPACE_AVAILABLE
=
90100
;
public
static
final
int
WRONG_XID_FORMAT_1
=
90101
;
public
static
final
int
UNSUPPORTED_COMPRESSION_OPTIONS_1
=
90102
;
public
static
final
int
UNSUPPORTED_COMPRESSION_ALGORITHM_1
=
90103
;
public
static
final
int
COMPRESSION_ERROR
=
90104
;
private
static
final
int
EXCEPTION_IN_FUNCTION
=
90105
;
public
static
final
int
CANT_TRUNCATE_1
=
90106
;
public
static
final
int
CANT_DROP_2
=
90107
;
public
static
final
int
STACK_OVERFLOW
=
90108
;
public
static
final
int
VIEW_IS_INVALID_1
=
90109
;
public
static
final
int
OVERFLOW_FOR_TYPE_1
=
90110
;
public
static
final
int
ERROR_ACCESSING_LINKED_TABLE_1
=
90111
;
public
static
final
int
ROW_NOT_FOUND_WHEN_DELETING_1
=
90112
;
public
static
final
int
UNSUPPORTED_SETTING_1
=
90113
;
public
static
final
int
CONSTANT_ALREADY_EXISTS_1
=
90114
;
public
static
final
int
CONSTANT_NOT_FOUND_1
=
90115
;
public
static
final
int
LITERALS_ARE_NOT_ALLOWED
=
90116
;
public
static
final
int
REMOTE_CONNECTION_NOT_ALLOWED
=
90117
;
public
static
final
int
CANT_DROP_TABLE_1
=
90118
;
public
static
final
int
USER_DATA_TYPE_ALREADY_EXISTS_1
=
90119
;
public
static
final
int
USER_DATA_TYPE_NOT_FOUND_1
=
90120
;
public
static
final
int
DATABASE_CALLED_AT_SHUTDOWN
=
90121
;
public
static
final
int
OPERATION_NOT_SUPPORTED_WITH_VIEWS_2
=
90122
;
public
static
final
int
CANT_MIX_INDEXED_AND_UNINDEXED_PARAMS
=
90123
;
public
static
final
int
FILE_NOT_FOUND_1
=
90124
;
public
static
SQLException
addSQL
(
SQLException
e
,
String
sql
)
{
if
(
e
.
getMessage
().
indexOf
(
"SQL"
)>=
0
)
{
return
e
;
}
if
(
e
instanceof
JdbcSQLException
)
{
JdbcSQLException
j
=
(
JdbcSQLException
)
e
;
return
new
JdbcSQLException
(
j
.
getOriginalMessage
()+
"; SQL statement: "
+
sql
,
j
.
getSQLState
(),
j
.
getErrorCode
(),
j
);
}
else
{
return
new
JdbcSQLException
(
e
.
getMessage
()+
"; SQL statement: "
+
sql
,
e
.
getSQLState
(),
e
.
getErrorCode
(),
e
);
}
}
public
static
SQLException
convert
(
Throwable
e
)
{
if
(
e
instanceof
InternalException
)
{
e
=
((
InternalException
)
e
).
getOriginalCause
();
}
if
(
e
instanceof
SQLException
)
{
return
(
SQLException
)
e
;
}
else
if
(
e
instanceof
InvocationTargetException
)
{
InvocationTargetException
ite
=
(
InvocationTargetException
)
e
;
Throwable
t
=
ite
.
getTargetException
();
if
(
t
instanceof
SQLException
)
{
return
(
SQLException
)
t
;
}
return
Message
.
getSQLException
(
Message
.
EXCEPTION_IN_FUNCTION
,
null
,
e
);
}
else
if
(
e
instanceof
IOException
)
{
return
Message
.
getSQLException
(
Message
.
IO_EXCEPTION_1
,
new
String
[]{
e
.
toString
()},
e
);
}
return
Message
.
getSQLException
(
Message
.
GENERAL_ERROR_1
,
new
String
[]{
e
.
toString
()},
e
);
}
public
static
Error
getInternalError
()
{
return
getInternalError
(
"unexpected code path"
);
}
public
static
InternalException
convertToInternal
(
Exception
e
)
{
return
new
InternalException
(
e
);
}
public
static
IOException
convertToIOException
(
Throwable
e
)
{
if
(
e
instanceof
JdbcSQLException
)
{
JdbcSQLException
e2
=
(
JdbcSQLException
)
e
;
if
(
e2
.
getOriginalCause
()
!=
null
)
{
e
=
e2
.
getOriginalCause
();
}
}
IOException
io
=
new
IOException
(
e
.
toString
());
io
.
fillInStackTrace
();
return
io
;
}
}
This diff is collapsed.
Click to expand it.
h2/src/main/org/h2/message/Trace.java
0 → 100644
浏览文件 @
f509ff65
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
message
;
/**
* @author Thomas
*/
public
class
Trace
{
// currently called trace because log mean something else
// TODO trace: java code generation does not always work
private
TraceSystem
traceSystem
;
private
String
module
;
private
String
lineSeparator
;
public
static
final
String
LOCK
=
"lock"
;
public
static
final
String
SETTING
=
"setting"
;
public
static
final
String
COMMAND
=
"command"
;
public
static
final
String
INDEX
=
"index"
;
public
static
final
String
SEQUENCE
=
"sequence"
;
public
static
final
String
CONSTRAINT
=
"constraint"
;
public
static
final
String
USER
=
"user"
;
public
static
final
String
TRIGGER
=
"trigger"
;
public
static
final
String
FUNCTION
=
"function"
;
public
static
final
String
JDBC
=
"jdbc"
;
public
static
final
String
FILE_LOCK
=
"fileLock"
;
public
static
final
String
TABLE
=
"table"
;
public
static
final
String
LOG
=
"log"
;
public
static
final
String
SCHEMA
=
"schema"
;
public
static
final
String
DATABASE
=
"database"
;
public
static
final
String
SESSION
=
"session"
;
public
Trace
(
TraceSystem
traceSystem
,
String
module
)
{
this
.
traceSystem
=
traceSystem
;
this
.
module
=
module
;
this
.
lineSeparator
=
System
.
getProperty
(
"line.separator"
);
}
public
boolean
info
()
{
return
traceSystem
.
isEnabled
(
TraceSystem
.
INFO
);
}
public
boolean
debug
()
{
return
traceSystem
.
isEnabled
(
TraceSystem
.
DEBUG
);
}
public
void
error
(
String
s
)
{
traceSystem
.
write
(
TraceSystem
.
ERROR
,
module
,
s
,
null
);
}
public
void
error
(
String
s
,
Throwable
t
)
{
traceSystem
.
write
(
TraceSystem
.
ERROR
,
module
,
s
,
t
);
}
public
void
info
(
String
s
)
{
traceSystem
.
write
(
TraceSystem
.
INFO
,
module
,
s
,
null
);
}
public
void
debugCode
(
String
java
)
{
traceSystem
.
write
(
TraceSystem
.
DEBUG
,
module
,
lineSeparator
+
"/**/"
+
java
,
null
);
}
public
void
infoCode
(
String
java
)
{
traceSystem
.
write
(
TraceSystem
.
INFO
,
module
,
lineSeparator
+
"/**/"
+
java
,
null
);
}
public
void
infoSQL
(
String
sql
)
{
sql
=
replaceNewline
(
sql
);
if
(
sql
.
startsWith
(
"/*"
))
{
sql
=
sql
.
substring
(
sql
.
indexOf
(
"*/"
)
+
2
).
trim
();
}
traceSystem
.
write
(
TraceSystem
.
INFO
,
module
,
lineSeparator
+
"/*SQL*/"
+
sql
,
null
);
}
private
String
replaceNewline
(
String
s
)
{
if
(
s
.
indexOf
(
'\r'
)
<
0
&&
s
.
indexOf
(
'\n'
)
<
0
)
{
return
s
;
}
StringBuffer
buff
=
new
StringBuffer
(
s
.
length
());
for
(
int
i
=
0
;
i
<
s
.
length
();
i
++)
{
char
ch
=
s
.
charAt
(
i
);
if
(
ch
==
'\r'
||
ch
==
'\n'
)
{
ch
=
' '
;
}
buff
.
append
(
ch
);
}
return
buff
.
toString
();
}
public
void
debug
(
String
s
)
{
traceSystem
.
write
(
TraceSystem
.
DEBUG
,
module
,
s
,
null
);
}
public
void
debug
(
String
s
,
Throwable
t
)
{
traceSystem
.
write
(
TraceSystem
.
DEBUG
,
module
,
s
,
t
);
}
}
This diff is collapsed.
Click to expand it.
h2/src/main/org/h2/message/TraceObject.java
0 → 100644
浏览文件 @
f509ff65
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
message
;
import
java.io.FileWriter
;
import
java.io.IOException
;
import
java.io.PrintWriter
;
import
java.math.BigDecimal
;
import
java.sql.SQLException
;
import
org.h2.engine.Constants
;
import
org.h2.util.StringUtils
;
/**
*
* @author tgdmuth6
*
*/
public
class
TraceObject
{
public
static
final
int
CALLABLE_STATEMENT
=
0
,
CONNECTION
=
1
,
DATABASE_META_DATA
=
2
,
PREPARED_STATEMENT
=
3
,
RESULT_SET
=
4
,
RESULT_SET_META_DATA
=
5
,
SAVEPOINT
=
6
,
SQL_EXCEPTION
=
7
,
STATEMENT
=
8
,
BLOB
=
9
,
CLOB
=
10
,
PARAMETER_META_DATA
=
11
;
public
static
final
int
DATASOURCE
=
12
,
XA_DATASOURCE
=
13
,
XID
=
14
;
private
static
int
LAST
=
XID
+
1
;
private
Trace
trace
;
private
static
final
int
[]
ID
=
new
int
[
LAST
];
private
static
final
String
[]
PREFIX
=
{
"call"
,
"conn"
,
"dbMeta"
,
"prep"
,
"rs"
,
"rsMeta"
,
"sp"
,
"ex"
,
"stat"
,
"blob"
,
"clob"
,
"pMeta"
,
"ds"
,
"xads"
,
"xid"
};
private
int
type
,
id
;
protected
void
setTrace
(
Trace
trace
,
int
type
,
int
id
)
{
this
.
trace
=
trace
;
this
.
type
=
type
;
this
.
id
=
id
;
}
protected
int
getTraceId
()
{
return
id
;
}
/**
* INTERNAL
*/
public
String
toString
()
{
return
PREFIX
[
type
]
+
id
;
}
protected
int
getNextId
(
int
type
)
{
return
ID
[
type
]++;
}
protected
boolean
debug
()
{
return
trace
.
debug
();
}
protected
Trace
getTrace
()
{
return
trace
;
}
protected
void
debugCodeAssign
(
String
className
,
int
type
,
int
id
)
{
if
(!
trace
.
debug
())
{
return
;
}
trace
.
debugCode
(
className
+
" "
+
toString
()
+
" = "
);
}
protected
void
infoCodeAssign
(
String
className
,
int
type
,
int
id
)
{
if
(!
trace
.
info
())
{
return
;
}
trace
.
infoCode
(
className
+
" "
+
toString
()
+
" = "
);
}
protected
void
debugCodeCall
(
String
text
)
{
if
(!
trace
.
debug
())
{
return
;
}
trace
.
debugCode
(
toString
()
+
"."
+
text
+
"();"
);
}
protected
void
debugCodeCall
(
String
text
,
long
param
)
{
if
(!
trace
.
debug
())
{
return
;
}
trace
.
debugCode
(
toString
()
+
"."
+
text
+
"("
+
param
+
");"
);
}
protected
void
debugCodeCall
(
String
text
,
String
param
)
{
if
(!
trace
.
debug
())
{
return
;
}
trace
.
debugCode
(
toString
()
+
"."
+
text
+
"("
+
quote
(
param
)+
");"
);
}
protected
void
debugCode
(
String
text
)
{
if
(!
trace
.
debug
())
{
return
;
}
trace
.
debugCode
(
toString
()
+
"."
+
text
);
}
protected
String
quote
(
String
s
)
{
return
StringUtils
.
quoteJavaString
(
s
);
}
protected
String
quoteTime
(
java
.
sql
.
Time
x
)
{
if
(
x
==
null
)
{
return
"null"
;
}
return
"Time.valueOf(\""
+
x
.
toString
()
+
"\")"
;
}
protected
String
quoteTimestamp
(
java
.
sql
.
Timestamp
x
)
{
if
(
x
==
null
)
{
return
"null"
;
}
return
"Timestamp.valueOf(\""
+
x
.
toString
()
+
"\")"
;
}
protected
String
quoteDate
(
java
.
sql
.
Date
x
)
{
if
(
x
==
null
)
{
return
"null"
;
}
return
"Date.valueOf(\""
+
x
.
toString
()
+
"\")"
;
}
protected
String
quoteBigDecimal
(
BigDecimal
x
)
{
if
(
x
==
null
)
{
return
"null"
;
}
return
"new BigDecimal(\""
+
x
.
toString
()
+
"\")"
;
}
protected
String
quoteBytes
(
byte
[]
x
)
{
if
(
x
==
null
)
{
return
"null"
;
}
return
"new byte["
+
x
.
length
+
"]"
;
}
protected
String
quoteArray
(
String
[]
s
)
{
return
StringUtils
.
quoteJavaStringArray
(
s
);
}
protected
String
quoteIntArray
(
int
[]
s
)
{
return
StringUtils
.
quoteJavaIntArray
(
s
);
}
protected
SQLException
logAndConvert
(
Throwable
e
)
{
if
(
Constants
.
LOG_ALL_ERRORS
)
{
synchronized
(
this
.
getClass
())
{
// e.printStackTrace();
try
{
FileWriter
writer
=
new
FileWriter
(
"c:\\temp\\h2error.txt"
,
true
);
PrintWriter
p
=
new
PrintWriter
(
writer
);
e
.
printStackTrace
(
p
);
p
.
close
();
writer
.
close
();
}
catch
(
IOException
e2
)
{
e2
.
printStackTrace
();
}
}
}
if
(
trace
==
null
)
{
TraceSystem
.
traceThrowable
(
e
);
}
else
{
if
(
e
instanceof
SQLException
)
{
trace
.
error
(
"SQLException"
,
e
);
return
(
SQLException
)
e
;
}
else
{
trace
.
error
(
"Uncaught Exception"
,
e
);
}
}
return
Message
.
convert
(
e
);
}
}
This diff is collapsed.
Click to expand it.
h2/src/main/org/h2/message/TraceSystem.java
0 → 100644
浏览文件 @
f509ff65
/*
* Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
message
;
import
java.io.FileWriter
;
import
java.io.IOException
;
import
java.io.PrintWriter
;
import
java.sql.DriverManager
;
import
java.sql.SQLException
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
java.util.HashMap
;
import
org.h2.engine.Constants
;
import
org.h2.util.FileUtils
;
/**
* It is possible to write after close was called, but that means for each write the
* log file will be opened and closed again (which is slower).
*
* @author Thomas
*/
public
class
TraceSystem
{
public
static
final
int
OFF
=
0
,
ERROR
=
1
,
INFO
=
2
,
DEBUG
=
3
;
// TODO log total and free memory from time to time
// max file size is currently 64 MB,
// and then there could be a .old file of the same size
private
static
final
int
DEFAULT_MAX_FILE_SIZE
=
64
*
1024
*
1024
;
public
static
final
int
DEFAULT_TRACE_LEVEL_SYSTEM_OUT
=
OFF
;
public
static
final
int
DEFAULT_TRACE_LEVEL_FILE
=
ERROR
;
private
static
final
int
CHECK_FILE_TIME
=
4000
;
private
int
levelSystemOut
=
DEFAULT_TRACE_LEVEL_SYSTEM_OUT
;
private
int
levelFile
=
DEFAULT_TRACE_LEVEL_FILE
;
private
int
maxFileSize
=
DEFAULT_MAX_FILE_SIZE
;
private
String
fileName
;
private
long
lastCheck
;
private
HashMap
traces
;
private
SimpleDateFormat
dateFormat
;
private
FileWriter
fileWriter
;
private
PrintWriter
printWriter
;
private
static
final
int
CHECK_SIZE_EACH_WRITES
=
128
;
private
int
checkSize
;
private
boolean
closed
;
public
static
void
traceThrowable
(
Throwable
e
)
{
PrintWriter
writer
=
DriverManager
.
getLogWriter
();
if
(
writer
!=
null
)
{
e
.
printStackTrace
(
writer
);
}
}
public
TraceSystem
(
String
fileName
)
{
this
.
fileName
=
fileName
;
traces
=
new
HashMap
();
dateFormat
=
new
SimpleDateFormat
(
"MM-dd HH:mm:ss "
);
if
(
fileName
!=
null
)
{
try
{
openWriter
();
}
catch
(
Exception
e
)
{
logWritingError
(
e
);
}
}
}
public
Trace
getTrace
(
String
module
)
{
Trace
t
=
(
Trace
)
traces
.
get
(
module
);
if
(
t
==
null
)
{
t
=
new
Trace
(
this
,
module
);
traces
.
put
(
module
,
t
);
}
return
t
;
}
public
boolean
isEnabled
(
int
level
)
{
int
max
=
Math
.
max
(
levelSystemOut
,
levelFile
);
return
level
<=
max
;
}
public
void
setFileName
(
String
name
)
{
this
.
fileName
=
name
;
}
public
void
setMaxFileSize
(
int
max
)
{
this
.
maxFileSize
=
max
;
}
public
int
getMaxFileSize
()
{
return
maxFileSize
;
}
public
void
setLevelSystemOut
(
int
l
)
{
levelSystemOut
=
l
;
}
public
int
getLevelFile
()
{
return
levelFile
;
}
public
int
getLevelSystemOut
()
{
return
levelSystemOut
;
}
public
void
setLevelFile
(
int
l
)
{
levelFile
=
l
;
}
private
String
format
(
String
module
,
String
s
)
{
return
dateFormat
.
format
(
new
Date
())
+
module
+
": "
+
s
;
}
void
write
(
int
l
,
String
module
,
String
s
,
Throwable
t
)
{
if
(
l
<=
levelSystemOut
)
{
System
.
out
.
println
(
format
(
module
,
s
));
if
(
t
!=
null
&&
levelSystemOut
==
DEBUG
)
{
t
.
printStackTrace
();
}
}
if
(
fileName
!=
null
)
{
if
(
l
>
levelFile
)
{
long
time
=
System
.
currentTimeMillis
();
if
(
time
>
lastCheck
+
CHECK_FILE_TIME
)
{
String
checkFile
=
fileName
+
Constants
.
SUFFIX_TRACE_START_FILE
;
lastCheck
=
time
;
if
(
FileUtils
.
exists
(
checkFile
))
{
levelFile
=
DEBUG
;
try
{
FileUtils
.
delete
(
checkFile
);
}
catch
(
Exception
e
)
{
// the file may be read only
}
}
}
}
if
(
l
<=
levelFile
)
{
writeFile
(
format
(
module
,
s
),
t
);
}
}
}
private
synchronized
void
writeFile
(
String
s
,
Throwable
t
)
{
try
{
if
(
checkSize
++
>=
CHECK_SIZE_EACH_WRITES
)
{
checkSize
=
0
;
closeWriter
();
if
(
maxFileSize
>
0
&&
FileUtils
.
length
(
fileName
)
>
maxFileSize
)
{
String
old
=
fileName
+
".old"
;
if
(
FileUtils
.
exists
(
old
))
{
FileUtils
.
delete
(
old
);
}
FileUtils
.
rename
(
fileName
,
old
);
}
}
if
(!
openWriter
())
{
return
;
}
printWriter
.
println
(
s
);
if
(
t
!=
null
)
{
t
.
printStackTrace
(
printWriter
);
}
printWriter
.
flush
();
if
(
closed
)
{
closeWriter
();
}
}
catch
(
Exception
e
)
{
logWritingError
(
e
);
}
}
private
void
logWritingError
(
Exception
e
)
{
// TODO translate trace messages
SQLException
se
=
Message
.
getSQLException
(
Message
.
LOG_FILE_ERROR_1
,
new
String
[]
{
fileName
},
e
);
// print this error only once
fileName
=
null
;
System
.
out
.
println
(
se
);
se
.
printStackTrace
();
}
private
boolean
openWriter
()
throws
IOException
{
if
(
printWriter
==
null
)
{
try
{
FileUtils
.
createDirs
(
fileName
);
if
(
FileUtils
.
exists
(
fileName
)
&&
FileUtils
.
isReadOnly
(
fileName
))
{
// read only database: don't log error if the trace file can't be opened
return
false
;
}
fileWriter
=
FileUtils
.
openFileWriter
(
fileName
,
true
);
printWriter
=
new
PrintWriter
(
fileWriter
,
true
);
}
catch
(
SQLException
e
)
{
return
false
;
}
}
return
true
;
}
private
synchronized
void
closeWriter
()
{
if
(
printWriter
!=
null
)
{
printWriter
.
flush
();
printWriter
.
close
();
printWriter
=
null
;
}
if
(
fileWriter
!=
null
)
{
try
{
fileWriter
.
close
();
}
catch
(
IOException
e
)
{
// ignore exception
}
fileWriter
=
null
;
}
}
public
void
close
()
{
closeWriter
();
closed
=
true
;
}
public
void
finalize
()
{
if
(!
Constants
.
RUN_FINALIZERS
)
{
return
;
}
close
();
}
}
This diff is collapsed.
Click to expand it.
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论