<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>sql database hang &#8211; ASP.NET Hosting Reviews and Guides</title>
	<atom:link href="https://topreviewhostingasp.net/tag/sql-database-hang/feed/" rel="self" type="application/rss+xml" />
	<link>https://topreviewhostingasp.net</link>
	<description>Help you to find best ASP.NET Core Hosting</description>
	<lastBuildDate>Thu, 16 Feb 2023 07:10:27 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://topreviewhostingasp.net/wp-content/uploads/2017/01/cropped-trhaico-32x32.png</url>
	<title>sql database hang &#8211; ASP.NET Hosting Reviews and Guides</title>
	<link>https://topreviewhostingasp.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Reduce Connection Pools Hitting Multiple Databases</title>
		<link>https://topreviewhostingasp.net/how-to-reduce-connection-pools-hitting-multiple-databases/</link>
					<comments>https://topreviewhostingasp.net/how-to-reduce-connection-pools-hitting-multiple-databases/#respond</comments>
		
		<dc:creator><![CDATA[Jacques Hunt]]></dc:creator>
		<pubDate>Thu, 16 Feb 2023 07:09:12 +0000</pubDate>
				<category><![CDATA[Hosting Tips]]></category>
		<category><![CDATA[database issue]]></category>
		<category><![CDATA[fix connection pool issue]]></category>
		<category><![CDATA[sql database]]></category>
		<category><![CDATA[sql database hang]]></category>
		<category><![CDATA[sql server]]></category>
		<category><![CDATA[sql tips]]></category>
		<category><![CDATA[sql tutorial]]></category>
		<guid isPermaLink="false">https://topreviewhostingasp.net/?p=3434</guid>

					<description><![CDATA[Microsoft’s connection pooling built in to the .Net Framework greatly improves performance by allowing hundreds of database calls to share the same pool of connections used to connect to the database. The .Net Framework creates a new pool of connections for each unique connection string. This works fantastic when your web application connects to a [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>Microsoft’s connection pooling built in to the .Net Framework greatly improves performance by allowing hundreds of database calls to share the same pool of connections used to connect to the database. The .Net Framework creates a new pool of connections for each unique connection string. This works fantastic when your web application connects to a single database and all your connections use the same connection string. It even works well if you have two or three different connection strings, but for each unique connection string value, the .Net Framework creates a new connection pool. If your web site has dozens of different databases to connect to, you may find your application creates dozens of connection pools which begins to consume a lot of resources on both the web server and SQL Server.</p>



<p>There is a fairly easy way to eliminate this problem, and that is to use the ChangeDatabase method on the Connection object.</p>



<p>Our web application was connecting to one of fifty different databases that all reside on the same instance of SQL Server. Each of our clients has their own database. But our application was creating fifty different connection pools. We were able to have all the clients share a single pool of connections by first connecting to a neutral database, then redirecting the connection to the desired database.</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://www.asphostportal.com" target="_blank" rel="noreferrer noopener"><img fetchpriority="high" decoding="async" width="300" height="271" class="wp-image-2584 aligncenter" src="https://topreviewhostingasp.net/wp-content/uploads/2018/11/ahp-banner-aspnet-01.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2018/11/ahp-banner-aspnet-01.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2018/11/ahp-banner-aspnet-01-50x45.png 50w" sizes="(max-width: 300px) 100vw, 300px" /></a></figure></div>


<p>We created a new database on the server, called Central, then we had all the connections to the database first connect to Central as the Initial Catalog. The next step was to call ChangeDatabase to switch to the desired database. This technique did not create a new connection to SQL Server and did not create a new connection pool on the .Net client. Microsoft documentation mentions this technique here: <a href="https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql-server-connection-pooling#pool-fragmentation-due-to-many-databases" target="_blank" rel="noreferrer noopener">https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql-server-connection-pooling#pool-fragmentation-due-to-many-databases</a> , but their example code does not show the ChangeDatabase method, which is more efficient than their example.</p>



<p>In a nutshell, you can do this:</p>



<pre class="wp-block-code"><code>connection.Open();
connection.ChangeDatabase(“Client1”);</code></pre>



<p>I created a simple Windows Form App to test the idea.  The test app runs a query to return the name of the current database it is in.  Also, on the SQL Server I run a query to see all the connections that exist.  Doing this I was able to prove that using ChangeDatabase did not create a new connection to SQL Server and my .Net SQL Connection Object was pointing at the correct desired database.  Sample code, query, and output is below:</p>



<p>Sample C# .Net Forms App:</p>



<pre class="wp-block-code"><code>using System;
using System.Data.SqlClient;
using System.Windows.Forms;
 
namespace SQLConnectionTest
{
    public partial class Form1 : Form
    {
        //https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/sql-server-connection-pooling#pool-fragmentation-due-to-many-databases
        private void button1_Click(object sender, EventArgs e)
        {
            using (SqlConnection connection = new SqlConnection(@"Server=.\;Database=master;Trusted_Connection = yes"))
            {
                connection.Open();
                RunSQL(connection, "select DB_NAME()");
            }
            using (SqlConnection connection = new SqlConnection(@"Server=.\;Database=model;Trusted_Connection = yes"))
            {
                connection.Open();
                RunSQL(connection, "select DB_NAME()");
            }
            using (SqlConnection connection = new SqlConnection(@"Server=.\;Database=msdb;Trusted_Connection = yes"))
            {
                connection.Open();
                RunSQL(connection, "select DB_NAME()");
            }
            using (SqlConnection connection = new SqlConnection(@"Server=.\;Database=master;Trusted_Connection = yes"))
            {
                connection.Open();
                RunSQL(connection, "select DB_NAME()");
            }
            using (SqlConnection connection = new SqlConnection(@"Server=.\;Database=master;Trusted_Connection = yes"))
            { 
                connection.Open();
                connection.ChangeDatabase("TempDb");
                RunSQL(connection, "select DB_NAME()");
            }
 
            using (SqlConnection connection = new SqlConnection(@"Server=.\;Database=master;Trusted_Connection = yes"))
            {
                connection.Open();
                RunSQL(connection, "select DB_NAME()");
            }
            using (SqlConnection connection = new SqlConnection(@"Server=.\;Database=master;Trusted_Connection = yes"))
            {
                connection.Open();
                connection.ChangeDatabase("TempDb");
                RunSQL(connection, "select DB_NAME()");
                connection.ChangeDatabase("model");
                RunSQL(connection, "select DB_NAME()");
            }
        }
 
        public static void RunSQL(SqlConnection connection, string queryString)
        {
            SqlCommand command = new SqlCommand(queryString, connection);
            try
            {
                SqlDataReader reader = command.ExecuteReader(); 
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        Console.WriteLine("\t{0}", reader[0]);
                    }
                    reader.Close();
                }
            }
            catch (Exception ex){Console.WriteLine(ex.Message);}
        }
        public Form1()
        {
            InitializeComponent();
        }
 
 
    }
}</code></pre>



<p>Output from Running the App written to console:</p>



<pre class="wp-block-code"><code>master
model
msdb
master
tempdb
master
tempdb
model</code></pre>



<p>Query to see connections on SQL Server:</p>



<pre class="wp-block-code"><code>CREATE TABLE #TmpLog
(SPID int, Status VARCHAR(150), Login varchar(100), HostName VARCHAR(150), BlkBy varchar(30), DBName varchar(60), Command varchar(500),
CPUTime int, DiskIO int, LastBatch varchar(20), ProgramName varchar(200), SPID2 int, requestid int)
INSERT INTO #TmpLog
EXEC sp_who2
SELECT SPID, Status, Login, HostName, BlkBy, DBName, Command, ProgramName
FROM #TmpLog
where STATUS not in (‘BACKGROUND’) and Command not in (‘TASK MANAGER’) and ProgramName like ‘.net%’
DROP TABLE #TmpLog
go</code></pre>



<p>Results of running query on SQL Server:</p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="143" class="wp-image-3435" src="https://topreviewhostingasp.net/wp-content/uploads/2023/02/sql-query-1024x143.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2023/02/sql-query-1024x143.png 1024w, https://topreviewhostingasp.net/wp-content/uploads/2023/02/sql-query-300x42.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2023/02/sql-query-768x107.png 768w, https://topreviewhostingasp.net/wp-content/uploads/2023/02/sql-query-1536x214.png 1536w, https://topreviewhostingasp.net/wp-content/uploads/2023/02/sql-query-50x7.png 50w, https://topreviewhostingasp.net/wp-content/uploads/2023/02/sql-query.png 2013w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://topreviewhostingasp.net/how-to-reduce-connection-pools-hitting-multiple-databases/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
