Solved

Adding a Layer between EJB and DAO (Data Access Objects)

Posted on 2002-04-19
8
2,294 Views
Last Modified: 2013-11-24
To access database we use simple java classes called as xxxDAO.java. Where we open the connection, call stored procedures and return the results(Result set and Out parameter) in User-Defined ResultSet to caller mainly Session Beans, and finally close the connection.

So the existing architecture has,

1. EJB - Session/Entity
2. DAO - where we call only stored procedures and return the result in User-Defined ResultSet.

e.g.
//Session bean
AbcEJB.java{
 //business method implementation
 public UserResultSet getValues(int a, int b, int c){
    //call DAO class and get the result
    return AbcDAO.getValues(int a, int b, int c);
 }
}

//Data Access code
AbcDAO.java{
 public static UserResultSet getValues(int a, int b, int c){
    //create connection in try catch block
    //call stored procedure
    return UserResultSet;
 }
}

There is NO problem as such calling the corresponding DAO's from EJB's.

There are several beans and corresponding DAO classes for data access like the above one with different method names.

I am thinking to add another layer between EJB and DAO. So that take an advatange of Facade/Factory or any other design pattern.

Here is my approach............

//changes in above Session Bean
AbcEJB.java{
//business method implementation
 public UserResultSet getValues(int a, int b, int c){
    //call Facade and get the result
    return Facade.getValues(int a, int b, int c);
 }
}

//interface between EJB and DAO
Facade.java{
    //create the connection
    //call the one business method for retated DAO
    public static UserResultSet getValues(int a, int b, int c){
         return AbcDAO.getValues(conn, a, b, c);
    }
}

//DAO without creating connection object
AbcDAO.java{
    public static UserResultSet getValues(Connection conn, int a, int b, int c){
         //call stored procedure
         return UserResultSet;
    }
}

Benefits:
Connection is created only in one class i.e. in facade and passed it to the DAO's.
Try-catch block for connection is not duplicated in DAO's.

Drawbacks:
DAO's methods are duplicated in facade.
EJB code has to be changed to accept the new class name instead of the existing DAO's.


I myself is not convinced with the above approach, but I think there is better way of doing the same work.

Please give your thoughts about the same and suggest a better way of doing things.

Thanks.
0
Comment
Question by:virendra032700
  • 3
  • 3
  • 2
8 Comments
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6954471
Hi, virendra.

First, is it true that your DAO methods will still have to catch SQLExceptions around the stored procedure calls? I mean, you still have to do:

    CallableStatement stmt = null;
    ResultSet rs = null;
    try {
        // prepare stored procedure call
        stmt = conn.prepareCall(...);
        // bind input and output params
        stmt.setXXX(...);
        stmt.setXXX(...);
        stmt.registerOutParameter(...);
        rs = stmt.executeQuery();
        // process the ResultSet?
    } catch (SQLException e) {
        // handle
    } finally {
        // resource cleanup -- ignore exceptions
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
            }
        }
    }

Is that right? So really the only thing you avoid is obtaining and releasing a Connection. Or perhaps I have misread your example. My point is, it seems you don't really buy much by eliminating the getConnection and Connection.close within the method.

What we did on my project, to abstract obtaining a Connection and releasing the JDBC resources, was to develop a DataSourceHelper class with utility methods. Something like:

public class DataSourceHelper {
    private static DataSource ds = null;

    private DataSourceHelper() {
        // prevent instantiation
    }

    private static DataSource getDataSource() {
        if (ds == null) {
            // JNDI lookup of DataSource
            ds = ...
        }
        return ds;
    }

    public Connection getConnection() {
        return getDataSource().getConnection();
    }

    public void cleanup(Connection conn, Statment stmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
            }
        }
    }
}

This encapsulates obtaining a Connection from the application server defined DataSource and the proper implementation of JDBC resource cleanup. (You'd be surprised how often developers seem to get this wrong even after you've given them the code!)

As far as "Connection is created in only one class", I'm not sure of your intent here. Is it to encapsulate the procedure for obtaining a Connection so that it is done consistently? If so, then the DataSourceHelper approach does that. Is it to reduce the number of Connections that are being created during a transaction? Then your application server very well may be doing this already. I'm using WebSphere and, according to an IBM document that explains connection pooling in WAS, when a user name and password are specified that match the user name and password of a connection already allocated in the current transaction, the new request will share the allocated connection. This means that multiple requesters can share the same connection object to the database as long as they are in the same transaction. Calling close() on the connection object does not return the connection to the free pool until all requesters have called close(). In most cases, the application does not supply a user name and password when calling getConnection() on a DataSource.

What application server are you using? Do you know its behavior with respect to connection pooling?

In short, I'm saying that I don't think you really need another "layer" between EJBs and DAOs to accomplish your goals. You might be able to simply add a helper class encapsulating Connection establishment and JDBC resource cleanup and call that directly from the DAOs.

Best regards,
Jim Cakalic
0
 

Author Comment

by:virendra032700
ID: 6955621
Hi Jim,

I admire your thoughts. Infact we follow the same approach for creating the connection e.g. by saying like DB.getConnection(), where DB has all the DataSource relared information. We use Weblogic as an AppServer.

I read some books on Design Patterns. I was thinking to use a design pattern in this case.

I strongly feel there is some way of doing things in a better fashion.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6956178
This question is current, others are not.  I have added this to them all to bring them back to the foreground of attention so that the participating experts and you can continue the collaboration process to help you achieve your goal.

ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20277924.html
http://www.experts-exchange.com/questions/Q.20279686.html
http://www.experts-exchange.com/questions/Q.20280880.html
http://www.experts-exchange.com/questions/Q.20288055.html
http://www.experts-exchange.com/questions/Q.20289517.html
http://www.experts-exchange.com/questions/Q.20289516.html
http://www.experts-exchange.com/questions/Q.20290055.html
http://www.experts-exchange.com/questions/Q.20290921.html
http://www.experts-exchange.com/questions/Q.20291464.html




*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations if this item remains inactive another seven (7) days.  If you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643
POINTS FOR EXPERTS awaiting comments are listed here -> http://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 6962635
Hi, virendra. I was reviewing this question and thought I might extend my previous remarks.

In practice, the DAOs that we use on my project extend a common base class called 'Manager'. This base class contains common SQL-related methods useful in implementing DAOs. For example, there is an escapeString method for escaping quote characters embedded in a String value and a dateToString method for converting a Date object to a String representation appropriate to the target database.

The Manager class also provides implementations of getConnection and cleanup which simply delegate to the DataSourceHelper object I described previously. Since the specific DAO extends Manager, these methods are conveniently available.

A coding pattern that we have adopted for our DAOs is to provide two methods for each loader (aka finder). Both accept a list of parameters that specify the criteria and one accepts an additional parameter for Connection. For example, if I have a table that has a CHAR column as its primary key then I might have these methods in my DAO (the Core object is a simple JavaBean object that directly reflects the structure of the underlying database table):

public Core[] loadByKey(String key) throws SQLException {
    try {
        Connection conn = getConnection();
        return loadByKey(key, conn);
    } finally {
        cleanup(conn, null, null);
    }
}

public Core[] loadByKey(String key, Connection conn) throws SQLException {
    PreparedStatement stmt = null;
    try {
        stmt = conn.prepareStatement(...);
        stmt.setString(1, key);
        return loadByPreparedStatement(stmt);
    } finally {
        cleanup(null, stmt, null);
    }
}

public Core[] loadByPreparedStatement(PreparedStatement stmt) throws SQLException {
    ResultSet rs = null;
    try {
        rs = stmt.executeQuery();
        ArrayList list = new ArrayList();
        while (rs.next()) {
            list.add(decodeRow(rs));
        }
        Core[] cores = new Core[list.size()];
        list.toArray(cores);
        return cores;
    } finally {
        cleanup(null, null, rs);
    }
}

private Core decodeRow(ResultSet rs) throws SQLException {
    Core obj = new Core();
    // retrieve values from ResultSet and apply to object
    return obj;
}

We've found this factoring of the code allows for a good bit of reusability. In actuality, to give credit where it is due, we adopted this style after trying an open source tool called sql2java from 'bitmechanics' which generates DAOs from a database schema. We ended up performing some substantial modifications because that tool didn't do the mapping quite the way we liked as well as some other features that seemed to be missing. But using it, we now generate a majority of our data access code. And the 'idiom' used above provides both the client convenience of not having to supply a Connection object as well as the control of supplying one when if necessary. The DAOs assume that Connections (supplied or retrieved) will be under external transactional control. That is, if the client provides a Connection, it is responsible for managing the transaction. If a Connection is retrieved from the DataSource, either it will be in auto-commit mode or it will be enlisted in a container-managed transaction.

Do you think this coding 'idiom' is more to your intent?
Jim
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:virendra032700
ID: 6977019
I feel there is a way of doing the same problem in a better fashion.
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 300 total points
ID: 6981013
OK. What about Command pattern? We're using that also but not at the DAO level. Basic idea would be that you encapsulate the JDBC for a specific activity, say selecting some ResultSet, in a Command class that is essentially a JavaBean that implements the Command interface. To execute the Command, you would instantiate it, use its mutators to provide the input parameters, call execute, and then use its accessors to retrieve the outputs. In addition to the Command class, you could provide an Executive class that provides an 'execution environment' for the Command. Specifically, when the Command.execute is called, it calls Executive.executeCommand(this) with itself as the parameter, and that calles Command.performExecute which is where your real code is. The Executive.executeCommand method can implement your try/catch for all JDBC Exceptions (and any others as well), and wraps them in a CommandException which it throws.

IBM has a command framework which they describe here:
http://www-106.ibm.com/developerworks/ibm/library/i-extreme13/

Jim
0
 

Author Comment

by:virendra032700
ID: 7003087
Hi Jim,

I did not get time to review your comments, as I am busy in the work.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 7022717
We are all busy, virenda, but this is YOUR QUESTION, you've received significant input here and need to finalize this.  Question asked, help given, and more than a month later, still open.

I have finalized this, as stated above.  If/when you want to continue on this collaboration effort, let Jim know.

Moondancer - EE Moderator
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now