Static methods in data access classes

Are we getting any advantage by keeping functions of DAOs (to be accessed by Session Beans) static ?
[107 byte] By [abhinav21a] at [2007-9-28 7:31:39]
# 1

If you place all DAO logic in it's own class, you keep the option of easily reusing it open. If you embed the JDBC logic in a Stateless Session Bean, you need to make changes if you later want to use this logic elsewhere.

I prefer to have a class of static methods that require a Connection to do their work. Usually, there is a SessionBean that 'wraps' this DAO. The method signatures for the SSB are the same, minus the need for a Connection, which the SSB gets before delegating to the Static Class.

ken_robinsona at 2007-7-9 18:43:30 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

> I prefer to have a class of static methods that

> require a Connection to do their work. Usually, there

> is a SessionBean that 'wraps' this DAO. The method

> signatures for the SSB are the same, minus the need

> for a Connection, which the SSB gets before delegating

> to the Static Class.

Uggh, passing around a connection. I've had to refactor a bunch of code that used this pattern. We had classes in our system that took a class in their constructor simply because one of their methods created an object that needed the connection. Bad news--maintenance nightmare--highly inflexible.

What we've done is create ConnectionFactory singletons that are used throughtout the application in order to get connections to the database. All connection factory implementations implement the same interface so they can be plugged in from other components at runtime.

In my opinion, classes that use connections should manage them themselves to ensure proper cleanup and consistent state. By using a factory implementation, we simply provide the DAO classes the means by which they can retrieve connections to the database and even the name of the database that needs to be used that is pluggable. The DAO classes do their own connection management.

For similar reasons, I eschew the static method concept. By using class methods, you make it difficult to plug in a new implementation at runtime. I much prefer the singleton pattern with an instance that implements a common interface instead of a class full of static methods.

I recently needed to dynamically plug in new connection factory implementation so that we could use JNDI and DataSources within the context of the application server (pooled connections) but use direct connections via the Driver manager for unit testing (so the application server didn't need to be running). Because of the way this was coded, I simply changed the original factory to be an abstract factory and changed the getInstance() method to return a different implementation based on the environment (unit test vs live). This was painless and didn't require changing a single line of client code.

If I had to do this using the previous code that I refactored, I would have had to change about 200 jsp pages and dozens of classes that were making calls to the static method of the previous factory or hacked in something ugly and hard to maintain.

YMMV

meadandalea at 2007-7-9 18:43:30 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

I totally disagree.

The logic that utilizes the Connection should not care where the Connection came from, if it was already open or if it needs to stay open when it is complete.

It is very easy to get a Connection (from JNDI or a method like the one you have described), call one or more static methods, and then continue.

A class with nothing but static methods has no state to maintain, much like a StatelessSessionBean. Putting just the logic in a group of static method in my opinion makes the code much more flexible. I can utilize these Data Access Objects in EJBs (via store()/load()), I can utilize them in a thick GUI or I can use them straight from a servlet. In each case, the logic is alwasy the same, however where the Connection comes from may not.

ken_robinsona at 2007-7-9 18:43:30 > top of Java-index,Other Topics,Patterns & OO Design...