You will be redirected in 30 seconds or close now.

ColdFusion Authors: Yakov Fain, Jeremy Geelan, Maureen O'Gara, Nancy Y. Nee, Tad Anderson

Related Topics: ColdFusion

ColdFusion: Article

Beyond <CFQUERY> Part 2 of 2

Beyond <CFQUERY> Part 2 of 2

This month I hope to shed some light on Oracle's larger datatypes for those of you who like to do things big.

Let's go straight to the deep end with stored procedures. They're faster and more secure than standalone queries. In the first instance, procedures are faster because a lot of the execution process has already been completed before runtime, as I mentioned in my previous article(CFDJ, Vol. 4, issue 6) (In that article I covered some of the many ways you can use Oracle with the rather humble-looking <CFQUERY> tag.) The reason is that the validation process is done at compilation time, so when you invoke the procedure from your ColdFusion template, all it has to do is run.

Obviously, there are more generic benefits to using stored procedures, as they provide a degree of separation from your ColdFusion code, meaning that as long as the procedure call doesn't change, it's entirely possible to update the Oracle procedure incorporating database changes without having to alter your front-end ColdFusion application at all. Also, as I mentioned in my last article, using functions and procedures allows you to share the load across servers (provided you have separate servers for your ColdFusion and Oracle installations, as Macromedia recommends).

Procedures have the added benefit of hiding some of the complexity of database interaction. As is quite often the case, there's a mix of skills in any development team, and using procedures means that your strong Oracle developers can develop the database application, your ColdFusion developers can create the front end, and neither will have to have an in-depth knowledge of what the other is doing.

Some of you may recall a recent article by Ben Forta on <CFQUERYPARAM> ("Faster and Safer Database Queries," CFDJ, Vol. 4, issue 2) in which he highlighted the added security it provided through facilitating datatype checking. The same is true for calling stored procedures, which requires the use of <CFPROCPARAM> instead (see Listing 1). This also extends to the Oracle side of the application, where the procedure has to define the datatypes (see Listing 2). Combine this with proper exception handling and you have more secure database access. The two listings constitute an example of a simple Oracle procedure, showing parameter declaration basic exception handling.

The example procedure includes two types of calls to sequences (seq_category, seq_img_id). As you can see, they are simple to use and just as easy to create:

/* this is a simple sequence creation script*/
CREATE SEQUENCE seq_example increment by 1 start with 1;

And they ensure that all your primary keys are unique to boot. The only reason I mention this at all is that in the not too distant past I recall inheriting some code in which the developer had created a primary key by selecting the Max(id) from the table and then adding 1 to it. The main issue with this is that if someone else is running the same page at the same time, you're going to retrieve the same Max(id) and therefore, ultimately, try to insert a record with the same primary key.

How Much Can a CLOB Hold?
Recently I followed a thread on the Macromedia Forums discussing the use of CLOBS in Oracle-ColdFusion applications. The crux of the issue was that no one seemed to know how to fully utilize the 4GB of storage space Oracle says a CLOB can hold. As the thread progressed, it emerged that a number of people were unable to input data held in a CLOB that ex-ceeded 4,000 characters. Reading on, I saw a range of possible solutions using Oracle's dbms_lob package, but as ColdFusion cfstoredproc doesn't support CLOBs, it seemed this couldn't be the best solution. I decided to investigate the problem. Initial searches proved fruitless, so I ran a few experiments. True to form, I found I could successfully input 4,000 characters to a CLOB, but as soon as I tried to put in 4,001, I got this error:

Oracle Error Code = 1704

ORA-01704: string literal too long
Now my math isn't too hot, but that's a far cry from the 4GB Oracle had promised. So I looked still deeper and discovered a thread on a disparate forum located in one of the far corners of the Web where some helpful individual hinted that there were certain limitations with Oracle native drivers. With this is mind, I recalled a time when I dabbled in some light Delphi development that introduced me to ODBC drivers. Now it's widely accepted that ODBC drivers are slower than native drivers, but in the course of my experiment I discovered that this time lag might well be worth the wait. I got our DBA to install the Merant ODBC drivers and set up a datasource pointing to the database containing my CLOB table.

I then ran the following code, increasing the loop size to see what would happen: <cftry>

<cfset v_txt = "start">
<cfloop from="1" to="1000">
<cfset v_txt = v_txt & "0123456789">
<cfset v_txt = v_txt & "end">

<cfquery name="clob_test" datasource="#request.ODBC_ds#">
INSERT INTO clob_test VALUES (seq_clob.nextval, '#v_txt#')

