473,485 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Sqlconnection object in website

Hi Guys I have a problem here.
I want to improve the performance for a website. When I looked into the
system, I have found that the system made the "SqlConnection Object" static.
That mean only one SqlConnection object will be used.
Can someone tell me is that a correct way to do? Does that affect the system
performance?

Cheers
Victor
Jan 15 '07 #1
4 3874
Here I have a short example how the code looks like
Class A
{
private static SqlConnection conn;

public static SqlConnection getDBAConnection()
{
string _connStr;
_connStr =
ConfigurationSettings.AppSettings["ConnectionStringA"];
conn = new SqlConnection(_connStr);
conn.Open();
return conn;
}
public static SqlConnection getDBBConnection()
{
string _connStr;
_connStr =
ConfigurationSettings.AppSettings["ConnectionStringB"];
conn = new SqlConnection(_connStr);
conn.Open();
return conn;
}

public static SqlConnection getDBCConnection()
{
string _connStr;
_connStr =
ConfigurationSettings.AppSettings["ConnectionStringC"];
conn = new SqlConnection(_connStr);
conn.Open();
return conn;
}

public static SqlConnection getDBDConnection()
{
string _connStr;
_connStr =
ConfigurationSettings.AppSettings["ConnectionStringD"];
conn = new SqlConnection(_connStr);
conn.Open();
return conn;
}

public static void CloseConnection()
{
if (conn!=null)
{
if (conn.State == ConnectionState.Open) conn.Close();
}
}
}

Is that a good way to do?

Cheers
Victor
"Victor" <VictorDotShiAtnurseryroomDotComwrote in message
news:es**************@TK2MSFTNGP02.phx.gbl...
Hi Guys I have a problem here.
I want to improve the performance for a website. When I looked into the
system, I have found that the system made the "SqlConnection Object"
static. That mean only one SqlConnection object will be used.
Can someone tell me is that a correct way to do? Does that affect the
system performance?

Cheers
Victor

Jan 15 '07 #2
yes and no. there is nothing wrong with static methods to return a new
connection, but the code assume a close will be called before another
open. no locks are done, so its not thread safe.

you site most likely leaks connections especially under load.

you should remove the private static and change close to be passed a
connection. better yet use a using:

using A.getDBAConnection()
{
// my code here - no close required
}

-- bruce (sqlwork.com)

Victor wrote:
Here I have a short example how the code looks like
Class A
{
private static SqlConnection conn;

public static SqlConnection getDBAConnection()
{
string _connStr;
_connStr =
ConfigurationSettings.AppSettings["ConnectionStringA"];
conn = new SqlConnection(_connStr);
conn.Open();
return conn;
}
public static SqlConnection getDBBConnection()
{
string _connStr;
_connStr =
ConfigurationSettings.AppSettings["ConnectionStringB"];
conn = new SqlConnection(_connStr);
conn.Open();
return conn;
}

public static SqlConnection getDBCConnection()
{
string _connStr;
_connStr =
ConfigurationSettings.AppSettings["ConnectionStringC"];
conn = new SqlConnection(_connStr);
conn.Open();
return conn;
}

public static SqlConnection getDBDConnection()
{
string _connStr;
_connStr =
ConfigurationSettings.AppSettings["ConnectionStringD"];
conn = new SqlConnection(_connStr);
conn.Open();
return conn;
}

public static void CloseConnection()
{
if (conn!=null)
{
if (conn.State == ConnectionState.Open) conn.Close();
}
}
}

Is that a good way to do?

Cheers
Victor
"Victor" <VictorDotShiAtnurseryroomDotComwrote in message
news:es**************@TK2MSFTNGP02.phx.gbl...
>Hi Guys I have a problem here.
I want to improve the performance for a website. When I looked into the
system, I have found that the system made the "SqlConnection Object"
static. That mean only one SqlConnection object will be used.
Can someone tell me is that a correct way to do? Does that affect the
system performance?

Cheers
Victor

Jan 15 '07 #3
Your best bet for getting good practices code is to find the

Data Access Application Block Version 2.0

this one is 'sql server only'.

...

Your best bet is to fine tune the connection pool.
Using a connection pool, and the DAAB .. will start you on the right path.

the DAAB has a "open late, close early" mentality, which is a good one for
the web.

...

The other place you need to watch out for . is.... NOT closing datareaders.
You need to ~~~~ always close datareaders.


"Victor" <VictorDotShiAtnurseryroomDotComwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
Here I have a short example how the code looks like
Class A
{
private static SqlConnection conn;

public static SqlConnection getDBAConnection()
{
string _connStr;
_connStr =
ConfigurationSettings.AppSettings["ConnectionStringA"];
conn = new SqlConnection(_connStr);
conn.Open();
return conn;
}
public static SqlConnection getDBBConnection()
{
string _connStr;
_connStr =
ConfigurationSettings.AppSettings["ConnectionStringB"];
conn = new SqlConnection(_connStr);
conn.Open();
return conn;
d }
>
public static SqlConnection getDBCConnection()
{
string _connStr;
_connStr =
ConfigurationSettings.AppSettings["ConnectionStringC"];
conn = new SqlConnection(_connStr);
conn.Open();
return conn;
}

public static SqlConnection getDBDConnection()
{
string _connStr;
_connStr =
ConfigurationSettings.AppSettings["ConnectionStringD"];
conn = new SqlConnection(_connStr);
conn.Open();
return conn;
}

public static void CloseConnection()
{
if (conn!=null)
{
if (conn.State == ConnectionState.Open) conn.Close();
}
}
}

