insert lock table.

insert lock table.

Post by Andrew J. Kell » Fri, 11 May 2001 03:10:41



I could be wrong since I didn't read word for word your entire post. But if
you are saying that SQL Server is issuing a table lock while inserting those
rows then that's perfectly normal. SQL Server will (when it thinks best)
upgrade the locks from a ROW level, to Page level or even Table level. If
you are inserting enough rows into a table it is more efficient to lock the
whole table than each individual row. With no indexes it may even go right
to a table level lock. By the way, there is no need to wrap that insert in a
transaction as it is ATOMIC as is.

--
Andrew J. Kelly
Targitmail.com


> Hi

> We have interesting problem which can be easely reproduced on our
> configuration NT4 + SP6A/ SQL7+SP3 / MDAC2.6
> Database locks up the table after inserting aproximetly 3000 rows. It
looks
> like a bug in MSSql server.

> This happends if we try to call stored procedure which execute just one
SQL
> statement - inser into a table.
> This table does not have any indexes and consist of the only column:

> MFC programm which calls this stored procedure from threads you can find
in
> attachments
> (you should change connection string - DBTestDlg.cpp:line 18).
> --------------------------------------------------------------------------
--
> --
> create  table tst
> (
>  c2 varchar(2048)  NOT NULL
> )
> --------------------------------------------------------------------------
--
> ----
> Before start the test you should trancate this table and shrink your
> database:
> truncate table tst
> DBCC SHRINKDATABASE ([DBNAME])

> stored proceduri is:
> --------------------------------------------------------------------------
--
> ----
> create procedure tst_sp
> as
>   SET DEADLOCK_PRIORITY LOW
>   SET LOCK_TIMEOUT 2000

>   begin
>     BEGIN TRANSACTION
>   end




>     ROLLBACK TRANSACTION
>   ELSE
>     COMMIT

>   SET NOCOUNT OFF
> --------------------------------------------------------------

> Thread function jast do a loop:
>   while(!m_bStop)
>   {
>    try
>    {
>     if(!(_con.BeginTrans()))
>     {
>      pDlg->addMessage("Can not begin transaction");
>     }

>     _con.ExecuteSQL("{call tst_sp}");

>     if(!(_con.CommitTrans()))
>     {
>      pDlg->addMessage("Can not commit transaction");
>     }

>    }
>    catch(CDBException& e)
>    {
>     //DebugBreak();

>     pDlg->addMessage(e.m_strError);
>     if(!(_con.Rollback()))
>     {
>      pDlg->addMessage("Can not rollback transaction");
>     }

>     _con.Close();
>     _con.OpenEx(CONNECTION_STR,8);
>    }
>    catch(CDBException* e)
>    {
>     //DebugBreak();

>     pDlg->addMessage(e->m_strError);
>     e->Delete();
>     if(!(_con.Rollback()))
>     {
>      pDlg->addMessage("Can not rollback transaction");
>     }

>     _con.Close();
>     _con.OpenEx(CONNECTION_STR,8);
>    }
>    catch(EXCEPTION_POINTERS& pep)
>    {
>     //DebugBreak();
>     char buffer[100];
>     _ltoa(pep.ExceptionRecord->ExceptionCode, buffer, 16);
>     pDlg->addMessage(CString("SEH exception: ")+=buffer);
>     if(!(_con.Rollback()))
>     {
>      pDlg->addMessage("Can not rollback transaction");
>     }
>     _con.Close();
>     _con.OpenEx(CONNECTION_STR,8);
>    }
>    catch(...)
>    {
>     //DebugBreak();
>     pDlg->addMessage(CString("Unknown exception"));
>     if(!(_con.Rollback()))
>     {
>      pDlg->addMessage("Can not rollback transaction");
>     }
>     _con.Close();
>     _con.OpenEx(CONNECTION_STR,8);
>    }
>    Sleep(10);
>   }
> //-----------------------------------------------------------------------

 
 
 

insert lock table.

Post by Vladimir Pette » Fri, 11 May 2001 04:59:50


Hi Andrew,

The problem is that it loks the table and does not unlock it after
insertion. The problem happends as I wrote after insertion aproximatly 3000
rows and can be reprodused on shrinked database. All threads after that just
can not insert anything into this table. They get message "Time out". It is
mean that table somehow have been locked.

Thanks,
Vladimir



> I could be wrong since I didn't read word for word your entire post. But
if
> you are saying that SQL Server is issuing a table lock while inserting
those
> rows then that's perfectly normal. SQL Server will (when it thinks best)
> upgrade the locks from a ROW level, to Page level or even Table level. If
> you are inserting enough rows into a table it is more efficient to lock
the
> whole table than each individual row. With no indexes it may even go right
> to a table level lock. By the way, there is no need to wrap that insert in
a
> transaction as it is ATOMIC as is.

> --
> Andrew J. Kelly
> Targitmail.com



> > Hi

