andy_booth
asked on
Record Locking
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.
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.
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
Hope this helps
ASKER
Where do I fit that into my code. Posted below...
sql = "SELECT MAX(quote_reference) FROM quote"
set conn = Server.CreateObject("ADODB .Connectio n")
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_referen ce") = rs.Fields(0).Value
If isnull(session("ses_quote_ reference" )) Then
session("ses_quote_referen ce") = 1
Else
session("ses_quote_referen ce") = session("ses_quote_referen ce") + 1
End If
sql_insert = "SELECT * FROM quote WHERE 1=0"
set conn_insert = Server.CreateObject("ADODB .Connectio n")
conn_insert.Open session("ses_connection")
set cmd_insert = Server.CreateObject("ADODB .Command")
cmd_insert.CommandText = sql_insert
set cmd_insert.ActiveConnectio n = 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_re ference"). Value = session("ses_quote_referen ce")
rs_insert.Fields("quote_ty pe").Value = "Truck"
rs_insert.Fields("status") .Value = "Incomplete"
rs_insert.Fields("date_add ed").Value = date()
rs_insert.fields("broker") .value = session("ses_username")
rs_insert.update
sql = "SELECT MAX(quote_reference) FROM quote"
set conn = Server.CreateObject("ADODB
conn.Open session("ses_connection")
set cmd = Server.CreateObject("ADODB
cmd.CommandText = sql
set cmd.ActiveConnection = conn
set rs = Server.CreateObject("ADODB
set rs = cmd.Execute
session("ses_quote_referen
If isnull(session("ses_quote_
session("ses_quote_referen
Else
session("ses_quote_referen
End If
sql_insert = "SELECT * FROM quote WHERE 1=0"
set conn_insert = Server.CreateObject("ADODB
conn_insert.Open session("ses_connection")
set cmd_insert = Server.CreateObject("ADODB
cmd_insert.CommandText = sql_insert
set cmd_insert.ActiveConnectio
set rs_insert = Server.CreateObject("ADODB
rs_insert.Open sql_insert, conn_insert, 2, 3
rs_insert.AddNew
rs_insert.Fields("quote_re
rs_insert.Fields("quote_ty
rs_insert.Fields("status")
rs_insert.Fields("date_add
rs_insert.fields("broker")
rs_insert.update
<%
sql = "SELECT MAX(quote_reference) FROM quote"
set conn = Server.CreateObject("ADODB .Connectio n")
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_referen ce") = rs.Fields(0).Value
If isnull(session("ses_quote_ reference" )) Then
session("ses_quote_referen ce") = 1
Else
session("ses_quote_referen ce") = session("ses_quote_referen ce") + 1
End If
sql_insert = "SELECT * FROM quote WHERE 1=0"
set conn_insert = Server.CreateObject("ADODB .Connectio n")
conn_insert.Open session("ses_connection")
set cmd_insert = Server.CreateObject("ADODB .Command")
cmd_insert.CommandText = sql_insert
set cmd_insert.ActiveConnectio n = 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_re ference"). Value = session("ses_quote_referen ce")
rs_insert.Fields("quote_ty pe").Value = "Truck"
rs_insert.Fields("status") .Value = "Incomplete"
rs_insert.Fields("date_add ed").Value = date()
rs_insert.fields("broker") .value = session("ses_username")
rs_insert.update
conn.committrans
%>
hongjun
sql = "SELECT MAX(quote_reference) FROM quote"
set conn = Server.CreateObject("ADODB
conn.Open session("ses_connection")
set cmd = Server.CreateObject("ADODB
cmd.CommandText = sql
set cmd.ActiveConnection = conn
set rs = Server.CreateObject("ADODB
set rs = cmd.Execute
session("ses_quote_referen
If isnull(session("ses_quote_
session("ses_quote_referen
Else
session("ses_quote_referen
End If
sql_insert = "SELECT * FROM quote WHERE 1=0"
set conn_insert = Server.CreateObject("ADODB
conn_insert.Open session("ses_connection")
set cmd_insert = Server.CreateObject("ADODB
cmd_insert.CommandText = sql_insert
set cmd_insert.ActiveConnectio
set rs_insert = Server.CreateObject("ADODB
conn.begintrans
rs_insert.Open sql_insert, conn_insert, 2, 3
rs_insert.AddNew
rs_insert.Fields("quote_re
rs_insert.Fields("quote_ty
rs_insert.Fields("status")
rs_insert.Fields("date_add
rs_insert.fields("broker")
rs_insert.update
conn.committrans
%>
hongjun
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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_quot e.asp, line 174
Transaction Failed! Please contact system adminstrator.
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_quot
Transaction Failed! Please contact system adminstrator.
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
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
ASKER
Had to play around a little bit. But got it to work.
Thanks very much.
Thanks very much.
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." ?
ASKER
This was in ASP Oversurge.
MakerP's code worked for me.
Application.Lock
'your sensitive code
Application.Unlock
MakerP's code worked for me.
Application.Lock
'your sensitive code
Application.Unlock
objConn.BeginTrans
objCon.Execute("insert into your_table values (1)")
objConn.CommitTrans
hongjun