On Wed, 03 Dec 2003 16:18, Todd O'Bryan wrote:

> How do people handle this elegantly? The requirements are: a single,
> globally visible (within a webapp) database interface and the ability
> to access multiple databases easily.

The first point is to use a singleton to set up the database connection - or 
more correctly the connection pool. This way you can request a connection and 
return it to the pool easily. Of course every time you use one you will have 
to use try-catch blocks. Sorry no way around that.

However, if you are clever you will create data objects to hide all that 
stuff. Even better - why not get something to write all those horrid data 
objects - sql and trycatch blocks for you.

Last year we (another developer any myself) developed a project called sysmod, 
which essentially takes a xml description of a database schema and turns it 
into Java code and a real SQL schema. This is by no means a unique approach - 
tools like JBuilder allow you to use graphical UI's and UML to do something 
similar - and is even two way.

However, our approach is somewhat lighter. All we have is the XML model. The 
model generates the Java data objects. The code includes comments such that 
it creates nice JavaDoc as well, so in effect you have a self documenting 
business model from the XML. At this stage we have to write very little SQL, 
and no SQL at all for updating and adding records.

The actions (we use struts) become very simple because in addition to being 
able to load and save themselves from a database they can also load 
themselves from a request, therefore our code looks something like this:


        Client client = ClientFactory.newClient();
        client.loadFromRequest( "client", request );
        client.saveChanges();

This code essentially loads all the fields from the request prefixed by 
"client" into the data object. Then the call to saveChanges() saves the 
record to the database. The data objects also handle their own SQL 
exceptions, although they throw their own exceptions.

The Factory for each data object also provides lists of various kinds, ie you 
can call ClientFactory.findAll() and it will return a list of Client objects 
prefilled by a single query. You can also do a findByQuery with parameters to 
define the query.

The approach works very well, but is not without its drawbacks. It is quite 
easy to code things which look logical but create hundreds of queries and is 
inefficient. We are still working on ways to improve the system, such as 
caching data. It is also limiting - in that while it creates DataObjects it 
is not a trrue object hiracy. This is because the system is still based on a 
relational database. In other words this system is not a relational mapping 
tool. It is also not a object database.

However, it does provide a quick and easy way to prototype databases and the 
Java code to access it. Currently the system creates objects that are not JDO 
or EJB.

If you are interested I can provide it - 

Regards,

Peter Harrison

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to