Switch between Client and Session Variables

Many developers often ask whether client variables or session variables are better. The answer to this question (as with many others) is "It depends". A common suggestion is to create some kind of wrapper for these scopes to allow easy switching.

The SessionMgr component provides this functionality. It will allow you to specify Client or Session scope when you intialize it. After that, you can have SessionMgr handle all of your Client/Session data. If you ever need to change which scope you use, just change the scope passed in and re-initialize SessionMgr - no need to change any other code.

SessionMgr will ensure proper locking and handle complex variables in Client scope, so you don't have to worry about those issues in your code.

[More]

An End to "String or binary data would be truncated"

If you use MS SQL Server, you have seen this error. It is annoying enough that you are getting an error, but this is one that doesn't tell you which field is causing the problem - leaving you to guess for yourself.

The first and most obvious solution is the use of the maxlength attribute of <cfqueryparam>. This is helpful in as much as the error that you get comes from the cfqueryparam tag instead of from the database - making it easier to determine which field has the problem.

The simplest way to prevent the error is to wrap a Left() function around the incoming data for that field, to truncate it to the length of the allowable data.

The main problem with these solutions is that you have to specify the length and if you decide to change the length of the field in the database, you have to remember to change your code as well.

Another option is to use my free DataMgr component for your inserts and updates. The DataMgr will actually tell you what field the error is on without you needing to specify the field length.

A newly added feature to DataMgr will help prevent the error altogether. The new truncate() method will return a structure of data truncated to the allowable length of each given field. This will allow you to insert or update a record using that data knowing that you won't get an error for data that is too long.

To use truncate() simply pass in the name of the table and the structure that you want to truncate and you will get back that same structure, truncated to the length of the fields matching the keys of the structure (this is the same sort of structure used for the insertRecord and updateRecord methods of DataMgr).

So, if you wanted to insert a record to a table named "mytable" using a structure named "mydata" and you wanted to circumvent an errors for text that is too long, you could do this:
<cfset DataMgr.insertRecord('mytable',DataMgr.truncate('mytable',mydata))>

Regardless of what solution you use on the server-side, the maxlength attribute of the input field will help on the client-side. This will prevent the user from entering more text into the text field than your database field can handle. This doesn't obviate the need for a solution on the server-side, but does make the applicaion more user-friendly.

Again, you must change this number if you change the length of your field in the database. I don't have a solution for this (yet), but I hope to add this capability to my free sebForm custom tags soon (well, eventually).

Whether or not you decide to use the DataMgr component, this will hopefully help you handle this common database error.

Starfish Rocks!

Ray Camden recently released an initial version of his free Starfish debugger. This is the first product that I have tried that really provides the information that you need to get detailed performance information on your ColdFusion applications.

I tried it because we were having performance problems on an application that I have been working on. The information provided by the ColdFusion debugging provides a great deal of detailed performance information. Unfortunately, it provides that information only on a per-page basis - which is difficult to aggregate and use to find performance problems that crop up over time.

The logging provided in the ColdFusion administrator will report long-running pages, but it won't provide more detailed information about which routines and queries.

Starfish provides all of the information available in the debugging aggregated over multiple requests and does so from the convenience of the ColdFusion administrator.

The information is easy to sort and provides information about how long templates take to run as well as CFC methods (distinguished by values passed in as arguments) as well as queries (also distinguished by the contents of the SQL).

As a version 0 product, you might expect that it would have some bugs. So far, I haven't found any that are major. The units of measurement aren't consistent (seconds used for Min Time as milliseconds used for the other two). For the record, I prefer seconds. Either the "Template Time Data" doesn't work or I haven't figured it out. These are very small nits, however, and it runs much better than one should expect from version 0 software.

In just two days of use, I have found several small (and one fairly large) performance problems on the site and fixed them. I highly recommend you check it out.

Good Luck!

PS - Raymond, I hope you enjoy your new copy of Civilation IV.

BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.