473,387 Members | 3,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

SELECT statement within transaction

Let's say that I am performing a bunch of insert/update queries within
a transaction that is created as follows:

Dim cnn As New
SqlClient.SqlConnection(My.Settings.GRPConnectionS tring)
cnn.Open()
Dim trx As SqlClient.SqlTransaction = cnn.BeginTransaction()

Further suppose that, in the middle of all of the insert/update
queries, I want to execute a SELECT query against one of the tables
that is being affected by the aforementioned queries.

Will the SELECT query reflect changes made by them even if the
transaction has not yet been committed?

Do I have to use a separate connection to do the SELECT query?
Aug 11 '08 #1
6 5620
Bob,

You have normally only one connection, so everything is done one by one.

Cor

"BobRoyAce" <br**@omegasoftwareinc.comschreef in bericht
news:70**********************************@79g2000h sk.googlegroups.com...
Let's say that I am performing a bunch of insert/update queries within
a transaction that is created as follows:

Dim cnn As New
SqlClient.SqlConnection(My.Settings.GRPConnectionS tring)
cnn.Open()
Dim trx As SqlClient.SqlTransaction = cnn.BeginTransaction()

Further suppose that, in the middle of all of the insert/update
queries, I want to execute a SELECT query against one of the tables
that is being affected by the aforementioned queries.

Will the SELECT query reflect changes made by them even if the
transaction has not yet been committed?

Do I have to use a separate connection to do the SELECT query?
Aug 11 '08 #2
BobRoyAce <br**@omegasoftwareinc.comwrote:
Let's say that I am performing a bunch of insert/update queries within
a transaction that is created as follows:
-snip-
Will the SELECT query reflect changes made by them even if the
transaction has not yet been committed?
Yes. All commands within the transaction share the same scope. Think
about it, it has to be that way, otherwise a transaction would consist
of a single command.
Do I have to use a separate connection to do the SELECT query?
That depends upon what you want...

You should really play with Query Analyzer or SSMS, using transactions
and rollbacks and commits to see what does what you want to happen.

But let's briefly examine what *MIGHT* happen if you used two
connections
C1 -UPDATE Tbl Set Value = 1 Where Value = 2 -- ten rows effected

C2 -SELECT * FROM Tbl Where Value = 2 -- ten rows returned

But with two connections, that could happen because the command on C2
was executed before the command on C1 or while the command on C1 was
executing, or after and the transaction wasn't committed.

--
J.B. Moreno
Aug 11 '08 #3

"BobRoyAce" <br**@omegasoftwareinc.comwrote in message
news:70**********************************@79g2000h sk.googlegroups.com...
Let's say that I am performing a bunch of insert/update queries within
a transaction that is created as follows:

Dim cnn As New
SqlClient.SqlConnection(My.Settings.GRPConnectionS tring)
cnn.Open()
Dim trx As SqlClient.SqlTransaction = cnn.BeginTransaction()

Further suppose that, in the middle of all of the insert/update
queries, I want to execute a SELECT query against one of the tables
that is being affected by the aforementioned queries.
The commit must be made, otherwise, the insert and updates have not been
applied to that database table.
>
Will the SELECT query reflect changes made by them even if the
transaction has not yet been committed?
Yes, you have to commit the changes.
>
Do I have to use a separate connection to do the SELECT query?
You do the Select after the transitions are committed, you don't need a
second connection, you use the same connection that's already being used
leave it open after the commit and you move the Select statement out from
the middle of the code that executing the transactions.

Aug 11 '08 #4
Let's say that I am performing a bunch of insert/update queries within
a transaction that is created as follows:

Dim cnn As New
SqlClient.SqlConnection(My.Settings.GRPConnectionS tring)
cnn.Open()
Dim trx As SqlClient.SqlTransaction = cnn.BeginTransaction()

Further suppose that, in the middle of all of the insert/update
queries, I want to execute a SELECT query against one of the tables
that is being affected by the aforementioned queries.

