SqlBulkCopy  做为SQL Server 官方 批量入库类,性能不会太差。其事务部份官方说明较模糊,因此 针对事务配置做了一些测试。

  A. 先准备测试场景 ,关于SqlBulkCopyOptions.KeepIdentity 应用。  新建两张表 , 每张表列a 做为自增列,同时做为主键 , 其中 test_sqlbulk 源表 ,先添加一组数据。

CREATE TABLE [dbo].[test_sqlbulk](
[a] [int] IDENTITY(1,1) NOT NULL,
[b] [int] NULL,
CONSTRAINT [PK_test_sqlbulk] PRIMARY KEY CLUSTERED
(
[a] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] CREATE TABLE [dbo].[test_sqlbulk_des](
[a] [int] IDENTITY(1,1) NOT NULL,
[b] [int] NULL,
CONSTRAINT [PK_test_sqlbulk_des] PRIMARY KEY CLUSTERED
(
[a] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

添加数据:

测试代码:

 SqlConnection sqlConn = new SqlConnection( sourceConnection) ;
sqlConn.Open() ;
SqlCommand commandSourceData = new SqlCommand("select * from test_sqlbulk ", sqlConn);
SqlDataReader reader = commandSourceData.ExecuteReader(); // Set up the bulk copy object using the KeepIdentity option.
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(
sourceConnection))
{
bulkCopy.BatchSize = ;
bulkCopy.DestinationTableName =
"dbo.test_sqlbulk_des"; // Write from the source to the destination.
// This should fail with a duplicate key error
// after some of the batches have been copied.
try
{
bulkCopy.WriteToServer(reader);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
reader.Close();
}
}

再看看目标表 test_sqlbulk_des 结果 , 由于SqlBulkCopy  构造参数没有 KeepIdentity 标识,目标表自增列与源表不一致 :

test_sqlbulk_des 查询结果:

=>调整代码,增加 SqlBulkCopyOptions.KeepIdentity 选项,再次导入:

 using (SqlBulkCopy bulkCopy = new SqlBulkCopy(
sourceConnection, SqlBulkCopyOptions.KeepIdentity))
{
bulkCopy.BatchSize = 2;
bulkCopy.DestinationTableName =
"dbo.test_sqlbulk_des"; // Write from the source to the destination.
// This should fail with a duplicate key error
// after some of the batches have been copied.
try
{
bulkCopy.WriteToServer(reader);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
reader.Close();
}
}

=>目标表,源表自增列一致了:

B. 测试无事务,内部事务(SqlBulkCopyOptions.UseInternalTransaction) , 外部传入事务 之间区别 , 以下所有测试均事先在目标表保留一行数据 使copy 过程中造成主键冲突,查看回退情况。

1. 无事务测试。初使化源表,目标表数据:

测试代码 , 不意外,如下代码最终会产生主键冲突错误, 执行结束后,再查询源表,目标表数据 。 BatchSize 设置为2 , 结果表明在第二批次主键冲突, 第一批次数据成功提交了(没有显示声明事务), 再将 BatchSize 设置为3 验证结果。

 SqlConnection sqlConn = new SqlConnection( sourceConnection) ;
sqlConn.Open() ;
SqlCommand commandSourceData = new SqlCommand("select * from test_sqlbulk ", sqlConn);
SqlDataReader reader = commandSourceData.ExecuteReader(); // Set up the bulk copy object using the KeepIdentity option.
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(
sourceConnection, SqlBulkCopyOptions.KeepIdentity))
{
bulkCopy.BatchSize = 2;
bulkCopy.DestinationTableName =
"dbo.test_sqlbulk_des"; // Write from the source to the destination.
// This should fail with a duplicate key error
// after some of the batches have been copied.
try
{
bulkCopy.WriteToServer(reader);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
reader.Close();
}
}

BatchSize = 2 结果:

BatchSize = 3 结果:

2.指定内部事务 , 分别将 BatchSize 指定为 2 , 3, 4  。如下执行结果与上文示例 结果一致, 因此无论是否指定 SqlBulkCopyOptions.UseInternalTransaction 参数(不包括外部传入事务) ,SqlBulkCopy 内部按照一个批次一个事务。在出现异常时只回滚当前批次 , 在此之前成功执行批次不回滚。 因此若需全部回退,需将  BatchSize 设为 总记录条数 ,此时所有数据做为一个批次提交 ,  异常回退批次,回退数据范围即全部数据。

测试代码:

 // Set up the bulk copy object using the KeepIdentity option.
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(
sourceConnection, SqlBulkCopyOptions.KeepIdentity | SqlBulkCopyOptions.UseInternalTransaction))
{
bulkCopy.BatchSize = 2;
bulkCopy.DestinationTableName =
"dbo.test_sqlbulk_des"; // Write from the source to the destination.
// This should fail with a duplicate key error
// after some of the batches have been copied.
try
{
bulkCopy.WriteToServer(reader);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
reader.Close();
}
}

