Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8224461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:04:45+00:00 2026-06-07T15:04:45+00:00

I have the following method for bulk insert of the data in tables. First

  • 0

I have the following method for bulk insert of the data in tables.
First my code populates the data in data tables and inserts this data in corresponding tables using the SqlBulkCopy claas of the .net .

I have requirement that data should get inserted in all tables or neither of them.
For this I have used SqlTransaction class of the .net.

Scenario is, multiple threads execute the following code block at the same time.

 public void Import()
        {
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {

                SqlTransaction sqlTrans =null;
                try
                {
                    sqlConnection.Open();
                    sqlTrans = sqlConnection.BeginTransaction(IsolationLevel.Serializable)


                    SqlCommand cmd = sqlConnection.CreateCommand();
                    cmd.CommandText = "select top 1 null from lockTable with(xlock)";
                    cmd.CommandTimeout = 3600*3;
                    cmd.Transaction = sqlTrans;
                    SqlDataReader reader = cmd.ExecuteReader();


                    foreach (DataTable dt in DataTables)
                    {
                        ImportIntoDatabase(sqlConnection, dt, sqlTrans);
                    }

                    reader.Close();
                    sqlTrans.Commit();                    
                }
                catch (Exception ex)
                {
                    sqlTrans.Rollback();
                    throw ex;
                }
            }
        }

       private void ImportIntoDatabase(SqlConnection sqlConn, DataTable dt, SqlTransaction sqlTrans)
        {
            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConn, SqlBulkCopyOptions.Default, sqlTrans))
            {
                bulkCopy.BulkCopyTimeout = dt.Rows.Count * 10;
                try
                {
                    bulkCopy.DestinationTableName = dt.TableName;                    
                    bulkCopy.WriteToServer(dt);
                }
                catch (Exception ex)
                {
                   throw ex;
                }
            }
        }

To handle this concurrency, I have created one dummy table(table named ‘lockTable’), in the database where the other table resides(the bulk insert tables). I am getting exclusive lock on this dummy table in the SqlTransaction having command time out as high as 3 hours.

Problem:
I am getting following exception

: Cannot access destination table ‘Tbl1’ (tbl1 is the table for bulk inserting)

followed by another exception, while rolling back the transaction in catch block

: Error While executing activity The server failed to resume the transaction. Desc:3a00000001.
The transaction active in this session has been committed or aborted by another session.

Can any one help me for this weird behavior of the code. I have already searched a lot on this issue on the internet, but I have not found anything helpful for me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:04:49+00:00Added an answer on June 7, 2026 at 3:04 pm

    I got my problem solved out.

    Following are the changes which I have made to my Import method

    public void Import()
            {
                using (SqlConnection sqlConnection = new SqlConnection(connectionString))
                {
                    sqlConnection.Open();
                    using (SqlTransaction sqlTrans = sqlConnection.BeginTransaction())
                    {
                        try
                        {                       
                            SqlCommand cmd = sqlConnection.CreateCommand();
                            cmd.CommandText = "select top 1 null from lockTable with(xlock)";
                            cmd.CommandTimeout = LOCK_TIME_OUT;
                            cmd.Transaction = sqlTrans;
                            SqlDataReader reader = cmd.ExecuteReader();
    
    
                            foreach (DataTable dt in DataTables)
                            {
                                ImportIntoDatabase(sqlConnection, dt, sqlTrans);                            
                            }
    
                            reader.Close();
                            sqlTrans.Commit();                        
                        }
                        catch (Exception ex)
                        {
                            sqlTrans.Rollback();
                            throw ex;
                        }
                    }
                    sqlConnection.Close();
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following method which I am using to load ActiveX control dynamically, Dim
I have an XML structure like the following: <tables> <table name=tableName1> <row ID=34 col1=data
I have following extension method written: static public IQueryable<OutboundPattern> ByClientID(this IQueryable<OutboundPattern> qry, int clientID)
I have following method in my Borland C++ code, static bool UploadBitstream(void) { //Some
I have following method in the c code. void add(int number) { Node node1;
I have the following method, using Hibernate, that's driving me nuts. It's not updating
I have the following method in some nemerle code: private static getLinks(text : string)
I have the following method signature: public void MyFunction(Object[,] obj) I create this object:
Inside my Table view controller I have following method -(BOOL)isValidCoordinate:(CLLocationCoordinate2D)coordinate { //This is just
I am using java with spring3 i have following controller's service method @Override public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.