Will the SELECT query reflect changes made by them even if the
transaction has not yet been committed?

Do I have to use a separate connection to do the SELECT query?
I've not used transactions with Sql Server and Visual Studio yet, but I have
used other DBMSs with other programming languages. How I would expect it to
work is that any query that you perform with a single connection, within the
scope of the same transaction, would see the changes that you have made,
even though the changes have not been committed to the database. Any other
connection, not within the scope of this transaction would not see the
changes until they have been committed. So whether you use a separate
connection or not depends on what you want to see.
Think of a transaction as working on a separate temporary copy of the
database.
For example
If I have a table with field 1 set to 1 in record 1, and I execute the
following SQL commands

--Begin Transaction --
UPDATE table1 SET field1=2 WHERE id=1
SELECT field1 FROM table1 WHERE id=1

I would expect this to return 2. However if the SELECT were performed using
a different connection I would expect it to return 1 (unless and until the
transaction is committed).
Things get a little more complicated if you have multiple transactions both
trying to update the same data.
Aug 11 '08 #5
I would conclude that I need to do the SELECT within the same
connection. However, the problem I was having, when trying to run with
a different connection, was that the query would just time out every
time. If I debugged through code, all the way up to, but not including
the SELECT query, then went to SQL Server Management Studio and tried
to run the SELECT query from there (while program execution is
suspended), I would get same result...timeout. So, was just trying to
figure out why that would be happening...
Aug 11 '08 #6

"BobRoyAce" <br**@omegasoftwareinc.comwrote in message
news:9a**********************************@m45g2000 hsb.googlegroups.com...
>I would conclude that I need to do the SELECT within the same
connection. However, the problem I was having, when trying to run with
a different connection, was that the query would just time out every
time. If I debugged through code, all the way up to, but not including
the SELECT query, then went to SQL Server Management Studio and tried
to run the SELECT query from there (while program execution is
suspended), I would get same result...timeout. So, was just trying to
figure out why that would be happening...
MS.Public.SQLserver.server

Aug 11 '08 #7

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

Similar topics

5
by: jayson_13 | last post by:
Hi, I need to implement a counter and i face problem of locking so hope that u guys can help me. I try to do test like this : 1st connection SELECT * FROM nextkey WHERE tblname = 'PLCN'...
4
by: Nick Barr | last post by:
Hi, I am trying to gather stats about how many times a resource in our web app is viewed, i.e. just a COUNT. There are potentially millions of resources within the system. I thought of two...
4
by: Ed L. | last post by:
I think I'm seeing table-level lock contention in the following function when I have many different concurrent callers, each with mutually distinct values for $1. Is there a way to reimplement...
1
by: Grant McLean | last post by:
Hi First a simple question ... I have a table "access_log" that has foreign keys "app_id" and "app_user_id" that reference the "application_type" and "app_user" tables. When I insert into...
2
by: Geoffrey KRETZ | last post by:
Hello, I'm wondering if the following behaviour is the correct one for PostGreSQL (7.4 on UNIX). I've a table temp_tab with 5 fields (f1,f2,f3,...),and I'm a launching the following request :...
5
by: rAinDeEr | last post by:
Hi, I have a web application with a table to store terms and conditions of a Company. This may some times run into many pages and some times it may be just a few sentences. It is a character...
19
by: Steve | last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement I got ASP error number 13 when I use the SELECT...FOR UPDATE statement as below. However, if I use SELECT statement without...
14
by: lewindletter | last post by:
Hi If I do Begin transaction Stmt 1: select count(*) from tableA If the count is zero, then proceed to the following Stmt 2: insert "something" to tableA Stmt 3: insert "something" to...
3
by: ThunderMusic | last post by:
Hi, We have a web application developped in asp.net (I think it's not relevant, but well, it's so you know)... Yesterday, we received the following message "Transaction (Process ID 69) was...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...

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.