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.


IoT & Smart Cities Stories
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
Where many organizations get into trouble, however, is that they try to have a broad and deep knowledge in each of these areas. This is a huge blow to an organization's productivity. By automating or outsourcing some of these pieces, such as databases, infrastructure, and networks, your team can instead focus on development, testing, and deployment. Further, organizations that focus their attention on these areas can eventually move to a test-driven development structure that condenses several l...
The term "digital transformation" (DX) is being used by everyone for just about any company initiative that involves technology, the web, ecommerce, software, or even customer experience. While the term has certainly turned into a buzzword with a lot of hype, the transition to a more connected, digital world is real and comes with real challenges. In his opening keynote, Four Essentials To Become DX Hero Status Now, Jonathan Hoppe, Co-Founder and CTO of Total Uptime Technologies, shared that ...
The graph represents a network of 1,329 Twitter users whose recent tweets contained "#DevOps", or who were replied to or mentioned in those tweets, taken from a data set limited to a maximum of 18,000 tweets. The network was obtained from Twitter on Thursday, 10 January 2019 at 23:50 UTC. The tweets in the network were tweeted over the 7-hour, 6-minute period from Thursday, 10 January 2019 at 16:29 UTC to Thursday, 10 January 2019 at 23:36 UTC. Additional tweets that were mentioned in this...
Over the course of two days, in addition to insightful conversations and presentations delving into the industry's current pressing challenges, there was considerable buzz about digital transformation and how it is enabling global enterprises to accelerate business growth. Blockchain has been a term that people hear but don't quite understand. The most common myths about blockchain include the assumption that it is private, or that there is only one blockchain, and the idea that blockchain is...
Never mind that we might not know what the future holds for cryptocurrencies and how much values will fluctuate or even how the process of mining a coin could cost as much as the value of the coin itself - cryptocurrency mining is a hot industry and shows no signs of slowing down. However, energy consumption to mine cryptocurrency is one of the biggest issues facing this industry. Burning huge amounts of electricity isn't incidental to cryptocurrency, it's basically embedded in the core of "mini...
Every organization is facing their own Digital Transformation as they attempt to stay ahead of the competition, or worse, just keep up. Each new opportunity, whether embracing machine learning, IoT, or a cloud migration, seems to bring new development, deployment, and management models. The results are more diverse and federated computing models than any time in our history.
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
Japan DX Pavilion at @CloudEXPO Silicon Valley
In his general session at 19th Cloud Expo, Manish Dixit, VP of Product and Engineering at Dice, discussed how Dice leverages data insights and tools to help both tech professionals and recruiters better understand how skills relate to each other and which skills are in high demand using interactive visualizations and salary indicator tools to maximize earning potential. Manish Dixit is VP of Product and Engineering at Dice. As the leader of the Product, Engineering and Data Sciences team at D...