ColdFusioning.com
Pronunciation \kold-fy-zhn-ing\ Noun: The actions of one that writes ColdFusion.

Please check out my new site: http://ja.mesbrown.com/

How Unique is a UUID? Should we use them instead of auto-incrementing?

We had a discussion today about the uniqueness of UUIDs (or GUID). In case you didn't know, according to Wikipedia, a UUID is:

"an identifier standard used in software construction, standardized by the Open Software Foundation (OSF) as part of the Distributed Computing Environment (DCE)."

The uniqueness of a UUID is 1 2^128. Now this doesn't really mean much to most people so the part I found particularly interesting is the uniqueness in terms we can try to understand. From Stackoverflow:

"consider the observable universe, which contains about 5×10^22 stars; every star could then have 6.8×10^15 universally unique GUIDs."

Wow.

Here's another way to write the probability:

1 in 340,000,000,000,000,000,000,000,000,000,000,000,000

or

"In other words, only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%. The probability of one duplicate would be about 50% if every person on earth owns 600 million UUIDs."

Stop and think about that for a second.

This raises the question. Given the fact they basically are unique, should we use them for a primary key instead of auto-incrementing in a database? There are good reasons to use either as a primary key.

Which do you use and why?

DELETED

I recently made an update to a table and start receiving a weird error:

view plain print about
1[Macromedia][Oracle JDBC Driver][Oracle]ORA-01722: invalid number

even though the SQL hadn't changed and I had only added a column to the database. Well, it turns out that cfqueryparam and ColdFusion do some weird caching that was causing the error. If changed the query (i.e. change the order of the WHERE statements (which didn't change the results)), the error went away.

Truly a weird error.

Using ColdFusion and Oracle To Get A Table's Primary Key

I recently ran into a situation where I needed to get the primary key of a table, knowing only the table name. Using the following code, you can pass it a table name, and it will return the primary key:

view plain print about
1<cfcomponent>
2    <cffunction name="getPrimaryKeyName" output="false">
3        <cfargument name="Table_Name">
4            
5            <cfquery name="getPrimaryKeyName" datasource="#request.maindsn#">
6            SELECT cols.table_name, cols.column_name, cols.position, cons.status, cons.owner
7            FROM all_constraints cons, all_cons_columns cols
8            WHERE cols.table_name = <cfqueryparam cfsqltype="cf_sql_text" value="#UCase(arguments.Table_Name)#">
9            AND cons.constraint_type = 'P'
10            AND cons.constraint_name = cols.constraint_name
11            AND cons.owner = cols.owner
12            ORDER BY cols.table_name, cols.position
13            </cfquery>
14            
15        <cfreturn getPrimaryKeyName.column_name>
16    </cffunction>
17</cfcomponent>

Then you only need to call the function to get the primary key name:
view plain print about
1<cfinvoke method = "getPrimaryKeyName" returnVariable = "request.PrimaryKeyName">
2    <cfinvokeargument name="Table_Name" value="#arguments.Table_Name#">
3</cfinvoke>

Maximum Open Cursors Exceeded on Oracle with ColdFusion

After beginning a new project for a client who required I use Oracle (I'm using version 11.1.0.6.0) for the database backend, I started running into the following error:

view plain print about
1Error Executing Database Query.
2[Macromedia][Oracle JDBC Driver][Oracle]ORA-01000: maximum open cursors exceeded

On solution is to raise the number of cursors in the Oracle configuration. Since this is not always possible or could be difficult to figure out without a dba, there is a solution in ColdFusion.

The problem arises from using cfqueryparam. ColdFusion pools queries so when the parameter changes, but the rest of the query doesn't, it can execute faster. For ColdFusion to pool a statement though, it must keep the cursor open on the database.

To solve the problem, you could remove all the cfqueryparam from your queries. However this is HIGHLY DISCOURAGED because you will lose the performance gains and also open your code up for SQL injection attacks.

Instead, simply go into the ColdFusion Administrator and lower the "Max Pooled Statements" setting. ColdFusion 9 has a default of 300. Lowering the setting to 100 solved the problem. You can experiment with the highest maximum based on your database settings.

To see how many open cursors you currently have open, run the following query:
view plain print about
1SELECT COUNT(v.value) as OPENCURSORCOUNT
2FROM V$SESSTAT v, V$SESSION s
3WHERE v.statistic# = 3 and v.sid = s.sid

To view other cursor information, use the following query:
view plain print about
1SELECT v.value as numopencursors ,s.machine ,s.osuser,s.username
2FROM V$SESSTAT v, V$SESSION s
3WHERE v.statistic# = 3 and v.sid = s.sid


Recent Comments

RSS

Subscribe