473,761 Members | 7,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Concurrent Access in Transactions

Hey Ppl,

I'm developing an Online Auction Site using ASP.net and am
experiencing a problem with Transactions in ADO.Net.

When beginTrasaction () function is invoked from a specific connection,
and insertions are made into a table, no other connection can access
the table, until the transaction is committed. I've set the
Isoloation level to ReadUncommited.

This is a problem because, I'm expecting 100s of concurrent users, and
key tables being unavailable for reads (something like pessimestic
locking), would lead to disastrous performance.

What should I do?

Following is the code excerpt for your reference:

Public Function addItem(ByVal p_Name As String, ByVal
p_Description As String, ByVal p_ItemImage As HtmlInputFile, ByVal
p_Category1ID As Integer, ByVal p_EndTime As String, ByVal
p_StartPrice As Integer, ByVal p_ReservedPrice As Integer, ByVal
p_BidIncrement As Integer, ByVal p_MarketPrice As Integer, ByVal
p_Quantity As Integer, ByVal p_Warranty As Boolean, ByVal p_OwnerID As
String) As Boolean

'Use of Transactions in this routine may pose a problem
with multiple users

Dim sqlTransaction As SqlClient.SqlTr ansaction
Dim sqlConn As New SqlClient.SqlCo nnection

Try
sqlConn.Connect ionString = getDBConnString ()

sqlConn.Open()
sqlTransaction =
sqlConn.BeginTr ansaction(Isola tionLevel.ReadU ncommitted)
Dim sqlString As String = "INSERT INTO Items (Name,
Description, StartTime, EndTime, StartPrice, ReservedPrice,
BidIncrement, Quantity, ExpectedMarketP rice, InWarranty,
OwnerID,SellTyp eID,SellPrice) VALUES ('" _
& sqlEncode(p_Nam e) & "','" &
sqlEncode(p_Des cription) & "', { fn NOW() }, { fn NOW() }+" _
& sqlEncode(p_End Time) & ", " &
sqlEncode(p_Sta rtPrice) & "," & sqlEncode(p_Res ervedPrice) _
& "," & sqlEncode(p_Bid Increment) & "," &
sqlEncode(p_Qua ntity) & "," & sqlEncode(p_Mar ketPrice) _
& "," & IIf(p_Warranty, 1, 0) & "," &
sqlEncode(p_Own erID) & ",1," & p_StartPrice & ")"

DataAccess.exec uteQueryTI(sqlS tring, sqlConn,
sqlTransaction)
Dim dsItem As DataSet
dsItem = New DataSet
sqlString = "SELECT ItemID from Items WHERE Name='" &
sqlEncode(p_Nam e) & "' and Description='" & sqlEncode(p_Des cription) &
"' and ReservedPrice=" & sqlEncode(p_Res ervedPrice) & " and
BidIncrement=" & sqlEncode(p_Bid Increment) & " and Quantity=" &
sqlEncode(p_Qua ntity) & " and ExpectedMarketP rice=" &
sqlEncode(p_Mar ketPrice) & " and OwnerID=" & sqlEncode(p_Own erID)
DataAccess.getD ataTI("item", sqlString, dsItem,
sqlConn, sqlTransaction)
'DataAccess.get Data("Item", sqlString,
getDBConnString (), dsItem)

Dim itemID As String =
dsItem.Tables(" item").Rows(0). Item(0)

'uploading the image

Dim fn As String =
System.IO.Path. GetFileName(p_I temImage.Posted File.FileName)
Dim SaveLocation As String =
"c:\Inetpub\www root\ZabAuction z\Data\" & itemID &
System.IO.Path. GetExtension(p_ ItemImage.Poste dFile.FileName)
p_ItemImage.Pos tedFile.SaveAs( SaveLocation)
sqlString = "UPDATE Items SET Image ='data/" &
sqlEncode(itemI D) & System.IO.Path. GetExtension(p_ ItemImage.Poste dFile.FileName)
& "' WHERE ItemID=" & sqlEncode(itemI D)
DataAccess.exec uteQueryTI(sqlS tring, sqlConn,
sqlTransaction)
sqlString = "SELECT "
sqlString = "INSERT INTO ItemCategories VALUES (" &
sqlEncode(itemI D) & ", " & sqlEncode(p_Cat egory1ID) & ")"
DataAccess.exec uteQueryTI(sqlS tring, sqlConn,
sqlTransaction)
sqlTransaction. Commit()

