This may not be the right place to ask this, but if you can direct me
to the right place, I'd appreciate it.
I'm looking for a design pattern that someone must have already thought
through so that my head can stop hurting. Here's the problem:
I'm designing a webapp that has several servlets that all access a
database (or a couple of databases, actually) to either update or
retrieve information.
Rather than initializing these connections multiple times, dealing with
SQLExceptions in every class that uses JDBC queries, and generally
doing things multiple times, I thought I'd put all the database stuff
into a single class. I created a setUp() method that initialized the
database connection and then used static methods so I could do
something like
SQLUtils.executeQuery("a SQL statement");
SQLUtils.executeUpdate("another one");
anywhere in my webapp. If the database connection had not been created,
it got created before executing the SQL statement. If it was already
created, it just got done. I handled all the nastiest exceptions in the
SQLUtils class, so I didn't have to deal with them elsewhere.
You can probably guess the next part. I've discovered I need to connect
to more than one database, and this design does not support that.
Creating instances of a SQLUtil class would be a big pain, because then
I have to pass those around between my servlets and I lose one of the
huge advantages of this approach, namely a single, globally visible
interface to the database.
I thought about multiple classes, one for each database I'm connecting
to, but I know that can't be right on so many levels. Meanwhile, I'm a
little stumped.
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.
Thanks in advance for any ideas,
Todd
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@(protected)
For additional commands, e-mail: tomcat-user-help@(protected)