PHP, multiple requests, and slow load times

Recently I did some major updating to a web application I wrote a few years ago. The web app was the usual LAMP stack and ran on 10-20 public kiosks.
The web app allowed an admin to run two kinds of reports on the data. The first report took a very long time to run, but the second report was much simpler and was usually quicker. After all the updates were done suddenly both reports took forever to run.

I had updated MySQL to the latest version and the database architecture had changed to support new features so I spent a lot of time re-examining how the db was set up and checked the indexes on the tables.

I then looked at the queries to see if any of them could be optimized. I added timers around queries that I thought might be slow, but everything seemed normal. Yet still the request times were abysmal.

I tried using xdebug to see if there were any processes that were being called that I was missing when reviewing the code, but everything was as expected there.

In the end I finally realized that the problem was with session. session_write_close(); was not being called in the reports scripts and so each request was locking the other out until it was done generating the report. This was a very simple thing that I overlooked.

When debugging its always best to start with the simple and move towards the complex. Checking that session was properly handled should have been one of the first things I checked. Long load times on concurrent request should’ve been a red flag, but so much had changed that I didn’t start at the beginning and instead dove into more complex areas that only muddied the issue. Its the sort of mistake that you tell yourself you’ll never do again, but somehow you end up repeating anyway.

SQL Server 2008 "Saving Changes is not permitted" error

Work just upgraded us developers to SQL Server Management Studio 2008. Developing in the test database I’m often quickly creating tables or modifying tables through Enterprise Manager. Once I was upgraded to 2008 I kept getting this error:

Saving changes is not permitted. The changes that you have made require the following tables to be dropped and re-created. You have either made changes to a table that can’t be re-created or enabled the option Prevent saving changes that require the table to be re-created.

I looked through the options but I could not find it. I can never find the option I’m looking for in MS products. A quick Google search showed me that it was right there all along. For anyone else that is blind and can’t navigate the menus you can turn this off here:

Tools->Options->Designers->Table and Database Designers->Prevent Saving changes that require table re-creation.

Coldfusion’s Missing Huffman code table entry error Redux


Updating Coldfusion with the newest patch did not work. We are still seeing the “Missing Huffman” error in production. This occurs a few times a day. I’m now attempting to use the work-around posted in the Adobe Thread on the issue.

Below is my version of the work around:

			newImageName =  createUUID() & ".jpg";
			// create a file object representing the path to the saved image
			outFile = createObject("java", "").init( arguments.destination_path );

			// extract the underlying BufferedImage from your CF image object
			bi = ImageGetBufferedImage(myImage);

			ImageIO = createObject("java", "javax.imageio.ImageIO");
			// use ImageIO (instead of cfimage) to physically save the image to disk
			ImageIO.write( bi, "jpeg", outFile );

A Few Holes in Bruce Willis's Career

At lunch today my co-workers and I noticed a few holes in Bruce Willis’s career:

  1. The First Deadly Sin / Loaded Weapon 1 / Alpha Dog
  2. Die Hard 2 / Look Who’s Talking Too
  3. Four Rooms
  4. The Fifth Element
  5. The Sixth Sense
  6. Lucky Number Slevin
  7. The Whole Nine Yards
  8. The Whole Ten Yards
  9. 12 Monkeys/Ocean’s Twelve
  10. Sixteen Blocks

I don’t think anyone could say he did this is on purpose. I’m just wondering if he’ll be able to fill in the gaps before he dies.

Coldfusion's Missing Huffman code table entry error


While working on a content management system with functionality to allow users to upload images there was an error that would occur seemingly at random. When a user uploaded a jpg nine times out of ten it would work fine, but every so often there would be an error “Missing Huffman code table entry”.

After some Googling I discovered on this blog that this is a bug in Coldfusion 8. Adobe has patched it and you can fix the issue by making sure you’ve installed the latest hot patch to Coldfusion 8. You can download the patch here.

Using Coldfusion's ImageNew() with imageOverlay()

