Improve company productivity with a Business Account.Sign Up

x
?
Solved

Record Locking

Posted on 2001-07-20
10
Medium Priority
?
419 Views
Last Modified: 2011-09-20
Please can someone explain the best way to stop duplicates in SQL Server 7.

I have a web page which via 2 recordsets finds the next id number + 1, then saves the record in the database through the second recordset doing rs.addnew.

I want to make sure there is no way even with two people pushing the buttons at the same time can create the same id.
0
Comment
Question by:andy_booth
10 Comments
 
LVL 33

Expert Comment

by:hongjun
ID: 6301654
Use transaction something like this.

objConn.BeginTrans
objCon.Execute("insert into your_table values (1)")
objConn.CommitTrans


hongjun
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6301670
This is a tricky problem. It is almost impossible to guarantee no duplicates. The easiest way is to use a serial type field when adding new records. SQL server allots the number automatically. The problem is if you want to recuperate the alloted number immediately after the update. Another easy method (but again not foolproof) is to do a "SELECT MAX(idfield) FROM table" just before the insert. Add 1 to this value and do the INSERT. The safest way (in my opinion) is to use MTS shared property groups for this. Using these, with multi users accessing the same property value, the first to arrive takes the lead, the others are queued until the first releases the property, and so on and so forth, thus avoiding any DB concurrency locks on the table. This method, however, implies an ActiveX component. I don't know how complicated you want to go.

Hope this helps
0
 
LVL 1

Author Comment

by:andy_booth
ID: 6301671
Where do I fit that into my code. Posted below...

sql = "SELECT MAX(quote_reference) FROM quote"
   
   set conn = Server.CreateObject("ADODB.Connection")
   conn.Open session("ses_connection")
   set cmd = Server.CreateObject("ADODB.Command")
   cmd.CommandText = sql
   set cmd.ActiveConnection = conn
   set rs = Server.CreateObject("ADODB.Recordset")
   set rs = cmd.Execute
   
   session("ses_quote_reference") = rs.Fields(0).Value
   
   If isnull(session("ses_quote_reference")) Then
    session("ses_quote_reference") = 1
   Else
    session("ses_quote_reference") = session("ses_quote_reference") + 1
   End If
   
   sql_insert = "SELECT * FROM quote WHERE 1=0"
   
   set conn_insert = Server.CreateObject("ADODB.Connection")
   conn_insert.Open session("ses_connection")
   set cmd_insert = Server.CreateObject("ADODB.Command")
   cmd_insert.CommandText = sql_insert
   set cmd_insert.ActiveConnection = conn_insert
   set rs_insert = Server.CreateObject("ADODB.Recordset")
   rs_insert.Open sql_insert, conn_insert, 2, 3
   
   rs_insert.AddNew    
   rs_insert.Fields("quote_reference").Value = session("ses_quote_reference")
   rs_insert.Fields("quote_type").Value = "Truck"
   rs_insert.Fields("status").Value = "Incomplete"
   rs_insert.Fields("date_added").Value = date()
   rs_insert.fields("broker").value = session("ses_username")
   rs_insert.update
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
LVL 33

Expert Comment

by:hongjun
ID: 6301678
<%
  sql = "SELECT MAX(quote_reference) FROM quote"
 
  set conn = Server.CreateObject("ADODB.Connection")
  conn.Open session("ses_connection")
  set cmd = Server.CreateObject("ADODB.Command")
  cmd.CommandText = sql
  set cmd.ActiveConnection = conn
  set rs = Server.CreateObject("ADODB.Recordset")
  set rs = cmd.Execute
 
  session("ses_quote_reference") = rs.Fields(0).Value
 
  If isnull(session("ses_quote_reference")) Then
   session("ses_quote_reference") = 1
  Else
   session("ses_quote_reference") = session("ses_quote_reference") + 1
  End If
 
  sql_insert = "SELECT * FROM quote WHERE 1=0"
 
  set conn_insert = Server.CreateObject("ADODB.Connection")
  conn_insert.Open session("ses_connection")
  set cmd_insert = Server.CreateObject("ADODB.Command")
  cmd_insert.CommandText = sql_insert
  set cmd_insert.ActiveConnection = conn_insert
  set rs_insert = Server.CreateObject("ADODB.Recordset")

  conn.begintrans

  rs_insert.Open sql_insert, conn_insert, 2, 3
 
  rs_insert.AddNew    
  rs_insert.Fields("quote_reference").Value = session("ses_quote_reference")
  rs_insert.Fields("quote_type").Value = "Truck"
  rs_insert.Fields("status").Value = "Incomplete"
  rs_insert.Fields("date_added").Value = date()
  rs_insert.fields("broker").value = session("ses_username")
  rs_insert.update

  conn.committrans