> > We have interesting problem which can be easely reproduced on our
> > configuration NT4 + SP6A/ SQL7+SP3 / MDAC2.6
> > Database locks up the table after inserting aproximetly 3000 rows. It
> looks
> > like a bug in MSSql server.

> > This happends if we try to call stored procedure which execute just one
> SQL
> > statement - inser into a table.
> > This table does not have any indexes and consist of the only column:

> > MFC programm which calls this stored procedure from threads you can find
> in
> > attachments
> > (you should change connection string - DBTestDlg.cpp:line 18).

> --------------------------------------------------------------------------
> --
> > --
> > create  table tst
> > (
> >  c2 varchar(2048)  NOT NULL
> > )

> --------------------------------------------------------------------------
> --
> > ----
> > Before start the test you should trancate this table and shrink your
> > database:
> > truncate table tst
> > DBCC SHRINKDATABASE ([DBNAME])

> > stored proceduri is:

> --------------------------------------------------------------------------
> --
> > ----
> > create procedure tst_sp
> > as
> >   SET DEADLOCK_PRIORITY LOW
> >   SET LOCK_TIMEOUT 2000

> >   begin
> >     BEGIN TRANSACTION
> >   end




> >     ROLLBACK TRANSACTION
> >   ELSE
> >     COMMIT

> >   SET NOCOUNT OFF
> > --------------------------------------------------------------

> > Thread function jast do a loop:
> >   while(!m_bStop)
> >   {
> >    try
> >    {
> >     if(!(_con.BeginTrans()))
> >     {
> >      pDlg->addMessage("Can not begin transaction");
> >     }

> >     _con.ExecuteSQL("{call tst_sp}");

> >     if(!(_con.CommitTrans()))
> >     {
> >      pDlg->addMessage("Can not commit transaction");
> >     }

> >    }
> >    catch(CDBException& e)
> >    {
> >     //DebugBreak();

> >     pDlg->addMessage(e.m_strError);
> >     if(!(_con.Rollback()))
> >     {
> >      pDlg->addMessage("Can not rollback transaction");
> >     }

> >     _con.Close();
> >     _con.OpenEx(CONNECTION_STR,8);
> >    }
> >    catch(CDBException* e)
> >    {
> >     //DebugBreak();

> >     pDlg->addMessage(e->m_strError);
> >     e->Delete();
> >     if(!(_con.Rollback()))
> >     {
> >      pDlg->addMessage("Can not rollback transaction");
> >     }

> >     _con.Close();
> >     _con.OpenEx(CONNECTION_STR,8);
> >    }
> >    catch(EXCEPTION_POINTERS& pep)
> >    {
> >     //DebugBreak();
> >     char buffer[100];
> >     _ltoa(pep.ExceptionRecord->ExceptionCode, buffer, 16);
> >     pDlg->addMessage(CString("SEH exception: ")+=buffer);
> >     if(!(_con.Rollback()))
> >     {
> >      pDlg->addMessage("Can not rollback transaction");
> >     }
> >     _con.Close();
> >     _con.OpenEx(CONNECTION_STR,8);
> >    }
> >    catch(...)
> >    {
> >     //DebugBreak();
> >     pDlg->addMessage(CString("Unknown exception"));
> >     if(!(_con.Rollback()))
> >     {
> >      pDlg->addMessage("Can not rollback transaction");
> >     }
> >     _con.Close();
> >     _con.OpenEx(CONNECTION_STR,8);
> >    }
> >    Sleep(10);
> >   }

//-----------------------------------------------------------------------

- Show quoted text -


 
 
 

insert lock table.

Post by Andrew J. Kell » Fri, 11 May 2001 05:14:04


You must have an Open Transaction somewhere. Run DBCC OPENTRAN, sp_who2 and
sp_lock to see who or what has the table locked.

--
Andrew J. Kelly
Targitmail.com


> Hi Andrew,

> The problem is that it loks the table and does not unlock it after
> insertion. The problem happends as I wrote after insertion aproximatly
3000
> rows and can be reprodused on shrinked database. All threads after that
just
> can not insert anything into this table. They get message "Time out". It
is
> mean that table somehow have been locked.

> Thanks,
> Vladimir



> > I could be wrong since I didn't read word for word your entire post. But
> if
> > you are saying that SQL Server is issuing a table lock while inserting
> those
> > rows then that's perfectly normal. SQL Server will (when it thinks best)
> > upgrade the locks from a ROW level, to Page level or even Table level.
If
> > you are inserting enough rows into a table it is more efficient to lock
> the
> > whole table than each individual row. With no indexes it may even go
right
> > to a table level lock. By the way, there is no need to wrap that insert
in
> a
> > transaction as it is ATOMIC as is.

> > --
> > Andrew J. Kelly
> > Targitmail.com



> > > Hi

> > > We have interesting problem which can be easely reproduced on our
> > > configuration NT4 + SP6A/ SQL7+SP3 / MDAC2.6
> > > Database locks up the table after inserting aproximetly 3000 rows. It
> > looks
> > > like a bug in MSSql server.