UPDATE: read here

I am working on a web app where users are allowed to upload images. All the images need to be square when displayed so I planned to use the imageOverlay() function in Coldfusion to create a blank square image and then overlay the uploaded image on top of it. I was using the imageNew() function to create the square image dynamically based on the height and width of the uploaded image. It seemed like it would work, but every time I uploaded an image imageOverlay() would crap out with the error “Overlay operation requires the two sources to match in number of bands and data type.”

I did a Google search for this error and there was only one relevant result with this error from Ray Camden’s blog. He couldn’t figure out how to fix this error either and created a work around by duplicating and then transforming his original image to his needs.

I really wanted to use ImageNew(). The functionality was there, but it just wouldn’t work. This felt just like another Coldfusion ‘gotcha’ (isDefined(“session.myVar”)!) but figured I could come up with a work-around. My solution was to save the file to a temp directory and then load it again. Once this is done the image will work with imageOverlay.

There’s got to be a better way to make this work. Anyone?

Below is my workaround:

Coldfusion SESSION and COOKIE scope

Working with both SESSION and COOKIE scope tripped me up today so I wanted to illustrate what went wrong so hopefully others can avoid this.

Example 1:

This sets a session variable that will exist for the life of the session.

Example 2:

This sets a cookie that will exist for the life of the browser session.

Example 3:

This sets a cookie that will expire in 8 hours.

In my application.cfc I had set:

" />

I had incorrectly thought that this timespan would also apply to the COOKIE scope, but it does not.

Using Coldfusion's ValueList() to evaluate dynamic Query column names

Occasionally I use Coldfusion’s valuelist() function to generate a comma separated list from a query. The syntax of this function will only allow the function call use a static variable and cannot use a dynamically generated value.

I just found a great tip from Richard Davies’ blog: You can use the array ToList function in place of the valuelist() function which will evaluate correctly at runtime.


Creating Webservices with Variable Arguments and Other Gotcha's in Coldfusion

Coldfusion makes building webservices very easy. You can quickly create a component and set the function you wish expose by setting the function access to remote and you are done!

Webservices are a bit quirky though and what worked as a component call won’t always work as a webservice call. Below are some of the issues I had to work though. Hopefully this saves time for someone else.

Webservice functions cannot have optional arguments.

Unlike a function call in a component, all arguments must be passed in a webservice call. If you do not pass the exact number of arguments with the correct type you will get an error that looks something like this:

coldfusion.xml.rpc.ServiceProxy$ServiceMethodNotFoundException: Web service operation getItems with parameters {} cannot be found

I was once building a webservice to search on multiple terms and for a moment was faced with the prospect that I would need to build multiple versions of the same function to search on a single term, or two, or three, etc. The solution to this problem is for your webservice to take a single structure as its only argument.

Inside the myArguments structure you can now pass as many or few values as you’d like.

If you’ve built your webservice as a public facade for an underlying service you can now just pass your webservice argument struct to that service as the argumentCollection. The service layer can then work normally and have multiple optional arguments if you’d like since it will receive the webservice’s single structure argument as its argumentCollection.

Webservices are cached in the Coldfusion Administrator

This is not an issue once the webservice is deployed, but during development you may find that your code updates don’t seem to be changing anything. This is because the old wsdl information of the webservice gets cached in the CFadmin. You must log into CFAdmin and manually delete it from the webservice registry. It will re-register itself once you use it again.

Webservices have data limitations that Coldfusion Components do not have

My co-worker & I were once getting an error from a webservice that was making us pull our hair out. The function worked fine when called as a component, but once called as a webservice the same data would throw this error:

The fault returned when invoking the web service operation is:AxisFault faultCode: { }Server.userException faultSubcode: faultString: java.lang.NumberFormatException: Invalid date fault

Eventually we discovered that in the test data there was a date with a value of “9999” for the year. This date is not out of range for Coldfusion, but when the call is converted to a webservice the date is out of range for Apache Axis and the request puked.