%>


hongjun
0
 
LVL 33

Accepted Solution

by:
hongjun earned 200 total points
ID: 6301685
You might also need this

<%@Language=VBScript transaction=required%>

<%
Sub OnTransactionCommit()
' put anything you want here
End Sub

Sub OnTransactionAbort()
     Response.write ("<br><br><font color=red size=4>Transaction Failed! Please contact system adminstrator.</font>")
     Response.End
End Sub
%>


<%
 sql = "SELECT MAX(quote_reference) FROM quote"
 
 set conn = Server.CreateObject("ADODB.Connection")
 conn.Open session("ses_connection")
 set cmd = Server.CreateObject("ADODB.Command")
 cmd.CommandText = sql
 set cmd.ActiveConnection = conn
 set rs = Server.CreateObject("ADODB.Recordset")
 set rs = cmd.Execute
 
 session("ses_quote_reference") = rs.Fields(0).Value
 
 If isnull(session("ses_quote_reference")) Then
  session("ses_quote_reference") = 1
 Else
  session("ses_quote_reference") = session("ses_quote_reference") + 1
 End If
 
 sql_insert = "SELECT * FROM quote WHERE 1=0"
 
 set conn_insert = Server.CreateObject("ADODB.Connection")
 conn_insert.Open session("ses_connection")
 set cmd_insert = Server.CreateObject("ADODB.Command")
 cmd_insert.CommandText = sql_insert
 set cmd_insert.ActiveConnection = conn_insert
 set rs_insert = Server.CreateObject("ADODB.Recordset")

 conn.begintrans

 rs_insert.Open sql_insert, conn_insert, 2, 3
 
 rs_insert.AddNew    
 rs_insert.Fields("quote_reference").Value = session("ses_quote_reference")
 rs_insert.Fields("quote_type").Value = "Truck"
 rs_insert.Fields("status").Value = "Incomplete"
 rs_insert.Fields("date_added").Value = date()
 rs_insert.fields("broker").value = session("ses_username")
 rs_insert.update

 conn.committrans

' other stuff here
%>

hongjun
0
 
LVL 1

Author Comment

by:andy_booth
ID: 6301759
Hongjun,

I have added your code, (thanks for the help so far!)

However it runs your Sub OnTransactionAbort() everytime I try to add a record.

Here is the error!

Microsoft OLE DB Provider for SQL Server error '8004d013'

Cannot start more transactions on this session.

/croweonline_dev/home_quote.asp, line 174

Transaction Failed! Please contact system adminstrator.
0
 
LVL 10

Expert Comment

by:makerp
ID: 6301819
wrap an

Application.Lock
 'your sensitive code
Application.Unlock

around the code in question, this will serialize access to to it, it is a heavy handed approach as it temporarily locks other users scripts. although it dont reallt matter if the script dont take long to execute
0
 
LVL 1

Author Comment

by:andy_booth
ID: 6302329
Had to play around a little bit. But got it to work.

Thanks very much.
0
 
LVL 1

Expert Comment

by:OverSurge
ID: 10583263
Andy.. I am getting this error with a SP that I am currently calling on a project I am working on. What did you end up changing to fix the "Cannot start more transactions on this session." ?
0
 
LVL 1

Author Comment

by:andy_booth
ID: 10596529
This was in ASP Oversurge.

MakerP's code worked for me.

Application.Lock
 'your sensitive code
Application.Unlock
0

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Through the video, you can check the migration process of Outlook PST file to PDF. Kernel for Outlook to PDF tool can convert Outlook emails with all attributes like Subject, To, From, Cc, Bcc and other folders such as Inbox, Outbox, Sent Items, Jun…
Go through the video which explain the procedure to export Amazon WorkMail single or multiple mailboxes to Outlook with Kernel Export Amazon WorkMail to PST tool. User can see the simple steps to export. It has salient features like filters (Include…

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question