Welcome!

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">
</cfloop>
<cfset v_txt = v_txt & "end">

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

<cfcatch type="DATABASE">
<cfoutput>#cfcatch.message#</cfoutput>
</cfcatch>
</cftry>
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"
datasource="#attributes.ds#">
INSERT INTO clob_test
VALUES (seq_clob.nextval,
<cfqueryparam cfsql
type="CF_SQL_CLOB"
value="#v_txt#">)
</cfquery>
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
DX World EXPO, LLC, a Lighthouse Point, Florida-based startup trade show producer and the creator of "DXWorldEXPO® - Digital Transformation Conference & Expo" has announced its executive management team. The team is headed by Levent Selamoglu, who has been named CEO. "Now is the time for a truly global DX event, to bring together the leading minds from the technology world in a conversation about Digital Transformation," he said in making the announcement.
"Space Monkey by Vivent Smart Home is a product that is a distributed cloud-based edge storage network. Vivent Smart Home, our parent company, is a smart home provider that places a lot of hard drives across homes in North America," explained JT Olds, Director of Engineering, and Brandon Crowfeather, Product Manager, at Vivint Smart Home, in this SYS-CON.tv interview at @ThingsExpo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
SYS-CON Events announced today that Conference Guru has been named “Media Sponsor” of the 22nd International Cloud Expo, which will take place on June 5-7, 2018, at the Javits Center in New York, NY. A valuable conference experience generates new contacts, sales leads, potential strategic partners and potential investors; helps gather competitive intelligence and even provides inspiration for new products and services. Conference Guru works with conference organizers to pass great deals to gre...
The Internet of Things will challenge the status quo of how IT and development organizations operate. Or will it? Certainly the fog layer of IoT requires special insights about data ontology, security and transactional integrity. But the developmental challenges are the same: People, Process and Platform. In his session at @ThingsExpo, Craig Sproule, CEO of Metavine, demonstrated how to move beyond today's coding paradigm and shared the must-have mindsets for removing complexity from the develop...
In his Opening Keynote at 21st Cloud Expo, John Considine, General Manager of IBM Cloud Infrastructure, led attendees through the exciting evolution of the cloud. He looked at this major disruption from the perspective of technology, business models, and what this means for enterprises of all sizes. John Considine is General Manager of Cloud Infrastructure Services at IBM. In that role he is responsible for leading IBM’s public cloud infrastructure including strategy, development, and offering m...
"Evatronix provides design services to companies that need to integrate the IoT technology in their products but they don't necessarily have the expertise, knowledge and design team to do so," explained Adam Morawiec, VP of Business Development at Evatronix, in this SYS-CON.tv interview at @ThingsExpo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
To get the most out of their data, successful companies are not focusing on queries and data lakes, they are actively integrating analytics into their operations with a data-first application development approach. Real-time adjustments to improve revenues, reduce costs, or mitigate risk rely on applications that minimize latency on a variety of data sources. In his session at @BigDataExpo, Jack Norris, Senior Vice President, Data and Applications at MapR Technologies, reviewed best practices to ...
Widespread fragmentation is stalling the growth of the IIoT and making it difficult for partners to work together. The number of software platforms, apps, hardware and connectivity standards is creating paralysis among businesses that are afraid of being locked into a solution. EdgeX Foundry is unifying the community around a common IoT edge framework and an ecosystem of interoperable components.
Large industrial manufacturing organizations are adopting the agile principles of cloud software companies. The industrial manufacturing development process has not scaled over time. Now that design CAD teams are geographically distributed, centralizing their work is key. With large multi-gigabyte projects, outdated tools have stifled industrial team agility, time-to-market milestones, and impacted P&L stakeholders.
"Akvelon is a software development company and we also provide consultancy services to folks who are looking to scale or accelerate their engineering roadmaps," explained Jeremiah Mothersell, Marketing Manager at Akvelon, 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.
"IBM is really all in on blockchain. We take a look at sort of the history of blockchain ledger technologies. It started out with bitcoin, Ethereum, and IBM evaluated these particular blockchain technologies and found they were anonymous and permissionless and that many companies were looking for permissioned blockchain," stated René Bostic, Technical VP of the IBM Cloud Unit in North America, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Conventi...
In his session at 21st Cloud Expo, Carl J. Levine, Senior Technical Evangelist for NS1, will objectively discuss how DNS is used to solve Digital Transformation challenges in large SaaS applications, CDNs, AdTech platforms, and other demanding use cases. Carl J. Levine is the Senior Technical Evangelist for NS1. A veteran of the Internet Infrastructure space, he has over a decade of experience with startups, networking protocols and Internet infrastructure, combined with the unique ability to it...
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 ...
"Cloud Academy is an enterprise training platform for the cloud, specifically public clouds. We offer guided learning experiences on AWS, Azure, Google Cloud and all the surrounding methodologies and technologies that you need to know and your teams need to know in order to leverage the full benefits of the cloud," explained Alex Brower, VP of Marketing at Cloud Academy, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clar...
Gemini is Yahoo’s native and search advertising platform. To ensure the quality of a complex distributed system that spans multiple products and components and across various desktop websites and mobile app and web experiences – both Yahoo owned and operated and third-party syndication (supply), with complex interaction with more than a billion users and numerous advertisers globally (demand) – it becomes imperative to automate a set of end-to-end tests 24x7 to detect bugs and regression. In th...
"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.
Coca-Cola’s Google powered digital signage system lays the groundwork for a more valuable connection between Coke and its customers. Digital signs pair software with high-resolution displays so that a message can be changed instantly based on what the operator wants to communicate or sell. In their Day 3 Keynote at 21st Cloud Expo, Greg Chambers, Global Group Director, Digital Innovation, Coca-Cola, and Vidya Nagarajan, a Senior Product Manager at Google, discussed how from store operations and ...
"There's plenty of bandwidth out there but it's never in the right place. So what Cedexis does is uses data to work out the best pathways to get data from the origin to the person who wants to get it," explained Simon Jones, Evangelist and Head of Marketing at Cedexis, 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.
SYS-CON Events announced today that CrowdReviews.com has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5–7, 2018, at the Javits Center in New York City, NY. CrowdReviews.com is a transparent online platform for determining which products and services are the best based on the opinion of the crowd. The crowd consists of Internet users that have experienced products and services first-hand and have an interest in letting other potential buye...
SYS-CON Events announced today that Telecom Reseller has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5-7, 2018, at the Javits Center in New York, NY. Telecom Reseller reports on Unified Communications, UCaaS, BPaaS for enterprise and SMBs. They report extensively on both customer premises based solutions such as IP-PBX as well as cloud based and hosted platforms.