返回首页

gbase数据、南大通用产品文档:GBase8a

更新日期:2024年09月11日

GBase 自动关闭连接问题
我有一个小服务程序/应用程序正常地工作了一白天,
然后晚上就停止工作
了:
回答:
GBase 在无活动8 小时后就自动关闭连接。你或许需要使用能处理失效连
接的连接池。
此外,用户应该在应用程序中捕获SQL 异常并处理它们,而不是一直传播
它们直到用户的程序退出,这只是一个好的编程习惯。当在处理查询过程中遇
到网络连通性问题时,GBase JDBC 会把SQLState (参见用户的APIDOCS 中的

GBase 8a 程序员手册JDBC 篇
南大通用数据技术股份有限公司

- 183 -
java.sql.SQLException.getSQLState()) 置为 “08S01” 。这时用户的程序
应该重新连接GBase 。
下面(简单的)例子说明了什么样的代码可以处理这理这些异常: 使用重
试逻辑的事务例子
public void doBusinessOp() throws SQLException
{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
//
// How many times do you want to retry the transaction
// (or at least _getting_ a connection)?
//
int retryCount = 5;
boolean transactionCompleted = false;
do {
try {
conn = getConnection(); // assume getting this from a
// javax.sql.DataSource, or the
// java.sql.DriverManager
conn.setAutoCommit(false);
//
// Okay, at this point, the 'retry-ability' of the
//
transaction
really
depends
on
your
application
logic,
// whether or not you're using autocommit (in this case
//
not),
and
whether
you're
using
transacational
storage
// engines
//
// For this example, we'll assume that it's _not_ safe
//
to
retry
the
entire
transaction,
so
we
set
retry
count
// to 0 at this point
//
// If you were using exclusively transaction-safe
tables,
// or your application could recover from a connection

GBase 8a 程序员手册JDBC 篇


- 184 -

南大通用数据技术股份有限公司
going
//
bad
in
the
middle
of
an
operation,
then
you
would
not
//
touch
'retryCount'
here,
and
just
let
the
loop
repeat
// until retryCount == 0.
//
retryCount = 0;
stmt = conn.createStatement();
String query = "SELECT foo FROM bar ORDER BY baz";
rs = stmt.executeQuery(query);
while (rs.next()) {}
rs.close();
rs = null;
stmt.close();
stmt = null;
conn.commit();
conn.close();
conn = null;
transactionCompleted = true;
} catch (SQLException sqlEx) {
//
// The two SQL states that are 'retry-able' are 08S01
// for a communications error, and 41000 for deadlock.
//
//
Only
retry
if
the
error
was
due
to
a
stale
connection,
// communications problem or deadlock
//
String sqlState = sqlEx.getSQLState();
if ("08S01".equals(sqlState) ||
"41000".equals(sqlState)) {
retryCount--;
}else {
retryCount = 0;
}
} finally {
if (rs != null) {
try {
rs.close();

GBase 8a 程序员手册JDBC 篇
南大通用数据技术股份有限公司

- 185 -
} catch (SQLException sqlEx) {
// You'd probably want to log this . . .
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException sqlEx) {
//
You'd
probably
want
to
log
this
as
well
.
.
.
}
}
if (conn != null) {
try {
//
// If we got here, and conn is not null, the
// transaction should be rolled back, as not
// all work has been done
try {
conn.rollback();
} finally {
conn.close();
}
} catch (SQLException sqlEx) {
//
// If we got an exception here, something
// pretty serious is going on, so we better
// pass it up the stack, rather than just
// logging it. . .
throw sqlEx;
}
}
}
} while (!transactionCompleted && (retryCount > 0));
}

GBase 8a 程序员手册JDBC 篇


- 186 -

南大通用数据技术股份有限公司

并行技术
..........................................
10

case
10 gloves/case
2
HRO
baseball