=>BatchSize = 2  , 目标表结果:

=>BatchSize = 3  , 目标表结果:

3. 外部传入事务对象场景 , 如果 使用SqlBulkCopy入库操作只做为本次业务处理一个子集,在整个业务处理环节中异常情况,需要回滚整个业务操作。 本次再新建一张表:test_sqlbulk_update , 测试过程中,先对该表增加一条记录,再重复之前异常场景。 结束时,之前新增记录也回退了。

因此, 当外部传入事务对象时,SqlBulkCopy  内部不提交事务, 在异常情况,有可能会自动回滚。

CREATE TABLE [dbo].[test_sqlbulk_update](
[a] [int] IDENTITY(1,1) NOT NULL,
[b] [int] NULL,
)
SqlConnection sqlConn = new SqlConnection( sourceConnection) ;
sqlConn.Open() ; SqlTransaction tran = sqlConn.BeginTransaction(); SqlCommand commandInsert = new SqlCommand("insert into [test_sqlbulk_update]( b ) values (1) ", sqlConn, tran); int result = commandInsert.ExecuteNonQuery(); SqlCommand commandSourceData = new SqlCommand("select * from test_sqlbulk ", sqlConn, tran); SqlDataReader reader = commandSourceData.ExecuteReader(); // Set up the bulk copy object using the KeepIdentity option.
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConn, SqlBulkCopyOptions.KeepIdentity, tran))
{
bulkCopy.BatchSize = 2;
bulkCopy.DestinationTableName =
"dbo.test_sqlbulk_des"; // Write from the source to the destination.
// This should fail with a duplicate key error
// after some of the batches have been copied.
try
{
bulkCopy.WriteToServer(reader);
reader.Close();
tran.Commit();
}
catch (Exception ex)
{
reader.Close();
tran.Rollback();
            // tran.Commit(); 异常仍提交执行,同时注释上一行。
} }

=> 异常时执行 rollback, 最终结果:没做任何修改.

=> 外部事务,Catch 中执行 Commint ,发生异常,因为自动回滚了,但在Catch 执行 Rollback 无异常。

SqlTransaction tran = sqlConn.BeginTransaction();

            SqlCommand commandSourceData = new SqlCommand("select * from test_sqlbulk ", new SqlConnection(sourceConnection));

            SqlDataAdapter ada = new SqlDataAdapter(commandSourceData ) ;
System.Data.DataSet ds = new System.Data.DataSet();
ada.Fill(ds); // Set up the bulk copy object using the KeepIdentity option.
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConn, SqlBulkCopyOptions.KeepIdentity, tran))
{
bulkCopy.BatchSize = ;
bulkCopy.DestinationTableName =
"dbo.test_sqlbulk_des"; // Write from the source to the destination.
// This should fail with a duplicate key error
// after some of the batches have been copied.
try
{ bulkCopy.WriteToServer(ds.Tables[]); SqlCommand commandInsert = new SqlCommand("insert into [test_sqlbulk_update]( b ) values ('a') ", sqlConn, tran); int result = commandInsert.ExecuteNonQuery(); tran.Commit();
}
catch (Exception ex)
{
tran.Commit();
} }

