473,242 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,242 software developers and data experts.

Shared DB connection + classes

Hi Group,

I've got a small web interface, basically a link index with
different categories.I use different classes for HTML rendering,
category managing, link managing etcetera.

Now, most of these classes need DB access, but it's best to limit
the DB connections per requested page (no?). So I've got two ideas
here:

- I create a DB connection $maindbconn (object of class
DBConnection) in the main script, and make sure every class has its
own $dbconn class variable. In the class constructors, the main DB
connection is passed by reference to the class, so every class has
access to that one db connection.

- I create a DB connection $maindbconn in the main script, and in
every class function that needs it, I use "global $maindbconn;".

Now, which approach seems best to you experts out there? And why?

Thanks in advance,
Bobo
Jul 17 '05 #1
4 1807
.oO(bobo)
Now, most of these classes need DB access, but it's best to limit
the DB connections per requested page (no?).
Yep. In most cases you only need one connection per page.
So I've got two ideas
here:

- I create a DB connection $maindbconn (object of class
DBConnection) in the main script, and make sure every class has its
own $dbconn class variable. In the class constructors, the main DB
connection is passed by reference to the class, so every class has
access to that one db connection.
Another way would be to implement the DB class with the singleton
pattern. The DB object will then be globally available without having to
pass it by reference to all other objects.
- I create a DB connection $maindbconn in the main script, and in
every class function that needs it, I use "global $maindbconn;".


With OOP using such global variables should not be necessary anymore.
OOP allows cleaner ways of sharing data and instances.

Micha
Jul 17 '05 #2
Michael Fesser <ne*****@gmx.net> wrote:
Another way would be to implement the DB class with the
singleton pattern. The DB object will then be globally available
without having to pass it by reference to all other objects.


Hmmm, sounds very nice. Now let me read up on that singleton
pattern. Design patterns is one of those things that always made me
think "sounds nice" but I never had the courage to start reading up
on them. Guess this is a nice opportunity.

Thanks,
bobo
Jul 17 '05 #3
wald <arnout.standaert@n*o_s-p%a|m.cit.kuleuven.ac.be> wrote:
Thanks,
bobo


Euh, yes it's me, newsreader screwed around with profiles :-)
Jul 17 '05 #4

"bobo" <bo**@someaddress.com> wrote in message
news:Xn**********************@134.58.127.12...
Hi Group,

I've got a small web interface, basically a link index with
different categories.I use different classes for HTML rendering,
category managing, link managing etcetera.

Now, most of these classes need DB access, but it's best to limit
the DB connections per requested page (no?). So I've got two ideas
here:


Pass the db database connect to the object method when it needs it. This is
the most flexible approach, since it opens up the possibility of the objects
retrieving data from different databases. It also makes more sense
linguistically. The database is not a component of your objects. Your
objects "make use of" or "operate on" the database. By passing the database
object as a parameter you indicate this clearly. Consider the following
scenario:

We have object $A of class A and object $B of class B. We also have $DB--the
database object, which both classes have access to by through a reference or
a singleton. We are presented with the following fragment:

$A->Panda();
$B->Dingo();
$A->Panda();

Panda() reads from $DB. On the first call it gets 5 rows; the second times
it gets 0. But what happened? Why is $DB behaving differently all of a
sudden? The answer is obvious, of course: the dingo has deleted the row. But
we don't see that in the code. All we see is a method invocation on $B,
which is understood to be changing only the state of $B. $B is basically
causing an action at a distance--cool in physics, not good for code clarity.
Jul 17 '05 #5

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

Similar topics

6
by: Paul | last post by:
Hi. Just trying to find out the best approach as I beleive it might give me problems later on down the road. I have an ASP.NET application which references a shared database class which...
2
by: Don | last post by:
I'm asking this for a friend of mine, so forgive me if I'm getting some of the terminology wrong (I don't have any experience with ASP.NET). I've got an ASP application that has some classes that...
10
by: Daniel Fernandes | last post by:
Hi there Let's say due to several reasons I have an ASP.Net application that has a database access class that stores a database connection in a shared member : public class dataaccess ...
2
by: tshad | last post by:
I am trying to set up some shared functions in my Global.asax file and can't seem to get it to work. In my Global.asax: Public Class myUtils inherits System.Web.HttpApplication public...
11
by: tshad | last post by:
I am setting up some of my functions in a class called MyFunctions. I am not clear as to the best time to set a function as Shared and when not to. For example, I have the following bit...
14
by: Joe Fallon | last post by:
I am trying to build a Data Access Layer for a SQL Server back end. My class has a number of methods in it. The constructor takes a connection string. I currently have to instantiate an object...
10
by: Stevie_mac | last post by:
Hi all, quick question - I need a bit of clarity. If I have a public class with only Public Shared functions in it, in an APS.NET web app (for common procedures) is it safe (since multiple web...
6
by: Daniel Fernandes | last post by:
Hi there Is there any difference I need to be aware when I create a class with only shared members & procedures when compared to a module (which is a shared class) ? I am asking this because...
2
by: Random | last post by:
Here's a design question I'm curious to know if anyone here has wrestled with before... I'm writing my data access methods in classes in the App_Code directory. I know that I can easily...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.