Solved

Details on Database Connections from ColdFusion

Posted on 2003-11-26
4
261 Views
Last Modified: 2013-12-24
Can someone confirm or deny the following?

---------------------------
In the web environment, there are no permanent connections between client
and server. The browser is not able to open a direct connection to the
database, so the role of the application server such as CFMX is to manage
the DB connections on behalf of clients. Basically, the app server
virtualizes database connections by managing a pool of pre-opened
connections and serving it up to applications each time a query is made.
This is very efficient because opening database connections on demand each
time an application makes a query is very costly in system resources. You
can see the connection pooling parameters in CF Admin > Data sources under
Advanced Settings.

Now, the database no longer has any knowledge or control of how many clients
are connecting to it because a small pool of connections can be time-shared
between a large number of users. That's why you can buy a 5-user licence and
have the database serve thousands of concurrent users. Of course the
database vendors can't allow that happen, so most of them now require that
if you serve internet (or intranet) users from the database, you will need
to purchase an additional "Internet licence" which maybe triples the price.

Preamble over, back to the original question. If you insert a new record and
retrieve its identity using @@IDENTITY within the same CFQUERY block, you
will be guaranteed of using the same database connection because a
connection is allocated per query. Because $$IDENTITY gives you the last
identity within the connection, that's what you want. If you put the insert
and select @@identity in different CFQUERY blocks, it is possible that each
may use a different database connection and you'll get wrong results.
---------------------------

And give me some more info on connections, here is how I always *thought* it worked
CF creates a connection to the DB via ODBC, this is one connection and will be seen as one connection to the db, no matter how many clients connect through it.... I could be wrong as this part has always been a bit unclear to me.

PS. I am aware of "CFMX is to manage the DB connections on behalf of clients"
0
Comment
Question by:Tacobell777
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
Dexstar earned 250 total points
ID: 9828435
@Tacobell777:

> And give me some more info on connections, here is how I always *thought* it worked
> CF creates a connection to the DB via ODBC, this is one connection and will
> be seen as one connection to the db, no matter how many clients connect through
> it.... I could be wrong as this part has always been a bit unclear to me.

Yes, that passage you posted is 100% true.
Here is how it works.  CF creates not just one connection, but a "pool" of connections.  When a client requests a web page that needs a database connection, it gets one from the pool, makes the request, and then puts it back into the pool.  That avoids constant connection/disconnecting to the database.

Apparently, it gets the connection and puts it back in the pool for each CFQUERY block.  That's why if you get the @@IDENTITY in a different CFQUERY block, then it grab a different connection out of the pool, and get the wrong @@IDENTITY value.

Hope That Helps,
Dex*
0
 
LVL 17

Author Comment

by:Tacobell777
ID: 9828465
Hi Dex,

Your saying "CF creates not just one connection, but a "pool" of connections" how can my passage be true then?
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9836670
That passage states "Basically, the app server virtualizes database connections by managing a pool of pre-opened connections and serving it up to applications each time a query is made."  which is just a more complicated way of saying what I said.

Maybe I don't understand.  What part of the passage conflicts or what part is unclear?

Dex*
0
 
LVL 4

Expert Comment

by:procept
ID: 9841767
Hi,

> If you put the insert
> and select @@identity in different CFQUERY blocks, it is possible that each
> may use a different database connection and you'll get wrong results.

That's not hte reason for an incorrect result... if you use separate queries, you may run into race conditions with 2 or more users:

User1 Query1: insert
User2 Query1: insert
User1: select @@identity ==> wrong result

By putting both in the same query, you avoid the race conditions, because the whole block (insert and identity-select) is executed by the database at one time. The DB will use features like e.g. automatic record locking to make sure the SQL batch retrieves the correct result.


> Apparently, it gets the connection and puts it back in the pool for each CFQUERY block.  

AFAIK CF does that for each request, not for each query within the request.... not sure about htat, though.

HTH,

Chris


0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Annoying "thing" blocks my view 4 130
spamming  on Hosted svrs? 6 103
Windows Tool to Build Android and iOS App 3 112
http to https 3 76
This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
When setting up new project requests for our site, one of the most powerful tools our team has available to use is Axure (http://www.axure.com/). It’s a tool for creating software and web prototypes that can function and interact as if it were the a…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…

713 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