> > > This happends if we try to call stored procedure which execute just
one
> > SQL
> > > statement - inser into a table.
> > > This table does not have any indexes and consist of the only column:

> > > MFC programm which calls this stored procedure from threads you can
find
> > in
> > > attachments
> > > (you should change connection string - DBTestDlg.cpp:line 18).

> --------------------------------------------------------------------------
> > --
> > > --
> > > create  table tst
> > > (
> > >  c2 varchar(2048)  NOT NULL
> > > )

> --------------------------------------------------------------------------
> > --
> > > ----
> > > Before start the test you should trancate this table and shrink your
> > > database:
> > > truncate table tst
> > > DBCC SHRINKDATABASE ([DBNAME])

> > > stored proceduri is:

> --------------------------------------------------------------------------
> > --
> > > ----
> > > create procedure tst_sp
> > > as
> > >   SET DEADLOCK_PRIORITY LOW
> > >   SET LOCK_TIMEOUT 2000

> > >   begin
> > >     BEGIN TRANSACTION
> > >   end




> > >     ROLLBACK TRANSACTION
> > >   ELSE
> > >     COMMIT

> > >   SET NOCOUNT OFF
> > > --------------------------------------------------------------

> > > Thread function jast do a loop:
> > >   while(!m_bStop)
> > >   {
> > >    try
> > >    {
> > >     if(!(_con.BeginTrans()))
> > >     {
> > >      pDlg->addMessage("Can not begin transaction");
> > >     }

> > >     _con.ExecuteSQL("{call tst_sp}");

> > >     if(!(_con.CommitTrans()))
> > >     {
> > >      pDlg->addMessage("Can not commit transaction");
> > >     }

> > >    }
> > >    catch(CDBException& e)
> > >    {
> > >     //DebugBreak();

> > >     pDlg->addMessage(e.m_strError);
> > >     if(!(_con.Rollback()))
> > >     {
> > >      pDlg->addMessage("Can not rollback transaction");
> > >     }

> > >     _con.Close();
> > >     _con.OpenEx(CONNECTION_STR,8);
> > >    }
> > >    catch(CDBException* e)
> > >    {
> > >     //DebugBreak();

> > >     pDlg->addMessage(e->m_strError);
> > >     e->Delete();
> > >     if(!(_con.Rollback()))
> > >     {
> > >      pDlg->addMessage("Can not rollback transaction");
> > >     }

> > >     _con.Close();
> > >     _con.OpenEx(CONNECTION_STR,8);
> > >    }
> > >    catch(EXCEPTION_POINTERS& pep)
> > >    {
> > >     //DebugBreak();
> > >     char buffer[100];
> > >     _ltoa(pep.ExceptionRecord->ExceptionCode, buffer, 16);
> > >     pDlg->addMessage(CString("SEH exception: ")+=buffer);
> > >     if(!(_con.Rollback()))
> > >     {
> > >      pDlg->addMessage("Can not rollback transaction");
> > >     }
> > >     _con.Close();
> > >     _con.OpenEx(CONNECTION_STR,8);
> > >    }
> > >    catch(...)
> > >    {
> > >     //DebugBreak();
> > >     pDlg->addMessage(CString("Unknown exception"));
> > >     if(!(_con.Rollback()))
> > >     {
> > >      pDlg->addMessage("Can not rollback transaction");
> > >     }
> > >     _con.Close();
> > >     _con.OpenEx(CONNECTION_STR,8);
> > >    }
> > >    Sleep(10);
> > >   }

> //-----------------------------------------------------------------------

 
 
 

1. BCP Insert Locks Table. Why?

Hi All,

  I've been using the BCP library for fast insertions into my tables.
I've discovered, using sp_lock, that the BCP functions obtain an _exclusive_
lock on each table while inserting. This is fine for occasional insertions,
but my particular application requires that browsers have maximal access to
existing data, even during inserts.

  Is there some way to prevent SQL Server from locking the entire table
during a BCP insert? (My tables have indexes that I'd rather not drop,
even temporarily. I'm also using program variables and bcp_bind(), if it makes
any difference). The manuals don't mention anything about the object-locking
tendencies of BCP.

  I'm no theorist, so if I can't get BCP to lock extents or pages instead of
entire tables, can someone out there justify this restriction?

Thanks in advance,

 - Russ Chamberlain
--
Russ Chamberlain      
University of Waterloo
Computer Systems Group

2. Oracle installer broken? Please help me!!! :)

3. MS Access97 + ODBC2.04 locks btrieve tables on Insert or Create Table queries

4. ! US - TRAVEL BUT NO RELO FOR QUALIFIED SYSTEMS ARCHITECTS

5. Table is locked, workstation coughs, table stays locked.

6. ipx/spx with access97

7. automatic change to table lock on insert

8. Visual Foxpro 6 database

9. Lock table for insert/update, not for read

10. Locking a Table For Inserts

11. Table lock on INSERT statement

12. Insert under transaction => table lock

13. How to insert to temp table without lock