Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Locking and Unlocking a table in SQL server

Author  Topic 

kelkins
Starting Member

4 Posts

Posted - 2010-12-17 : 13:56:10
Hi guys, I have a problem with a java servlet that I am writing.

I have a table which has some base 36 values in it. I need to:
-lock the table
-select these base 36 numbers
-increase their value by one (in java)
-save the new values to variables in my servlet (the values are used to write to other tables)
-update the table with the new values
-unlock the table

I have been unsuccessful in finding a way to do this without releasing the lock after I initially select the values. The table needs to be locked from reads and writes until after the values are updated. Thanks in advance.

Edit: This is a SQL Server 2005 database.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-12-17 : 14:17:26
Take a look at locking hints in Books Online. To lock the table, you can use TABLOCKX. You could alternatively use XLOCK with ROWLOCK to lock just the rows you are reading/touching.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

kelkins
Starting Member

4 Posts

Posted - 2010-12-17 : 14:40:35
I looked at TABLOCKX, but the problem is that the lock is released once you execute the statement. I have to execute it in order to get the current values to convert before I can write them back to the table. I could theoretically lock both transactions, but that still leaves a little time between them that something else could happen to the table.

Is there a SQL command that I can use that will lock the table until I tell it to unlock?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-12-17 : 14:49:19
Well you have to be inside a transaction.

BEGIN TRAN

Do your steps here

COMMIT TRAN or ROLLBACK TRAN

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

kelkins
Starting Member

4 Posts

Posted - 2010-12-17 : 14:55:58
When I "BEGIN TRANSACTION" will this lock the entire database or just the tables that are specified in the statements that follow?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-12-17 : 15:03:45
Typically it's just the rows inside the transaction. When you add additional locking hints such as TABLOCKX, it'll lock more. But it won't lock the entire database, just what is in scope.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

kelkins
Starting Member

4 Posts

Posted - 2010-12-17 : 15:06:22
Awesome! Thanks you so much for your help :0)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-12-17 : 15:08:02
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -