返回首页

gbase数据、南大通用产品文档:GBase8aBeginTransaction 方法 ()

更新日期:2024年09月11日

开始一个数据库事务。

语法
[Visual Basic]
Public Function BeginTransaction As GBaseTransaction
[C#]
public GBaseTransaction BeginTransaction()


返回值
代表新事务的对象。

注释
这个命令等价于在GBase 数据库中执行BEGIN TRANSACTION 命令,用户必
须使用 Commit 方法提交事务,使用Rollback 方法回滚事务。
如果用户没有指定一个隔离级别,会使用默认的隔离级别。要使用



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

- 171 -
BeginTransaction 指定一个隔离级别,
可以使用带有IsolationLevel 的重载函
数。

异常
InvalidOperationException 异常
异常类型
条 件
InvalidOperationException
不支持并发事务。

示例
下面的例子中演示了如何使用BeginTransaction 的Commit 和 Rollback
方法。
[Visual Basic]
Public Sub RunTransaction(gsConnString As String)
Dim gsConnection As New GBaseConnection(gsConnString)
gsConnection.Open()
Dim gsCommand As GBaseCommand = gsConnection.CreateCommand()
Dim gsTrans As GBaseTransaction
' Start a local transaction
gsTrans = gsConnection.BeginTransaction()
' Must assign both transaction object and connection
' to Command object for a pending local transaction
gsCommand.Connection = gsConnection
gsCommand.Transaction = gsTrans
Try
gsCommand.CommandText
=
"Insert
into
Test
(id,
desc)
VALUES"
_ &" (100, 'Description')"
gsCommand.ExecuteNonQuery()
gsCommand.CommandText
=
"Insert
into
Test
(id,
desc)
VALUES"
_ &" (101, 'Description')"
gsCommand.ExecuteNonQuery()
gsTrans.Commit()
Console.WriteLine("Both records are written to
database.")
Catch e As Exception
Try

GBase 8a 程序员手册ADO.NET 篇


- 172 -

南大通用数据技术股份有限公司
gsTrans.Rollback()
Catch ex As GBaseException
If Not gsTrans.Connection Is Nothing Then
Console.WriteLine("An exception of type " + _
ex.GetType().ToString()
+
"
was
encountered
while
attempting
to
roll
back the transaction.")
End If
End Try
Console.WriteLine("An exception of type " +
e.GetType().ToString() + "was encountered while inserting the data.")
Console.WriteLine("Neither record was written to
database.")
Finally
gsConnection.Close()
End Try
End Sub
[C#]
public void RunTransaction(string gsConnString)
{
GBaseConnection gsConnection = new
GBaseConnection(gsConnString);
gsConnection.Open();
GBaseCommand gsCommand = gsConnection.CreateCommand();
GBaseTransaction gsTrans;
// Start a local transaction
gsTrans = gsConnection.BeginTransaction();
// Must assign both transaction object and connection
// to Command object for a pending local transaction
gsCommand.Connection = gsConnection;
gsCommand.Transaction = gsTrans;
try
{
gsCommand.CommandText
=
"insert
into
Test
(id,
desc)
VALUES
(100, 'Description')";
gsCommand.ExecuteNonQuery();
gsCommand.CommandText
=
"insert
into
Test
(id,
desc)
VALUES



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

- 173 -
(101, 'Description')";
gsCommand.ExecuteNonQuery();
gsTrans.Commit();
Console.WriteLine("Both
records
are
written
to
database.");
}
catch(Exception e)
{
try
{
gsTrans.Rollback();
}
catch (SqlException ex)
{
if (gsTrans.Connection != null)
{
Console.WriteLine("An exception of type " +
ex.GetType() +" was encountered while attempting to roll back the
transaction.");
}
}
Console.WriteLine("An exception of type " + e.GetType() +
" was encountered while inserting the data.");
Console.WriteLine("Neither record was written to
database.");
}
finally
{
gsConnection.Close();
}
}

功能

GBase 8a MPP Cluster 产品手册
6 附录
文档版本953(2022-04-10)
南大通用数据技术股份有限公司
1577
这个参数用于控制在GBase 8a MPP Cluster 宕机时,是否生成core-dump 文件。
core-file 是一个无值参数。
当在参数文件中出现这个参数时,GBase 8a MPP Cluster 服务在宕机后,会生成
core-dump 文件。
GBase 8a MPP Cluster 默认不启用该参数。

异大
问题现象
两表like 条件关联使用limit 和limit offset 性能差异大,如下示例,该SQL 执行
时跑了几万秒没有结果,但把最后的limit 0,500 换成limit 500,20 秒左右就可

GBase 8a MPP Cluster 最佳实践
5 FAQ
文档版本(2022-02-11)
南大通用数据技术股份有限公司
94
以执行完毕。
SELECT a.*
,b.addr_full_name
,b.gridcode
FROM gd_eda.temp_wyj_obd_01 a
,gd_lx.address_grid b
WHERE b.addr_full_name LIKE a.address || '%'
AND a.address IS NOT NULL
AND b.gridcode IS NOT NULL limit 0,500
解决方法

Limit 0,500 时,在默认set _t_gcluster_limit_optimize=1;的情况下,执行器会
先去各个节点评估本节点结果集行数,该语句2 表关联条件为b.addr_full_na
me like a.address||'%',左表(小表,数据量为140 多万)拉复制表后,与右
表的分片按nest-loop 方式join,2 表数据量都很大,join 执行的时间长,导致
长时间评估不完;

limit 500 时,不按照上述优化执行,直接下发到第一个节点执行,虽然也是
nest-loop join,但是当结果集行数到500 时就退出了,对外表现性能更好;

性能差异的根本原因是在sql 的join 上面,建议请用户核查下sql 的逻辑是否
有问题;或session 级设置上述参数值为0。
改造业务逻辑或者执行该sql 时设置set _t_gcluster_limit_optimize=0。