Is that a good way to do?

Cheers
Victor
"Victor" <VictorDotShiAtnurseryroomDotComwrote in message
news:es**************@TK2MSFTNGP02.phx.gbl...
Hi Guys I have a problem here.
I want to improve the performance for a website. When I looked into the
system, I have found that the system made the "SqlConnection Object"
static. That mean only one SqlConnection object will be used.
Can someone tell me is that a correct way to do? Does that affect the
system performance?

Cheers
Victor


Jan 15 '07 #4
hi
Here is my another question. So is that correct to set the connection
object to be static?
Cheers
Victor

"sloan" <sl***@ipass.netдÈëÏûÏ¢
news:Oh*************@TK2MSFTNGP04.phx.gbl...
Your best bet for getting good practices code is to find the

Data Access Application Block Version 2.0

this one is 'sql server only'.

..

Your best bet is to fine tune the connection pool.
Using a connection pool, and the DAAB .. will start you on the right path.

the DAAB has a "open late, close early" mentality, which is a good one for
the web.

..

The other place you need to watch out for . is.... NOT closing
datareaders.
You need to ~~~~ always close datareaders.


"Victor" <VictorDotShiAtnurseryroomDotComwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>Here I have a short example how the code looks like
Class A
{
private static SqlConnection conn;

public static SqlConnection getDBAConnection()
{
string _connStr;
_connStr =
ConfigurationSettings.AppSettings["ConnectionStringA"];
conn = new SqlConnection(_connStr);
conn.Open();
return conn;
}
public static SqlConnection getDBBConnection()
{
string _connStr;
_connStr =
ConfigurationSettings.AppSettings["ConnectionStringB"];
conn = new SqlConnection(_connStr);
conn.Open();
return conn;
d }
>>
public static SqlConnection getDBCConnection()
{
string _connStr;
_connStr =
ConfigurationSettings.AppSettings["ConnectionStringC"];
conn = new SqlConnection(_connStr);
conn.Open();
return conn;
}

public static SqlConnection getDBDConnection()
{
string _connStr;
_connStr =
ConfigurationSettings.AppSettings["ConnectionStringD"];
conn = new SqlConnection(_connStr);
conn.Open();
return conn;
}

public static void CloseConnection()
{
if (conn!=null)
{
if (conn.State == ConnectionState.Open) conn.Close();
}
}
}

Is that a good way to do?

Cheers
Victor
"Victor" <VictorDotShiAtnurseryroomDotComwrote in message
news:es**************@TK2MSFTNGP02.phx.gbl...
Hi Guys I have a problem here.
I want to improve the performance for a website. When I looked into the
system, I have found that the system made the "SqlConnection Object"
static. That mean only one SqlConnection object will be used.
Can someone tell me is that a correct way to do? Does that affect the
system performance?

Cheers
Victor



Jan 15 '07 #5

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

Similar topics

1
558
by: Donnie Darko | last post by:
I'm trying to understand SqlConnection(), SqlCommand() For example. I use SqlConnection() as a parameter when I create a new SqlCommand. Then I open SqlConnection(), then I execute the...
17
9342
by: LP | last post by:
Hello, Here's the scenario: Object A opens a Sql Db connection to execute number of SqlCommands. Then it needs to pass this connection to a constructor of object B which in turn executes more...
1
2375
by: Mark | last post by:
I have an SqlConnection object defined as a member of main form. I also have a thread that creates an SqlDataReader but uses the same SqlConnection object. The SqlDataReader simple loops round for...
11
2848
by: Bob | last post by:
In our new .NET web applications, we try to limit the use of SqlConnection to just one instance per page, even if there are multiple accesses to various queries. The thinking behind is that this...
2
1188
by: MrMike | last post by:
I have a web.config file containing the SQLConnection String to a SQL Database. Before I implemented this SQLConnection String into web.config, I had individual SQLConnection objects in each...
4
2043
by: Steve Richter | last post by:
I really miss c++ .... I have an SqlConnection object within my Web.UI.Page object. The thinking is that the connection to the database server is opened once when the page starts to do its...
2
5439
by: axel22 | last post by:
Ok here it goes. 1. I've created a new web service project in F:\c#\WebSites\ServisBaze. 2. I've published the web service with IIS in a virtual directory who's actual path is:...
0
7116
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,...
0
7161
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...
1
6825
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7275
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3058
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3063
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1376
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 ...
1
595
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
247
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...

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.