Catch ex As Exception
sqlTransaction. Rollback()
End Try

End Function

Is there any way to maintain transaction integrity, while allowing
concurrent access to the tables.
Nov 20 '05 #1
2 2372
Adnan:

A few things. First, you are using Dynamic SQL and you aren't using
parameters. This is just a recipe for disaster.
Here's how to use Params w/ Stored procedures
http://www.knowdotnet.com/articles/storedprocsvb.html and here's a whole
discussion on Dynamic SQL and how to replace it (both why and how)
http://msmvps.com/williamryan/posts/4063.aspx

First off, it'd really help in every regard if you cleaned up the code.
There's some stuff that's defintely not necessary, Option Strict isn't on
(and Option Strict Off = Option Slow On AND Option ErrorProne On)

Next, when you do something like ExecuteNonQuery or ExecuteReader, there
will be a momentary lock on the record while the operation is being
performed. This is a safeguard of your data integrity. Now, looking at
that code below.. if you are doing this with SQL Server, I'd seriously
recommend losing the whole client side Transaction approach. At best you'll
have a solution that's never going to be very good. I'd replace this with
Stored procedures and implement the Transactions in the procs. In the
instance below, the flow of the transaction seems a bit bizarre. You have
more datasets than you need, you set the command text to "SELECT' then you
just write over it, it looks like setting the value to Select does nothing
in the one part b/c you immediately write over it. You always reference
colum0 and row 0 of the datatable,so if you are filling a datatable that
only is used to hold one row, you could probably work around this. You
could also make a field that indicates "OK". At first, set it to not ok..
At the end when you get all the data and you know everything is good you can
fire a query to make it "OK" and if that fails, you can have logic ot kill
the record. This is a convoluted way I know, but it wouldn't lock the table.

Anyway, I think the problem you are having is the structure of the
transaction.. I don't think you need to wrap everything like this and even
if I'm really misreading it and you do, it would definitely be better to
wrap it in a proc and do it server side. Transactions are just much more
natural there. I can see a few other problems here but correcting one thing
would break a few other things that are also wrong. If you at a minimum
parameterize this, it'll be a lot cleaner. For instance, can't you get that
fielname or restructure how that works so you can use this in another
command? I bet you could use some Output Parameters and get values back
differently and cut out the select statement, or at least append it onto the
insert. that p_OwnerID looks like a prime candidate for using Output params
but it's really hard to follow b/c of the dynamic sql.

