Accessing table through session bean/DAO


EJB programming & troubleshooting: Accessing table through session bean/DAO

  1. Accessing table through session bean/DAO (5 messages)

    I am trying to print a table using JSP page. I am trying to use MVC architecture for this purpose. I want to access the database in a stateless session bean through a DAO. I tried accessing database and get whole table in a ResultSet and then return the ResultSet. But I am getting a

    Here is the code of my DAO Interface and the Implemetation class:

    public interface DataEJBDAO
        public void init();

        public java.sql.ResultSet giveResults() ;


     * @author ram
     * To change the template for this generated type comment go to
     * Window>Preferences>Java>Code Generation>Code and Comments
    public class DataEJBDAOImpl implements DataEJBDAO{

    private DataSource jdbcFactory;

    public void init(){
    System.out.println("Entering DataEJBDAOImpl.init()");
    InitialContext c=null;


    c=new InitialContext();
    }catch(Exception e){
    System.out.println("Error in DataEJBDAOImpl.init()");
    System.out.println("Leaving DataEJBDAOImpl.init()");

      public java.sql.ResultSet giveResults()
    Connection con=null;
    PreparedStatement ps=null;
    ResultSet rs=null;

    String query="select employeeName from employeePersonelDetails";

    }catch(SQLException e){
    System.out.println("Inside DataEJBDAOImpl.giveResults()"+e);
    }catch(Exception e){
    System.out.println("Leaving DataEJBDAOImpl.giveResults()");

    Can anyone please tell me what to do? Is there any other better method of accessing data and passing back. I don't want to use Entity Beans.

    Thanks in Advance.
  2. You are getting a NonSerializableException because you are attempting to serialize an object that is, er not serializable. i.e the jdbc result set is being serialized at some point in your call stack, probably at the point of calling a remote EJB.

    The answer here is that you should not pass a JDBC result set out of a DAO. If your DAO interface exposes methods that are specific to any persistence technology (such as by using any JDBC classes) then your class is not a DAO. DAO must encapsuate the persistence mechanism.

    The usual technique is to use model objects, simple custom java beans with no persistence code inside. In the DAO unload the resultset into a collection of model objects and pass this to the EJB layer.

    Developers on my team currently think its ok to pass SQL strings into and out of DAOs.
  3. Hi,
    Thanks for your reply. Can you please elaborate your point or give me any example of how to do it. I am a newbee and don't know much about this.
    Thanks a lot.
  4. As rightly said u should not be passing resultset back to the DAO client, it wont be DAO in that case. DAO should parse the resultset , and create a Data Transfer 0bject or Value object and return it back for further usage. The value object has to be serializable(One of the requirements for being a value object) hence you wont get Serialization related error.
    So the steps wud be
    1) Session beans invoke DAO
    2) DAO access the data layer and gets the resultset.
    3) DAO create a Value object out of it.
    4) Returns the VO back to the session bean client.
    Hope this helps.
  5. Hey actually i faced a same problem few monts back
    Its because ResultSet is not serializabe and u must be trying to serialize it or passing it over a network(either ejb,rmi,or probably sockets,or storing it in file)
    Actually resultset object contains a pointer which points to current row
    Probably u can use vectors having your dto objects set to each result from resultset and return that vector

    I did the same thing using vectors
  6. I don't see this working... you should have the JDBC driver in the client side, the underlaying ResultSet representation should be serializable, and so on... just won't work.
    The best approach is the one mentioned earlier:
    Session queries DAO
    DAO creates DTO/Value Object from ResultSet (or whatever) and returns it to Session
    Session returns the DTO/VO to the client-side (might even transform it into something else).

    This approach some clear drawbacks:
    - You must duplicate the data in the ResultSet in the DTO/VO.
    - It difficults paginating through large datasets.

    Cheers and happy coding,