Direct JDBC query vs. Entity Bean

Discussions

EJB design: Direct JDBC query vs. Entity Bean

  1. Direct JDBC query vs. Entity Bean (2 messages)

    Hi all,
    I have a Session Bean that need to select
    some data from a database table.
    I need to use a findXXX() method of the
    home interface of the entity bean related to
    the table, or the session bean can directly
    query the DB via JDBC ?

    In the first case, the entity bean returned
    from the findXXX() method will stay inside
    my server EJB even after the end of my
    session bean (I think that it's not correct
    calling the remove() method in order to remove
    the entyty bean ... I don't want cancel data
    in the DB!).

    Thanks in advance for your help,
    Moreno

    p.s. sorry for my question .. I'm a newbie in EJB world.
  2. I have a Session Bean that need to select

    > some data from a database table.
    > I need to use a findXXX() method of the
    > home interface of the entity bean related to
    > the table, or the session bean can directly
    > query the DB via JDBC ?

    In my experience you should strive to use the findByXxxxx() method of the home interface. It has the benefit of loading the data from the database into the cache you will only lock entities which are actually retrieved from the collection returned by the findByXxxx() method.

    Mixing direct JDBC calls with entity beans often causes trouble. There is no way for the application server to detect that your are changing data in the database behind the scenes so in effect you are messing up the cache. Remember also that the application server has its own transaction manager which is separate from the DBMS.

    There is nothing wrong in using JDBC directly from a session EJB but you should probably take care not to mix the two without being very carefull. I have been surprised a couple of times :-)

    Finally, writing a simple EJB QL is very easy and involves 1/10 of the code!
  3. According to me one should not use Entity bean if good performance requirement is there instead one should go for session bean which makes use of precompiled statement improves the performance of J2EE /java application.