Solved

Multiple Database Connections vs Single Database Connection

Posted on 2004-04-30
9
931 Views
Last Modified: 2007-12-19
Hi All,

I have an Oracle database which I want to connect to. I have an app running on my IIS server which the users will connect to.

The normal scenario is to when a request come to the web server it opens a connection to the database and then closes it when it is finished. This means for every client connection to the web server there is one to the server ... so if we have 1000 web connections we have 1000 database connections. This eats up too many resources.

I know that you can use some sort of session information to keep one database connection open and all the web requests use the same database connection.

I would like to get some information for it. And if anybody can give me a working ORACLE and C# example, you get the 500 points :)

Thanks
Peter
0
Comment
Question by:Pete2003
9 Comments
 
LVL 21

Accepted Solution

by:
tovvenki earned 500 total points
ID: 10958044
Hi,

try this when you open the connection for the first time open it and save it in a session variable

OracleClient.OracleConnection conn = new OracleClient.OracleConnection ();
conn.ConnectionString = "Data Source=" + "<oracle data source name>;Integrated Security=yes";
    try
    {
        conn.Open();
        // Insert code to process data.
    }
        catch (Exception ex)
    {
        //("Failed to connect to data source");
    }

    Session["DbCon"] = conn;
-------------------------------------------------

now whereever you need to use the connection in the session use the following code

OracleClient.OracleConnection myconn  = (OracleClient.OracleConnection)Session["DbCon"];


hope that this helps you.

Regards,
venki

0
 

Author Comment

by:Pete2003
ID: 10958142
Thanks for the quick response ...

Ok maybe let me expand on what my problem a bit ...

If three users connect to the web server they each have a unique IIS connection session. I however want these separate application instances to use the SINGLE oracle connection.

I have seen this done ... ie: you have 10 people connected to the IIS server and using the database .. bu tthere is only one connection to the database server open.

0
 
LVL 6

Expert Comment

by:jkna_gunn
ID: 10958180
maybe you would have to store the database object in the application array not the session array
0
 
LVL 21

Expert Comment

by:tovvenki
ID: 10958225
then try with the Application object like

Application("DbCon") = conn;

myConn = (OracleClient.OracleConnection)Application["DbCon"];

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:DaniPro
ID: 10958226
In Orcale .NET Provider there are connections pool for an ADO.NET application.
When you open a new connection, a connection pool's created with an alghoritm based on connection string; when you open a new connection, if the connection string is not the same of the previous connection, a new connection pool will be created.
A connection pool is delete when the active process end.
You don't need to store the connection in the session or application object, the connections pool are managed automatically from the server.

You can read this link for more detail:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconconnectionpoolingfornetdataproviderfororacle.asp
0
 

Author Comment

by:Pete2003
ID: 10958304
The application object does not work ...

OracleConnection conn = new OracleConnection ();
conn.ConnectionString = "User Id=user;Password=pass;Data Source=oradbase;Integrated Security=yes";
try
{
     conn.Open();
     Application("DbCon") = conn;
}
catch (Exception ex)
{
      //("Failed to connect to data source");
}

The error is

WebForm1.aspx.cs(25): 'System.Web.UI.Page.Application' denotes a 'property' where a 'method' was expected


shoudl I be including some extra library ?  it works with the session but not application ...


0
 

Author Comment

by:Pete2003
ID: 10958319
OOps sorry my mistake ( instead of [
0
 
LVL 21

Expert Comment

by:tovvenki
ID: 10958342
hi,
it should be like
Application["DbCon"] = conn;
and not
Application("DbCon") = conn;

0
 

Author Comment

by:Pete2003
ID: 10958651
One final question ... the Application variable works in a web application ... how would this work if I wanted to put this into a library ?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now