Link to home
Start Free TrialLog in
Avatar of Tacobell777
Tacobell777

asked on

Details on Database Connections from ColdFusion

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"
ASKER CERTIFIED SOLUTION
Avatar of Dexstar
Dexstar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tacobell777
Tacobell777

ASKER

Hi Dex,

Your saying "CF creates not just one connection, but a "pool" of connections" how can my passage be true then?
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*
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