I'll walk you through it if you want, but let me know first b/c I don't know
how much you are willing to change. Here's a starting place with params
(but seriously, if we can, let's go with Stored procs and server side
Transactions:) [Use StringBuilders too not b/c it's causing the problem but
just to be efficient)
Dim sqlStringBuilde r as new StringBuilder
sb.Append("INSE RT INTO Items(Name
, Description
, StartTime
, EndTime
, StartTime
, ReserevedPrice
, BidIncrement
, Quantity
, ExpectedMarketP rice
, InWarranty
, OwnerID
, SellTypeID
, SellPrice)
sb.Append(" VALUES @pName
, @pDescription
, Now()
, Now()
, @pEndTime
, @pStartPrice
, @pReservedPrice
, @pBidIncrement
, @pQuantity
, @pMarketPrice
, @pOwnerID
, @pWarranty ")

With cmd.Parameters
.Add("@pDescrip tion", SqlDbType.Varch ar, 50).value = p_Name
.Add("@pEndTime ", SqlDbType.DateT ime).Value = p_EndTime
'keep on here.
End With

--
W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/
"Adnan" <ah******@yahoo .com> wrote in message
news:ad******** *************** ***@posting.goo gle.com...
Hey Ppl,

I'm developing an Online Auction Site using ASP.net and am
experiencing a problem with Transactions in ADO.Net.

When beginTrasaction () function is invoked from a specific connection,
and insertions are made into a table, no other connection can access
the table, until the transaction is committed. I've set the
Isoloation level to ReadUncommited.

This is a problem because, I'm expecting 100s of concurrent users, and
key tables being unavailable for reads (something like pessimestic
locking), would lead to disastrous performance.

What should I do?

Following is the code excerpt for your reference:

Public Function addItem(ByVal p_Name As String, ByVal
p_Description As String, ByVal p_ItemImage As HtmlInputFile, ByVal
p_Category1ID As Integer, ByVal p_EndTime As String, ByVal
p_StartPrice As Integer, ByVal p_ReservedPrice As Integer, ByVal
p_BidIncrement As Integer, ByVal p_MarketPrice As Integer, ByVal
p_Quantity As Integer, ByVal p_Warranty As Boolean, ByVal p_OwnerID As
String) As Boolean

'Use of Transactions in this routine may pose a problem
with multiple users

Dim sqlTransaction As SqlClient.SqlTr ansaction
Dim sqlConn As New SqlClient.SqlCo nnection

Try
sqlConn.Connect ionString = getDBConnString ()

sqlConn.Open()
sqlTransaction =
sqlConn.BeginTr ansaction(Isola tionLevel.ReadU ncommitted)
Dim sqlString As String = "INSERT INTO Items (Name,
Description, StartTime, EndTime, StartPrice, ReservedPrice,
BidIncrement, Quantity, ExpectedMarketP rice, InWarranty,
OwnerID,SellTyp eID,SellPrice) VALUES ('" _
& sqlEncode(p_Nam e) & "','" &
sqlEncode(p_Des cription) & "', { fn NOW() }, { fn NOW() }+" _
& sqlEncode(p_End Time) & ", " &
sqlEncode(p_Sta rtPrice) & "," & sqlEncode(p_Res ervedPrice) _
& "," & sqlEncode(p_Bid Increment) & "," &
sqlEncode(p_Qua ntity) & "," & sqlEncode(p_Mar ketPrice) _
& "," & IIf(p_Warranty, 1, 0) & "," &
sqlEncode(p_Own erID) & ",1," & p_StartPrice & ")"

DataAccess.exec uteQueryTI(sqlS tring, sqlConn,
sqlTransaction)
Dim dsItem As DataSet
dsItem = New DataSet
sqlString = "SELECT ItemID from Items WHERE Name='" &
sqlEncode(p_Nam e) & "' and Description='" & sqlEncode(p_Des cription) &
"' and ReservedPrice=" & sqlEncode(p_Res ervedPrice) & " and
BidIncrement=" & sqlEncode(p_Bid Increment) & " and Quantity=" &
sqlEncode(p_Qua ntity) & " and ExpectedMarketP rice=" &
sqlEncode(p_Mar ketPrice) & " and OwnerID=" & sqlEncode(p_Own erID)
DataAccess.getD ataTI("item", sqlString, dsItem,
sqlConn, sqlTransaction)
'DataAccess.get Data("Item", sqlString,
getDBConnString (), dsItem)

Dim itemID As String =
dsItem.Tables(" item").Rows(0). Item(0)

'uploading the image

Dim fn As String =
System.IO.Path. GetFileName(p_I temImage.Posted File.FileName)
Dim SaveLocation As String =
"c:\Inetpub\www root\ZabAuction z\Data\" & itemID &
System.IO.Path. GetExtension(p_ ItemImage.Poste dFile.FileName)
p_ItemImage.Pos tedFile.SaveAs( SaveLocation)
sqlString = "UPDATE Items SET Image ='data/" &
sqlEncode(itemI D) & System.IO.Path. GetExtension(p_ ItemImage.Poste dFile.FileName) & "' WHERE ItemID=" & sqlEncode(itemI D)
DataAccess.exec uteQueryTI(sqlS tring, sqlConn,
sqlTransaction)
sqlString = "SELECT "
sqlString = "INSERT INTO ItemCategories VALUES (" &
sqlEncode(itemI D) & ", " & sqlEncode(p_Cat egory1ID) & ")"
DataAccess.exec uteQueryTI(sqlS tring, sqlConn,
sqlTransaction)
sqlTransaction. Commit()

