Solved

Multiple Database Connections vs Single Database Connection

Posted on 2004-04-30
9
930 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

13 Experts available now in Live!

Get 1:1 Help Now