concept job in category iBatis

appears as: job
iBatis in Action

This is an excerpt from Manning's book iBatis in Action.

Putting business logic in stored procedures is widely accepted as being a bad practice. The primary reason is that stored procedures are more difficult to develop in line with modern application architectures. They are harder to write, test, and deploy. To make things worse, databases are often owned by other teams and are protected by tight change controls. They may not be able to change as fast as they need to to keep up with modern software development methodologies. Furthermore, stored procedures are more limited in their capability to implement the business logic completely. If the business logic involves other systems, resources, or user interfaces, the stored procedure will not likely be able to handle all of the logic. Modern applications are very complex and require a more generic language than a stored procedure that is optimized to manipulate data. To deal with this, some vendors are embedding more powerful languages like Java in their database engines to allow for more robust stored procedures. This really doesn’t improve the situation at all. It only serves to further confuse the boundaries of the application and the database and puts a new burden on the database administrators: now they have to worry about Java and C# in their database. It’s simply the wrong tool for the job.

Some people use batched statements to insert a group of records that differ only by a value that can be easily determined. One example is a system that inserted up to 200 records that represented tickets in the database. The tickets differed only by the database-generated primary key, and a ticket number had a starting value and was incremented for each ticket inserted. A loop in the DAO was used to accomplish the job, but a stored procedure would have been a faster and cleaner solution.

The simple fact of the matter is that the old adage that “Purists are always wrong” applies here, because both extremes are wrong. Stored procedures are a tool, and should be viewed as nothing more and nothing less than that. As an analogy, let’s look at the carpenter: he uses a hammer, a tape measure, and a saw. While he could use a hammer to measure a board, a tape measure does a much better job of it (and the same can be said for using the saw to drive nails). Every job has a tool that is the most appropriate, and every tool has a job that it was designed to do. Any time you use the wrong tool for a job, it does not work very well.