Catch ex As Exception
sqlTransaction. Rollback()
End Try

End Function

Is there any way to maintain transaction integrity, while allowing
concurrent access to the tables.

Nov 20 '05 #2
Bill,

I'm really thankful for your detailed response.
I've carefully read the given links, and see the prudence in
parameterized queries and stored procedures. I'll be reworking the
highest-trafikked web-pages, according to the approach you demonstrated.

I made the decision to go with dynamic SQL, because I wanted to keep the
code database independent. Its currently on MsSql, I aim to shift it to
mySql latter. And I assumed that using storedProcs would've made porting
difficult.

Thanks again, will seek your advice along the way.

Regards,

A H

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
3040
by: Eloff | last post by:
This is not really Python specific, but I know Python programmers are among the best in the world. I have a fair understanding of the concepts involved, enough to realize that I would benefit from the experience of others :) I have a shared series of objects in memory that may be > 100MB. Often to perform a task for a client several of these objects must be used. Since many clients can be making requests at once (>100 per second during...
1
1779
by: Robert Zurer | last post by:
My architecture is, (ala Fowler PoEAA) Presentation Layer | Service Layer | Problem Domain (My Business Objects are held in memory) | Persistence Layer
11
5449
by: Durai | last post by:
Hi All, I tested "concurrent testing" in MySQL. It works fine. But I couldn't do in PostgreSQL 7.3.4 on HPUX IPF. I got deadlock problem. I used the PHP script to update table( one script increment the column & another one decrement the column). Is the postgres support the concurrent access to update database? I got the following errors: test=# ERROR: deadlock detected ERROR: deadlock detected ERROR: deadlock detected .....
8
4029
by: pnp | last post by:
Hi all, I've developed a win C# app that is actually database driven using SQL server 2000. The idea is that only one application will be installed on a server in a network and the program will be able to run at each client machine by just double-clicking the application executable through a network share. The program supports user logins. What I want to do is find a secure way to control the number of concurrent *users* using the...
5
1446
by: Jill Graham | last post by:
Hi, What is the best way to resolve following problem in a multi-user environment ? I have a table called TOTAL_SALES. The table has 2 columns : "CarId" and "TotalSales" I need to access a given car in the TOTAL_SALES table using the "CarId", read the "TotalSales" value and increment this value with 1 unit.
2
2700
by: Sridhar | last post by:
Hi, I am trying to implement sql transactions. But I am not knowing how to do that. I created a data access layer which contains methods to select/insert/update tables in a database. I have also created a business logic layer to retrieve/update the data from the data access layer. All of this is working fine if there are no transactions involved. If I need to use transactions, I am not knowing how to do that. Lets say I have a webform...
9
2387
by: rysch | last post by:
Hi, I am building a warehouse database for a small non-profit organisation that works in Africa. Currently, I have created a data entry form and table. The table is called: Inventory Transactions. This table contains information regarding the donation or purchase of a product. One of the reports I am making is a shipping manifest. I want to take the information from Inventory Transactions and put five fields from that table into the table...
3
2897
by: Michael Schöller | last post by:
Hello, First of all english is not my natural language so please fogive me some bad mistakes in gramatic and use of some vocables :). I have a great problem here. Well I will not use it anymore but I want to know why it is as it is ^^. I tried with .NET3.0 but I think it will be the same with 2.0 and 3.5. MSDTC is configured and working.
0
13361
amitpatel66
by: amitpatel66 | last post by:
There is always a requirement that in Oracle Applications, the Concurrent Program need to be execute programatically based on certain conditions/validations: Concurrent programs can be executed programatically either from UNIX or Oracle PLSQL. In this Section, I will be explaining about calling a Concurrent program from UNIX using the CONCSUB Command. Pre-requisite: 1. Concurrent Program should be registered in oracle Applications...
0
9531
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9957
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8780
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7332
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3881
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.