Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you write your applications to be database independent?

My boss asks me to write only ANSI SQL to make it database independent. But I learned that it is not that easy as no database fully ANSI SQL compatible. SQL code can rarely be ported between database systems without modifications.

I saw people do different way to make their program database independent. For example:

  1. Externalize SQL statements to resource files.
  2. Write many providers class to support different database.
  3. Write only simple SQL, and keep away from advance functions/joins.

Do you always write your code "any database ready"? Or do it only if needed? If yes, how do you achieve it?

like image 600
Dennis C Avatar asked Dec 21 '08 16:12

Dennis C


People also ask

What is database independent code to data?

The generated java code is database independent. This means that you do not have to re-generate the java code in order to run it on another database. However, there are a few points to observe: Not all data types are available on all databases. Available data types may behave differently on different databases.

What can applications use to interact with a database?

SQL. The database server applications interface is accessed using SQL. It's a standard query language that's used to define and manipulate databases and data, and it's supported by all popular database servers.


4 Answers

You could use one of the many Object/Relational Mapper tools, like Hibernate/NHibernate, LLBLGen, and so forth. That can get you a long way to database portability. No matter what you do, you need to have some abstraction layer between your model and the rest of your code. This doesn't mean you need some sort of dependency injection infrastructure, but good OO design can get you pretty far. Also, sticking with plain SQL and thinking that will get you portability is rather naive. That would be true if your application was trivial and only used very trivial queries.

As for always writing an application to be "any database ready," I usually use some sort of abstraction layer so it is not hard to move from one database system to another. However, in many circumstances, this is not required, you are developing for the Oracle platform or SQL Server or MySQL whatever so you shouldn't sacrifice the benefits of your chosen RDBMS just for the possibility of an entirely seamless transition. Nevertheless, if you build a good abstraction layer, even targeting a specific RDBMS won't necessarily be too difficult to migrate to a different RDBMS.

like image 182
BobbyShaftoe Avatar answered Nov 20 '22 05:11

BobbyShaftoe


To decouple the database engine from your application, use a database abstraction layer (also data access layer, or DAL). You didn't mention what language you use, but there are good database abstraction libraries for all the major languages.

However, by avoiding database-specific optimizations you will be missing out on the advantages of your particular brand. I usually abstract what's possible and use what's available. Changing database engines is a major decision and doesn't happen often, and it's best to use the tools you have available to the max.

like image 40
Eran Galperin Avatar answered Nov 20 '22 05:11

Eran Galperin


Tell your boss to mind his own business. No, of course one can't say such things to one's boss, but stay tuned.

What's interesting is what business value is supposed to be supported by this requirement. One obvious candidate seems to be that the database code should be ready for working on other database engines than the current. If that's the case then that's what should be stated in the requirement.

From there it's up to you as an engineer to figure out the different ways to achieve that. One might be writing ANSI SQL. One might be using a database abstraction layer.

Further it's your responsibility to inform your boss what the costs of the different alternatives are (in terms of performance, speed of development, etcetera).

"Write ANSI SQL"... gah!

like image 43
PEZ Avatar answered Nov 20 '22 07:11

PEZ


Just for the record. There is a similar question here on Stackoverflow:

Database design for database-agnostic applications

like image 25
splattne Avatar answered Nov 20 '22 07:11

splattne