Database Abstraction - It's Stored Procedures
Posted by Mike Brunt at 6:31 AM
5 comments - Categories: .NET | DataBase | ColdFusion | JRun-J2EE
There are two situations that get discussed reasonably often relating to databases and applications. The first is the possibility that a database engine (the RDBMs) might be changed at some point from let's say, SQL Server to Oracle. In my career in the database-web application, which spans 16 years, I have literally never seen that happen with any database of any palpable size. I have seen several occasions where Access was up sized to SQL Server but I see that as different. The second situation, which I have seen several times, is where a new application is created to utilize an RDBMs that already existed or an existing application is extended to use an existing RDBMs. This second situation, which is discussed far less often, as it pertains to database abstraction is actually a far more real and occurring situation. In either of these two situations, efficient abstraction of the RDBMs from the application code makes the operation easier to accomplish.
In my experience, there is one method of abstraction that surpasses all others in terms of continuity of standards and usability across all application code development ASP, ASPX, CFML, JSP .NET PHP etc etc. That abstraction layer is Stored Procedures not some sort of code-level paradigm. My advise is used Stored Procedures wherever possible, avoid in-line SQL and also any paradigm that is application code dependent when interacting with RDBM's. Ongoing, this will make your development processes much smoother and predictable.
This blog piece of mine was inspired by one created by the author of the SQL Server 2005 Bible - Paul Nielsen
Just a reminder that I will be in Boston/Cambridge on May 11 and 12 at The Charles Hotel, in the bar from 7:00PM on if you want to come down to chat about all things Enterprise and HA.
tony petruzzi wrote on 05/10/09 8:46 AM
I'm going to have to completely disagree with you on this one as there are some reasons why stored procedures should never be used for database abstraction:1) you need to be granted the proper permission in the database in order to create or modify stored procedures. I know in most cases this is a problem, but in some case it can be.
2) depending on your RDBM the stored procedure language can be limiting to use in terms of proper error handling (sql server 2000 doesn't have a try, catch) and functionality.
3) debugging where exactly the stored procedure is failing is very difficult without the proper tools. it's much easier doing this on the application end.
4) the day that your application needs to move from sql server to oracle or from oracle to postgresql you're going to be in a world of hurt. while some of the syntax used in your stored procedures could be the same on the migrating engine as your current one, chances you're going to be porting about 85% of them.
with the advert of ORMs you get the flexibility of have all your business logic at the application level while not having to do the mundane task of writing crud and sql statement.
i still use stored procedures when i need to import and export data from the database in a nightly job and don't need to have coldfusion involved in the mix. but as application development, everything is contained on the application's end