473,386 Members | 2,129 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,386 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 1810
.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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.