<cfcatch type="DATABASE">
The result (as you'll find if you try it yourself) is way beyond the measly 4,000 characters facilitated by the native drivers. In fact, I consistently managed to increase this by four times - to just over 16,000 characters. Admittedly this is a long way short of the 4GB promised by Oracle, but in reality it's plenty to store dynamic content in.

The story doesn't end there, however. Late in my testing I stumbled across a new cf_sql_datatype that relates back to what I mentioned at the beginning of this article, where <CFQUERYPARAM> allows you to do datatype checking. Now I'm familiar with the normal array of varchar, number, and so on, but I'd never seen cf_sql_clob, so I tried this out by using the trusty <CFQUERYPARAM> tag in my insert statement and specifying the datatype as cf_sql_clob, making the insert look something like this:

<cfquery name="clob_test"
INSERT INTO clob_test
VALUES (seq_clob.nextval,
<cfqueryparam cfsql
Using Oracle native drivers I got a number of errors. On 8.1.5 I got this one:

Oracle Error Code = 1461
ORA-01461: can bind a LONG value only for insert into a LONG column
And on 8.1.6:

Oracle Error Code = 936

ORA-00936: missing expression
Not good. However, Merant came to the rescue, and I now have no problem inserting into my CLOBs. I actually let it stand at a final figure of around 400,000 characters successfully inserted.

The Driver Makes the Difference
So there you have it. The trick is not in complex Oracle coding, but in selecting the right drivers. If you're really worried about the time lags associated with ODBC drivers, then use them only for your CLOB manipulation. The main thing is, you won't have to change your code at all, but be sure your datasource is configured properly in the ColdFusion Administrator, either by enabling Long text retrieval or setting the buffer to an adequate level.

Oracle also offers another LOB in the form of binary large objects (BLOBs). As the name suggests, these store the information in binary format. It's possible to store all manner of information in your database: images, word documents, test files, even PDFs - the list is nearly endless.

Don't get too excited, though. While Oracle does provide the handy dbms_lob package with which you can insert BLOBs, check their length, see if they're empty, and so on, it's annoying that you can retrieve them only in binary format. Now I don't know about your users, but I've found mine prefer that the Word document they stored in the database comes out of the database as a Word document. Unfortunately, when you simply select a BLOB from the database...well, it comes out in binary. So this is a bit of a problem, as I'm sure you can appreciate.

All is not lost, however. You can create your own CFX tag to apply the mime type to the binary and format it correctly. Unfortunately, this is beyond the scope of this article, as my Java knowledge is minimal and my C++ nonexistent, but I won't leave you high and dry. There are two solutions for those whose pockets are deeper than their Java knowledge. There's a CFX tag called BLOB manager, available from the Macromedia tag library. I've tried the online demo, which seems pretty slick. For the more Java-savvy or less well funded, try out the tutorial at www.planet-source-code.com/vb/scripts/ ShowCode.asp?txtCodeId=45&lngWId=9.

*  *  *

Well, that's all from me, folks, but remember: use sequences for your primary keys and stored procedures to hide complexity, and be wary when handling large objects.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

@ThingsExpo Stories
DXWorldEXPO LLC announced today that All in Mobile, a mobile app development company from Poland, will exhibit at the 22nd International CloudEXPO | DXWorldEXPO. All In Mobile is a mobile app development company from Poland. Since 2014, they maintain passion for developing mobile applications for enterprises and startups worldwide.
Bill Schmarzo, author of "Big Data: Understanding How Data Powers Big Business" and "Big Data MBA: Driving Business Strategies with Data Science," is responsible for setting the strategy and defining the Big Data service offerings and capabilities for EMC Global Services Big Data Practice. As the CTO for the Big Data Practice, he is responsible for working with organizations to help them identify where and how to start their big data journeys. He's written several white papers, is an avid blogge...
I think DevOps is now a rambunctious teenager - it's starting to get a mind of its own, wanting to get its own things but it still needs some adult supervision," explained Thomas Hooker, VP of marketing at CollabNet, in this SYS-CON.tv interview at DevOps Summit at 20th Cloud Expo, held June 6-8, 2017, at the Javits Center in New York City, NY.
"MobiDev is a software development company and we do complex, custom software development for everybody from entrepreneurs to large enterprises," explained Alan Winters, U.S. Head of Business Development at MobiDev, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
Major trends and emerging technologies – from virtual reality and IoT, to Big Data and algorithms – are helping organizations innovate in the digital era. However, to create real business value, IT must think beyond the ‘what’ of digital transformation to the ‘how’ to harness emerging trends, innovation and disruption. Architecture is the key that underpins and ties all these efforts together. In the digital age, it’s important to invest in architecture, extend the enterprise footprint to the cl...
Data is the fuel that drives the machine learning algorithmic engines and ultimately provides the business value. In his session at Cloud Expo, Ed Featherston, a director and senior enterprise architect at Collaborative Consulting, discussed the key considerations around quality, volume, timeliness, and pedigree that must be dealt with in order to properly fuel that engine.
Two weeks ago (November 3-5), I attended the Cloud Expo Silicon Valley as a speaker, where I presented on the security and privacy due diligence requirements for cloud solutions. Cloud security is a topical issue for every CIO, CISO, and technology buyer. Decision-makers are always looking for insights on how to mitigate the security risks of implementing and using cloud solutions. Based on the presentation topics covered at the conference, as well as the general discussions heard between sessio...
IoT is rapidly becoming mainstream as more and more investments are made into the platforms and technology. As this movement continues to expand and gain momentum it creates a massive wall of noise that can be difficult to sift through. Unfortunately, this inevitably makes IoT less approachable for people to get started with and can hamper efforts to integrate this key technology into your own portfolio. There are so many connected products already in place today with many hundreds more on the h...
No hype cycles or predictions of zillions of things here. IoT is big. You get it. You know your business and have great ideas for a business transformation strategy. What comes next? Time to make it happen. In his session at @ThingsExpo, Jay Mason, Associate Partner at M&S Consulting, presented a step-by-step plan to develop your technology implementation strategy. He discussed the evaluation of communication standards and IoT messaging protocols, data analytics considerations, edge-to-cloud tec...
DXWorldEXPO LLC announced today that the upcoming DXWorldEXPO | CloudEXPO New York event will feature 10 companies from Poland to participate at the "Poland Digital Transformation Pavilion" on November 12-13, 2018.
Digital Transformation is much more than a buzzword. The radical shift to digital mechanisms for almost every process is evident across all industries and verticals. This is often especially true in financial services, where the legacy environment is many times unable to keep up with the rapidly shifting demands of the consumer. The constant pressure to provide complete, omnichannel delivery of customer-facing solutions to meet both regulatory and customer demands is putting enormous pressure on...
CloudEXPO | DXWorldEXPO are the world's most influential, independent events where Cloud Computing was coined and where technology buyers and vendors meet to experience and discuss the big picture of Digital Transformation and all of the strategies, tactics, and tools they need to realize their goals. Sponsors of DXWorldEXPO | CloudEXPO benefit from unmatched branding, profile building and lead generation opportunities.
The best way to leverage your CloudEXPO | DXWorldEXPO presence as a sponsor and exhibitor is to plan your news announcements around our events. The press covering CloudEXPO | DXWorldEXPO will have access to these releases and will amplify your news announcements. More than two dozen Cloud companies either set deals at our shows or have announced their mergers and acquisitions at CloudEXPO. Product announcements during our show provide your company with the most reach through our targeted audienc...
@DevOpsSummit at Cloud Expo, taking place November 12-13 in New York City, NY, is co-located with 22nd international CloudEXPO | first international DXWorldEXPO and will feature technical sessions from a rock star conference faculty and the leading industry players in the world.
Everything run by electricity will eventually be connected to the Internet. Get ahead of the Internet of Things revolution. In his session at @ThingsExpo, Akvelon expert and IoT industry leader Sergey Grebnov provided an educational dive into the world of managing your home, workplace and all the devices they contain with the power of machine-based AI and intelligent Bot services for a completely streamlined experience.
DXWorldEXPO | CloudEXPO are the world's most influential, independent events where Cloud Computing was coined and where technology buyers and vendors meet to experience and discuss the big picture of Digital Transformation and all of the strategies, tactics, and tools they need to realize their goals. Sponsors of DXWorldEXPO | CloudEXPO benefit from unmatched branding, profile building and lead generation opportunities.
22nd International Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, and co-located with the 1st DXWorld Expo will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud ...
In his keynote at 19th Cloud Expo, Sheng Liang, co-founder and CEO of Rancher Labs, discussed the technological advances and new business opportunities created by the rapid adoption of containers. With the success of Amazon Web Services (AWS) and various open source technologies used to build private clouds, cloud computing has become an essential component of IT strategy. However, users continue to face challenges in implementing clouds, as older technologies evolve and newer ones like Docker c...
JETRO showcased Japan Digital Transformation Pavilion at SYS-CON's 21st International Cloud Expo® at the Santa Clara Convention Center in Santa Clara, CA. The Japan External Trade Organization (JETRO) is a non-profit organization that provides business support services to companies expanding to Japan. With the support of JETRO's dedicated staff, clients can incorporate their business; receive visa, immigration, and HR support; find dedicated office space; identify local government subsidies; get...
Dion Hinchcliffe is an internationally recognized digital expert, bestselling book author, frequent keynote speaker, analyst, futurist, and transformation expert based in Washington, DC. He is currently Chief Strategy Officer at the industry-leading digital strategy and online community solutions firm, 7Summits.