SqlBulkCopy 参数配置示例的更多相关文章

  1. Hystrix参数配置

    1.Hystrix参数配置文档  2.Hystrix参数配置示例 import org.springframework.beans.factory.annotation.Autowired; impo ...

  2. 教你如何利用分布式的思想处理集群的参数配置信息——spring的configurer妙用

    引言 最近LZ的技术博文数量直线下降,实在是非常抱歉,之前LZ曾信誓旦旦的说一定要把<深入理解计算机系统>写完,现在看来,LZ似乎是在打自己脸了.尽管LZ内心一直没放弃,但从现状来看,需要 ...

  3. HBase + Kerberos 配置示例(二)

    接上篇<HBase + Kerberos配置示例(一)>,我们继续剩下的配置工作. 环境准备 Hadoop配置 Zookeeper配置 HBase配置 Java测试程序 环境准备 安装ha ...

  4. Linux环境下vsftpd参数配置

    很久之前就用过vsftpd,但总是忘了参数该如何配置,今天特地有搜索了一遍,把配置方法整理如下: (1)检查是否已安装vsftpd #rpm -qa | grep vsftpd vsftpd--.el ...

  5. Haproxy的安装和配置示例

    1.ha proxy简介ha proxy是一个开源的,高性能的,基于tcp第四层和http第七层应用的负载均衡软件优点:可靠性和稳定性非常好          最高可以同时维护40000-50000个 ...

  6. java日志规约及配置示例终极总结

    目录 什么是日志 常用日志框架 日志级别详解 日志的记录时机 日志使用规约 logback 配置示例 loh4j2 配置示例 什么是日志? 简单的说,日志就是记录程序的运行轨迹,方便查找关键信息,也方 ...

  7. spring boot 日志介绍 以及 logback配置示例

    https://www.cnblogs.com/flying607/p/7827460.html 以下是springboot的一个局部依赖关系: 可以看到,java util logging(jul) ...

  8. Mysql半同步复制模式说明及配置示例 - 运维小结

    MySQL主从复制包括异步模式.半同步模式.GTID模式以及多源复制模式,默认是异步模式 (如之前详细介绍的mysql主从复制).所谓异步模式指的是MySQL 主服务器上I/O thread 线程将二 ...

  9. HAPRoxy(一):HAProxy基本配置、调度算法与tcp、http、heath模式配置示例

    一.HAProxy安装 1.HAProxy简单介绍 HAProxy虽然名字前有HA,但它并不是一款高可用软件,而是一款用于实现负载均衡的软件,可实现四层与七层的负载均衡. 2.yum安装HAProxy ...

随机推荐

  1. 【问题解决】Eclipse中 ctrl+空格 content assist

    改一下你的快捷键设置:window->perferences-->keys--->查找 content assist--->把这个地方改成你想要的就可以了.!

  2. es6重点笔记:数值,函数和数组

    本篇全是重点,捡常用的怼,数值的扩展比较少,所以和函数放一起: 一,数值 1,Number.EPSILON:用来检测浮点数的计算,如果误差小于这个,就无误 2,Math.trunc():取整,去除小数 ...

  3. idea 远程调试 tomcat web应用

    最近在做的一个东西,测试环境和本地环境差距太大,本地能运行的代码,放到测试环境上到处报错,哪里哪里都连不上,所以决定把代码部署到远程服务器上调试,节省时间. 网上看了很多教程,大部分都是互相抄来抄去, ...

  4. common-logging源码解析

    OK,现在我们来研究下common-logging的源码.这篇博客有参照上善若水的博客,感谢他的无私分享. 先来随便扯点吧,貌似所有这些流行的Logging框架都和Log4J多少有点关系(不太确定Co ...

  5. base64_encode与base64_decode

    base64_encode 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! base64_encode() returns 使用 base64 对 data 进行编码. ...

  6. tcpdump 使用

    例子: 首先切换到root用户 tcpdump -w  aaa.cap   -i eth7   -nn -x  'port  9999'  -c  1 以例子说明参数: -w:输出到文件aaa.cap ...

  7. Struts2 (二)

    1 自定义结果视图 1.1 自定义一个类实现com.opensymphony.xwork2.Result接口. package com.xuweiwei.action; import com.open ...

  8. 验证码 jsp

    验证码的jsp实现 <%@ page contentType="image/jpeg" import="java.awt.*, java.awt.image.*,j ...

  9. php的print_r第二个参数是true有啥用啊

    print_r(); 第二个参数 为true时 返回值 而不显示. echo ,print,print_r的区别 echo ,print的区别在于echo 可以输出多个变量值,而print只有一个变量 ...

  10. mysql 我们眼中的int(10)

    自我总结,欢迎拍砖! 目的:定义int(3)和int(10)真的有区别吗? 论证: 1.创建student,student2表 分别定义一个student,student2表 create table ...