Hi Jonny,
Welcome to posting in ADO.NET newsgroup! This is Colbert Zhou [MSFT] and I
will be working on this issue with you.
In this post, I'd like to analyze the cause of this issue firstly, and then
I will provide three solutions. Each solution will also contain its
strength and weakness analysis.
----------------------------------
CAUSE:
The default transaction isolation level in SQL Server 2005 is "READ
COMMITTED WITH LOCKS", which means if one transaction is updating the data
source, before this transaction commits, other operation related to this
data source will be locked and waiting for the updating transaction ends.
So, that is why the GUI application will be locked if the updating batch
application starts.
For detail on transaction isolation levels, please see
http://msdn.microsoft.com/en-us/library/ms173763(SQL.90).aspx.
---------------------------------
SOLUTIONS:
Solution 1: Set Transaction Isolation Level to READ UNCOMMITTED
"READ UNCOMMITTED" level can let the reading transaction get data which is
modified by other transactions but not yet committed. Through this
method, the GUI application can read the data which has been modified by
the updating transaction anytime during the updating transaction is
working.
For how to set transaction isolation level to "READ UNCOMMITTED", you can
refer to
http://msdn.microsoft.com/en-us/library/ms173763(SQL.90).aspx.
For C# codes to set transaction isolated level to "READ UNCOMMITTED",
please see following code snippet:
using (SqlConnection cn = new SqlConnection())
{
cn.ConnectionString = connectionString;
cn.Open();
using (SqlTransaction tran =
cn.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted))
{
// Query data
}
}
[Strength]
This method is easy to achieve.
[Weakness]
Each read operation should use a transaction, so it will affect the
application performance.
After one read query for the data source, the GUI application may get some
newly updated data and some original data. It may cause some confusion to
the user.
Solution 2: Set Transaction Isolation Level to SNAPSHOT
"SNAPSHOT" transaction isolation level can let the reading transaction get
the data which existed at the start of the updating transaction. So the
GUI application will retrieve the original data all the time when the
updating transaction is working. Meanwhile, to use this transaction
isolation level, we need to first turn on the database snapshot setting.
We can first turn on the database snapshot setting via this SQL command:
ALTER DATABASE DatabaseName
SET ALLOW_SNAPSHOT_ISOLATION ON
Then we can use the similar method to set transaction isolation level to
SNAPSHOT which I discussed in the first solution.
[Strength]
This method is also easy to achieve. Comparing to the first solution, when
the updating transaction is working, we will get the same data via this
method.
[Weakness]
Each reading query will be contained in a transaction, so this method will
also affect application performance.
Solution 3: Customize a Database Snapshot
Before we start updating transaction, we can also using SQL Server 2005 new
feature to create a database snapshot. So when the updating transaction is
working, we can operate the data in the database snapshot directly.
For detail introduction on database snapshot, please see
http://msdn.microsoft.com/en-us/library/ms175158(SQL.90).aspx.
For how to create a database snapshot in SQL Server 2005, please refer to
http://technet.microsoft.com/en-us/library/ms175876(SQL.90).aspx.
[Strength]
Through this method, there is no need to create a transaction to read the
data source, so if we need to query the data many times when the updating
transaction is working, this solution can improve application performance.
[Weakness]
This solution is more difficult to achieve than solution one and solution
two.
If you have any questions, please be free to let me know. Have a nice day,
Jonny!
Best regards,
Colbert Zhou (colbertz @online.microsoft.com, remove 'online.')
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.