ASp.Net day

Micro blog

transaction

July 21
by Satalaj 21. July 2010 09:21


  Many time row processing becomes dependent process.


Below stuff will shade some lights on

1. Transaction using ADO.net application
2. Transaction using SQL server / stored procedure


E.g. Your application has processed the CC information to third party web and its waiting
       for response.
       What if their site is down? or there is network falure or communication error?

Obviousely, you need to roll back the transaction. Using C#.Net below code snippet will help you to understand and use
SqlTransaction techniuque

Code is self explanatory


// create SQL connection object and pass connection string to it from application settings

 SqlConnection sqlconnect = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);


// create SQL command object

            SqlCommand sqlcommdt = new SqlCommand();                       

// Tell command object where to connect

            sqlcommdt.Connection = sqlconnect;

// Tell command object what to do once get connected

// In this case, I'm telling it to execute Stored procedure

            sqlcommdt.CommandText = "SP_Name";

// set type of command object

            sqlcommdt.CommandType = CommandType.StoredProcedure;
    
       

            try
            {

//  Open connection

                sqlcommdt.Connection.Open();

//  Create SQL Transaction

                SqlTransaction transaction = sqlconnect.BeginTransaction();

// Tell command object to use SQL Transaction object

                sqlcommdt.Transaction = transaction;

                DataTable dt = new DataTable();

                dt.Load(sqlcommdt.ExecuteReader());
               
                try
                {

                    if (dt.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {

                            // Process the row
                            // It may throw excetion. In this case, immediate catch block will throw that exception to outer one.
                         
                              ProcessRow(dr);   

                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }

// If there is no exception, commit the transaction.

                sqlcommdt.Transaction.Commit();
            }

            catch (Exception ex)
            {
           
// If there is an error, you can roll back the transaction.

                sqlcommdt.Transaction.Rollback();
            }


2. When to go for SQL transaction using stored procedure?

See below code snippet.

 Begin transaction tr1
   begin try
  
           -- Do insertion on one table
           -- Do deletion from xyz table

   -- If every thing worked fine commit transaction.

    commit transaction tr1

   End try

   begin catch

-- If any error occured during insertion or delition, you can roll back that transaction.

    rollback transaction tr1

  end catch


Scenario when transaction invlove insertion / updation / deletion on multiple SQL server, you need to
go for MSDTC.

Transaction (Process ID ) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

July 13
by Satalaj 13. July 2010 09:17


   You had written update or delete  statements without SQL hints like nolock, updlock, readpast etc.
This is very bad practice when you are dealing with multiple udates on rows or tables without locks.
Below scenario will explain how to use those hints to avoid deadlocks aswell as concurrency issues.

e.g.

UPDATE top 10 xyzTable with (updlock)

set x = 1

In this case if second user comes in to update xyzTable, he has to wait for first user operation to finish.

To let second user do update on next top 10 rows which are not locked by first user, you can

modify above query using readpast hint.

ReadPast hint will return row sets which are not locked by any transaction in that contxt.

UPDATE top 10 xyzTable with (updlock,readpast)
set x = 1


ReadPast and NoLock are two different hints
Readpast Only return rows which are not locked, while nolock returns
commited as well as non commited rows by other transaction this is also called as dirty reads.

SP_LOCK will written locks details.

You can avoid concurrency issues with your transaction using those lock hints.

For more information about locks in MS SQL you can visit

http://msdn.microsoft.com/en-us/library/aa213026(SQL.80).aspx


 

Tags:

sql server

Sql server cpu utilisation

April 23
by Satalaj 23. April 2010 08:50

 Days ago I wanted to know which query is taking lots of time and cpu utilisation for execution.
and I came to one blog from where I copied below query. It tells details of all query. You need to fire it on your SQL server to know more.

SELECT
    substring(text,qs.statement_start_offset/2
        ,(CASE   
            WHEN qs.statement_end_offset = -1 THEN len(convert(nvarchar(max), text)) * 2
            ELSE qs.statement_end_offset
        END - qs.statement_start_offset)/2)
    ,qs.plan_generation_num as recompiles
    ,qs.execution_count as execution_count
    ,qs.total_elapsed_time - qs.total_worker_time as total_wait_time
    ,qs.total_worker_time as cpu_time
    ,qs.total_logical_reads as reads
    ,qs.total_logical_writes as writes
FROM sys.dm_exec_query_stats qs
    CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
    LEFT JOIN sys.dm_exec_requests r
        ON qs.sql_handle = r.sql_handle
ORDER BY 3 DESC

Could not allocate space for object 'dbo.SORT temporary run storage

April 22
by Satalaj 22. April 2010 06:58
Msg 1105, Level 17, State 2, Line 1
Could not allocate space for object 'dbo.SORT temporary run storage:  162245511741440' in database 'tempdb' because the 'PRIMARY' filegroup is full. Create disk space by deleting unneeded files, dropping objects in the filegroup, adding additional files to the filegroup, or setting autogrowth on for existing files in the filegroup.


 It happens when you don't have enough space on your Drive where your tempDb database is lockated. You can avoid this by freeing up some space on that drive or
 Restart MS SQL server. After restarting of SQL server it will freeup temp db size.

Note:* Do not restrict auto grow size of .MDF or .LDF file.

Satalaj

MS SQL server Transaction commit rollback

April 19
by Satalaj 19. April 2010 12:21

To know your transaction is getting rolled back you need to generate an exception.

This will ensure that transcation is rolled back.

Trick is very simple, just write statement select 1/0 at the end bo transaction block as shown in below query.

select 1/0 will generate an exception and your transaction needs to be rolled back.

Begin transaction MyTransactionName

begin try

-- Delete some rows from sql server
-- insert deleted rows into other table

select 1/0  -- This will generate an exception and your transaction needs to be rolled back

commit transaction MyTransactionName

End try

begin catch

rollback transaction MyTransactionName

end catch

END

If you are using linked server, you need to enable MSDTC services (Microsoft distributed transaction co-ordinates).

database in recovery

April 12
by Satalaj 12. April 2010 09:52

If database suddenly went in to in recovery mode, due to less available space for transaction, you can fire below query and bring it live

USE master
GO
ALTER DATABASE pubs
SET QUOTED_IDENTIFIER ON, RECOVERY FULL, MULTI_USER

Simple Recovery
Simple Recovery allows the database to be recovered to the most recent backup.

Full Recovery
Full Recovery allows the database to be recovered to the point of failure.

Bulk-Logged Recovery
Bulk-Logged Recovery allows bulk-logged operations.


MSDTC

April 02
by Satalaj 2. April 2010 06:24

   If you are transferring data from one server to another server in transaction mode, you need to activate MSDTC (Microsoft distributed transaction cordinates).
 In my scenario, I used MS SQL server A and MS SQL server B.  SQL SERVER B is linked server of SERVER A.
 I have same credentisals for both Windows 2003 and same credentisal for SQL server. To avoid network plot blocking I disabled firewalls from both machines.
 Then from command prompt I started MSDTC
Net start MSDTC

 If you changed the password of your sql server, you need to restart your MSDTC service.

Tags:

sql server

Connection pooling

January 29
by Satalaj 29. January 2010 12:16
kick it on DotNetKicks.com


  Hi, my name is Satalaj. Here, I'm going to explain what is connection pool with solid proof.
Connection pool as name suggest its a pool of Established connections.
When we are closing the connection in finally or in try catch block that doesn't mean we are closing Established connection with your db server.
Closing connection means notifying the application about that connection is free for future request.

Here, I will tell my connection string to use min pool size = 1 and max pool size = 2.

As soon as pool gets initialized it will establish only one connection with SQL server db.  

You can use below query to test how many connections are established with your MS SQL db server. I established the connection with asptest DB.

select db_name(dbid) as DataBaseName , count(dbid) as

NoOFConnections , loginame as LoginName

from sys.sysprocesses where dbid > 0 and db_name(dbid) = 'apitest'

group by dbid,loginame

 

Here is screen shot of Established connection.

 

 I fired a command netstat -b from my command prompt and found that WindowsApplication1.VShost.exe with process ID 6140
Established one connection with MS SQL server IP 192.168.1.1.28 on port 1433.

My application is running and it has processed his task and I have closed the connection. However, you can see the connection is Established
and it will stay established, still the life of application. If I colse the application, I will not see any connection with my DB APITEST.

   TCP    smartmirror:2096                      192.168.1.28:ms-sql-s  ESTABLISHED     6140
  [WindowsApplication1.vshost.exe]

In my application I have one established connection. What will happen If I get 2 requests simultaniouesly?

Ans. The application will establish another connection to DB server as my Max pool size is 2 and that new connection will serve the request.

What if I get 3 or 4 simultaneous requests?

If there is no free  connection available in connection pool the application will throw an error

Timeout expired.  The timeout period elapsed prior to obtaining a connection from the pool.  This may have occurred because all pooled connections were in use and max pool size was reached.
 

What happens when I set Min pool size = 10 in my connection string ?
The application will establish 10 connection with DB.

You can see it by using query above and at client / server side, you can execute netstat -a command to know
which are the ports participates in this communication. You can see how my application is talking to sql server using ports ranging from 3979 to 3988.

 TCP    smartmirror:3979       192.168.1.28:ms-sql-s  ESTABLISHED
 TCP    smartmirror:3980       192.168.1.28:ms-sql-s  ESTABLISHED
 TCP    smartmirror:3981       192.168.1.28:ms-sql-s  ESTABLISHED
 TCP    smartmirror:3982       192.168.1.28:ms-sql-s  ESTABLISHED
 TCP    smartmirror:3983       192.168.1.28:ms-sql-s  ESTABLISHED
 TCP    smartmirror:3984       192.168.1.28:ms-sql-s  ESTABLISHED
 TCP    smartmirror:3985       192.168.1.28:ms-sql-s  ESTABLISHED
 TCP    smartmirror:3986       192.168.1.28:ms-sql-s  ESTABLISHED
 TCP    smartmirror:3987       192.168.1.28:ms-sql-s  ESTABLISHED
 TCP    smartmirror:3988       192.168.1.28:ms-sql-s  ESTABLISHED

It proves that I have 10 Established connections with SQL server and my application.
when I tell the connection object to close the connection, It only notifies the application about that "free connection", which can be used to serve next request.
When I tell connection object to open a connection I get a free connection form connection pool.


To know more about, how clients talks to the server refer http://revenmerchantservices.com/post/2010/01/25/client-server-communication.aspx

You can have multiple pools in applications. Of Course, there is a limit as there is limits on available ports ranging from 0 to 65535.

Its best practice not to have connection string opened in infinite time to serve the request. If you let the connection opened for infinite loop
your application will not be able to serve other requests. you can try it yourself by setting min and max pool size of your connection string.

By default min pool size is 0 and max pool size is 100.

In IIS, if you configure you application pool to use more than one worker process then for every process you will get new instance of established connections.

if you set connection lifetime = 30 property of connection string in web.config your minimum establised connections will never get dead 
till the lifetime of your application pool.

To know how many worker processes are currently servicing your application pool. you need to execute below command on that IIS server.
It is always good practivce to set life time of connection forcefully. This ensures that connection is backed into the pool after the use of it.

IISapp.VBS

It will return details of worker process along with its process ID and name of application pool under whom it resides.


Conclusion: I told connection object to open a connection with SQL DB server means I'm telling connection object to setup actual connection with DB server? 
                No, I'm telling the connection object to get the already established free connection from connection pool.

I hope you understood the location of Connection Pool and its importance.

 Other usefull information:

Clearing the Pool
ADO.NET 2.0 introduces two new methods to clear the pool: ClearAllPools and ClearPool. ClearAllPools clears the connection pools for a given provider, and ClearPool clears the connection pool that is associated with a specific connection. If there are connections in use at the time of the call, they are marked appropriately. When they are closed, they are discarded instead of being returned to the pool.

Transaction Support
Connections are drawn from the pool and assigned based on transaction context. Unless Enlist=false is specified in the connection string, the connection pool ensures that the connection is enlisted in the Current context. When a connection is closed and returned to the pool with an enlisted System.Transactions transaction, it is set aside in such a way that the next request for that connection pool with the same System.Transactions transaction will return the same connection. If there is no connection available for that transaction, the connection is automatically enlisted when it is opened.

When a connection is closed, it is released back into the pool and into the appropriate subdivision based on its transaction context. Therefore, you can close the connection without generating an error, even though a distributed transaction is still pending. This allows you to commit or abort the distributed transaction at a later time.

Controlling Connection Pooling with Connection String Keywords
The ConnectionString property of the SqlConnection object supports connection string key/value pairs that can be used to adjust the behavior of the connection pooling logic. For more information, see ConnectionString.

Pool Fragmentation
Pool fragmentation is a common problem in many Web applications where the application can create a large number of pools that are not freed until the process exits. This leaves a large number of connections open while consuming memory, resulting in poor performance.

Pool Fragmentation Due to Integrated Security
Connections are pooled according to the connection string plus the user identity. Therefore, if you use Basic authentication or Windows Authentication on the Web site and an integrated security login, you get one pool per user. Although this improves the performance of subsequent database requests for a single user, that user cannot take advantage of connections made by other users. It also results in at least one connection per user to the database server. This is a side effect of a particular Web application architecture that developers need to weigh against security and auditing requirements.

Pool Fragmentation Due to Many Databases
Many Internet service providers host several Web sites on a single server. They may use a single database to confirm a Forms authentication login and then open a connection to a specific database for that user or group of users. The connection to the authentication database gets pooled and used by everyone. However, there is a separate pool of connections to each database, thus increasing the number of connections to the server.

This is also a side effect of the application design. There is a relatively simple way to avoid this side effect, however, without compromising security when connecting to SQL Server. Instead of connecting to a separate database for each user or group, connect to the same database on the server and then execute the Transact-SQL USE statement to change to the desired database. The following code fragment demonstrates creating an initial connection to the master database and then switching to the desired database specified in the databaseName string variable.

 

Satalaj.

The shrink operation is not duplicated on the mirror database when you use database mirroring in SQL Server 2005

January 18
by Satalaj 18. January 2010 05:07

  If you are trying to shrink database which has mirrored copy on other server, shrink operation will not get duplicated on mirrored.
You can resolve this issue by creating following script on your principal server using master database.

use master
       go
       if object_id ('sp_shrink_mirrored_database', 'P') is not null
         drop proc sp_shrink_mirrored_database
       go
       create procedure sp_shrink_mirrored_database @dbname sysname, @target_percent int = null
       as
       begin
         declare @filename sysname
         declare @filesize int
         declare @sql nvarchar(4000)
        
         if @target_percent is null
           dbcc shrinkdatabase (@dbname)
         else
           dbcc shrinkdatabase (@dbname, @target_percent)
         declare c cursor for
         select [name], [size] from sys.master_files where type=0 and database_id = db_id (@dbname)
         open c
         fetch next from c into @filename, @filesize
         while @@fetch_status=0
         begin
           set @filesize=(@filesize+1)*8
           set @sql='alter database [' + @dbname + '] modify file ( name='
             + @filename + ', size=' + cast(@filesize as nvarchar) + 'kb )'
           execute sp_executesql @sql
           fetch next from c into @filename, @filesize
         end
         close c
         deallocate c
       end
       go

Now, you can shrink principal database using above stored procedure
e.g.
EXEC sp_shrink_mirrored_database my_db_name', 10
or

EXEC sp_shrink_mirrored_database 'my_db_name'

OLE DB provider "SQLNCLI" for linked server returned message "Cannot start more transactions on this session.".

January 16
by Satalaj 16. January 2010 06:45

 OLE DB provider "SQLNCLI" for linked server  returned message "Cannot start more transactions on this session."


Quick Fix: 
add below statement into your stored procedure who updates or transfer data on multiple server in transaction mode


SET XACT_ABORT ON

Here XACT_ABORT mode needs to be turned on

Tags:

sql server

About Satalaj

My name is Satalaj. I'm 2010 asp.net MVP. I write technical stuff here. www.satalaj.com

Ads by Lake Quincy Media

The best inline translator

Live lookup to see what asp.net developers are searching