When to use EJBs

1) Does any one know where can I find documents that describe when EJBs should be used in an application? If I use a J2EE server like WebLogic, should I always try to use EJBS an my applications?
2)Once I have decided to use EJBs in an application, should there be an entity bean corresponding to every table in the database??
thanks

Hi
Well documentation can be found on sun's site and for EJB regarding the number based on table is not required.
I have a project which had around 30 Tables in it and we were using EJB's for it and had grouped EJB's based on table or functionality.
You could also make EJB's based on the tables logical grouping and can work it out.
Based on that you can define ur Interface.
Bye

Similar Messages

  • When not using EJBs can I make BD a Singleton and cache facade instances?

    Hi,
    In an application which does not use EJBs can I make BD(Business Delegate) a singleton?
    I was very sure about doing this but when I tried Google on the same subject the answers were'nt supportive of this but that was in the context of applications which used EJBs. And also item 4 in Effective Java isnt very supportive of caching Objects at the drop of a hat.
    When not using EJBs would it be an unnecessary thing to make BD a singleton and cahce Facade instances in a BD and DAO instances in a Facade? I am planning to use a array based blocking bounded buffer for the purposes of caching. Or would it be better to make both BD and a facade as SIngletons and just cache DAOs in a Facade?
    Any suggestion would be of good help to me.
    Thanks a lot.

    Not sure I understand all your design, but you seem
    to describe an architecture where requests are queued
    and handled serially.Sorry if I messed up while explaining it. No, it will not be handled serially. Since the BD is a singleton multiple threads can pass messages to it simulteanously, a bit like an object of the Action class in Struts. Since I dont see having any synchronized methods in a BD requests will be handled simulteanously.
    The impact on throughput of handling requests
    serially (as opposed to parallelizing them) probably
    outweights by far the cost of instantiating one more
    object per request...Yes, I understand that but as I explained above the reqests wont be handled serially.
    To be more clear, I am thinking of using any one of these two things:
    1) BD(Singleton)-->Facade(Singleton, caches DAOs in a thread safe data structure)
    2)1) BD(Singleton, caches Facade instances in a thread safe data structure)-->Facade(caches DAOs in a thread safe data structure).
    the thread safe data structure I am planning to have is a array based bounded buffer which blocks using wait and notify mechanism.
    Thank you for the reply.

  • Question about when to use EJB

    Hello All,
    Right now, I am tasked with taking data out of the database and storing it on the mid-tier. We will be storing this static data in an XML file. We were hoping to read it in when starting the server and then persisting the data for the life of the server. Is this a good time to user EJB? All I have seen is examples using a database. We would like to take the database hit out of the equation when it comes to retrieving this data, but would also like to avoid parsing the XML doc every time someone logs in to the application. Is EJB a good answer here, or is it overkill?

    If this is for use as a read-only dictionary for supporting an EJB application, by all means.
    But to use EJB just to do this - I think that would be overkill.
    A singleton would do the job fine - especially if the XML doesn't change - actually even if it does that doesn't matter.

  • When to use @Resource and when to use @EJB for dependency injection

    Hi,
    When do you use
    @Resource and when do you use @EJB for dependency injection?
    Thanks

    Captain obvious: Use @EJB for injection of EJBs, and @Resource for everything else.
    There was a discussion about this very topic quite recently, perhaps you can find it through the search.

  • Have to ask again:when to use ejb-ref ?

    hi, I asked this last week but nobody answered it.
    i am really curious about it and i checked some docs it said " it is useful
    coz beans can look each other up without needing to initialize JNDI to any
    particular driver".
    i wonder what means particular driver and when there will be great
    difference between using and not using.
    thanx in advance
    Ciao,
    zhxt

    In WLS you can have either global JNDI names that are visible throughout
    the server or JNDI names that are local to your application.
    The recommended best practice these days is to use ejb-refs and/or
    ejb-links to lookup your EJBs. For local EJBs, you don't need to have a
    global JNDI name since they are not visible to other applications.
    -- Rob
    scratchback wrote:
    hi, I asked this last week but nobody answered it.
    i am really curious about it and i checked some docs it said " it is useful
    coz beans can look each other up without needing to initialize JNDI to any
    particular driver".
    i wonder what means particular driver and when there will be great
    difference between using and not using.
    thanx in advance
    Ciao,
    zhxt

  • When to use ejb-ref?

    if i do not write <ejb-ref> in ejb-jar.xml,what will happen?the application
    can not run or performance is low?
    and is there any difference between bmp and cmp?i mean "does cmp need
    <ejb-ref>?"
    Ciao,
    zhxt

    In WLS you can have either global JNDI names that are visible throughout
    the server or JNDI names that are local to your application.
    The recommended best practice these days is to use ejb-refs and/or
    ejb-links to lookup your EJBs. For local EJBs, you don't need to have a
    global JNDI name since they are not visible to other applications.
    -- Rob
    scratchback wrote:
    hi, I asked this last week but nobody answered it.
    i am really curious about it and i checked some docs it said " it is useful
    coz beans can look each other up without needing to initialize JNDI to any
    particular driver".
    i wonder what means particular driver and when there will be great
    difference between using and not using.
    thanx in advance
    Ciao,
    zhxt

  • EJB3 newbie - when to use EJB?

    If I just want to build a reusable module that does not have any business critical actions, such as a login module, or a blog module, should I use EJB?
    Also, am I correct that only EJBs can run on distributed servers, and that all other non-EJB components in a web applications can only run on a single server?

    If I just want to build a reusable module that does
    not have any business critical actions, such as a
    login module, or a blog module, should I use EJB?
    No. Any Java module can be made to be reusable, if designed to be so. Actually, by using EJBs, you'll be limiting the reusability of your modules, since those would need to run inside an EJB container.
    Also, am I correct that only EJBs can run on
    distributed servers, No, they can run in distributed as well as non-distributed environments.
    and that all other non-EJB
    components in a web applications can only run on a
    single server?No. Not sure where you got that idea from. I think you're confusing multiple server environment (clusters) with distributed[i] business objects. You can cluster applications that collocate all their components in a single JVM.
    There are actually very few cases where the use of EJB is to be considered (roughly 10% of cases):
    - if you really need object distribution - this is not the norm
    - if you need to use IIOP as the protocol for communication
    - MDBs are a good solution for some applications heavily based on messaging

  • When to use ejb's

    Hi there
    I just have the following question?
    What requirement would make you use ejb's?

    Hi there
    I just have the following question?
    What requirement would make you use ejb's?A single requirement? "Requirement 5. The system must use ejbs."
    (And I have seen a requirement that was basically like this.)
    Other than that it certainly can't be done with a single requirement.

  • Why string has an extra blank when I use EJB business method to get it

    I have the following code:
    Person bean = home.create("Jon", 10, "Huge", 1.0);
    java.util.Enumeration result = home.findAll();
    if (result.hasMoreElements()) {
    Person bean1 =(Person) javax.rmi.PortableRemoteObject.narrow(result.nextElement(), Person.class);
    getTraceWriter().println(" ** name = " + bean1.getName() + ", rank = " + bean1.getRank() +
    ", power = " + bean1.getPower() + ", rating = " + bean1.getRating());
    bean1.remove();
    I found the bean1.getName is "Jon " instead of "Jon". why???? Does sb. have a clue?
    Thanks,
    JST

    Here is the code. Thanks very much!
    import javax.ejb.*;
    import java.sql.*;
    import javax.sql.DataSource;
    import java.util.Vector;
    import java.util.Enumeration;
    import javax.naming.Context;
    public class PersonBean implements EntityBean {
    public String name;
    public int rank;
    public String power;
    public double rating;
    public EntityContext context;
    private DataSource ds = null;
    private String user = "db2admin";
    private String password = "db2admin";
    * ejbCreate method
    * @param name String The person name
    * @exception javax.ejb.CreateException
    public PersonKey ejbCreate(String name)
    throws CreateException {
    return ejbCreate(name, 0, "default", 0.0);
    * ejbCreate method
    * @param name String The person name
    * @param rank int The person rank
    * @param power String The person power
    * @param rating double Rating of the person
    * @exception javax.ejb.CreateException
    public PersonKey ejbCreate(String name, int rank, String power, double rating)
    throws CreateException {
    if (name == null || name.length() == 0) {
    throw new CreateException("Invalid parameter: name cannot be null");
    else if (power == null || power.length() == 0) {
    throw new CreateException("Invalid parameter: power cannot be null");
    this.name = name;
    this.rank = rank;
    this.power = power;
    this.rating = rating;
    Connection con = null;
    PreparedStatement ps = null;
    try {
    System.err.println("before getconnection");
    con = this.getConnection();
    System.err.println("after getconnection");
    ps = con.prepareStatement("insert into TESTMEN values (?,?,?,?)");
    ps.setString(1, name);
    ps.setInt(2, rank);
    ps.setString(3, power);
    ps.setDouble(4, rating);
    if (ps.executeUpdate() != 1) {
    throw new CreateException("Failed to add (" + name + "," + rank + "," +
    power + "," + rating + ") to database.");
    PersonKey pk = new PersonKey(name);
    return pk;
    catch (javax.naming.NamingException ne) {
    throw new EJBException (ne);
    catch (SQLException sqle) {
    throw new EJBException(sqle);
    finally {
    try {
    if (ps != null) ps.close();
    if (con != null) con.close();
    catch (SQLException sqle) {
    sqle.printStackTrace();
    public void ejbPostCreate(String name, int rank, String power, double rating) {
    // empty for now
    public void ejbPostCreate(String name) {
    // empty for now
    * ejbFindPrimaryKey
    * Find bean by primary key
    * @return PersonKey Primary key object for the bean
    * @exception FinderException
    * @exception ObjectNotFoundException
    public PersonKey ejbFindByPrimaryKey(PersonKey key)
    throws FinderException, ObjectNotFoundException {
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    boolean found = false;
    boolean multipleFound = false;
    try {
    con = this.getConnection();
    ps = con.prepareStatement("select name from TESTMEN where name = ?");
    ps.setString(1, key.name);
    rs = ps.executeQuery();
    found = rs.next();
    if (found) {
    multipleFound = rs.next();
    if (!multipleFound) {
    return key;
    else {
    throw new FinderException("Multiple objects found with name = " + name);
    else {
    throw new ObjectNotFoundException("Cannot find object with name = " + name);
    catch (javax.naming.NamingException ne) {
    throw new EJBException (ne);
    catch (SQLException sqle) {
    throw new EJBException(sqle);
    finally {
    try {
    if (rs != null) rs.close();
    if (ps != null) ps.close();
    if (con != null) con.close();
    catch (SQLException sqle) {
    sqle.printStackTrace();
    * setEntityContext method
    public void setEntityContext(EntityContext context) {
    this.context = context;
    * unsetEntityContext method
    public void unsetEntityContext() {
    context = null;
    * ejbFindAll method
    * Find all the beans.
    * @return java.util.Enumeration Enumeration of all beans
    public Enumeration ejbFindAll(){
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
    con = this.getConnection();
    stmt = con.createStatement();
    rs = stmt.executeQuery("select * from TESTMEN");
    Vector keys = new Vector();
    while (rs.next()) {
    keys.addElement(new PersonKey(rs.getString("name")));
    return keys.elements();
    catch (javax.naming.NamingException ne) {
    throw new EJBException (ne);
    catch (SQLException sqle) {
    throw new EJBException(sqle);
    finally {
    try {
    if (rs != null) rs.close();
    if (stmt != null) stmt.close();
    if (con != null) con.close();
    catch (SQLException sqle) {
    sqle.printStackTrace();
    * ejbActivate() method
    public void ejbActivate() {}
    * ejbPassivate method
    public void ejbPassivate() {}
    * ejbLoad method
    public void ejbLoad() {
    PersonKey pk = (PersonKey) context.getPrimaryKey();
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
    con = this.getConnection();
    ps = con.prepareStatement("select * from TESTMEN where name = ?");
    ps.setString(1, pk.name);
    rs = ps.executeQuery();
    if (rs.next()) {
    name = pk.name;
    rank = rs.getInt("rank");
    power = rs.getString("power");
    rating = rs.getDouble("rating");
    else {
    throw new EJBException("No record found in ejbLoad()");
    catch (javax.naming.NamingException ne) {
    throw new EJBException (ne);
    catch (SQLException sqle) {
    throw new EJBException(sqle);
    finally {
    try {
    if (rs != null) rs.close();
    if (ps != null) ps.close();
    if (con != null) con.close();
    catch (SQLException sqle) {
    sqle.printStackTrace();
    * ejbStore method
    public void ejbStore() {
    Connection con = null;
    PreparedStatement ps = null;
    try {
    con = this.getConnection();
    ps = con.prepareStatement("update TESTMEN set " +
    "name = ?, rank = ?, power =?, rating = ? where name = ?");
    ps.setString(1, name);
    ps.setInt(2, rank);
    ps.setString(3, power);
    ps.setDouble(4, rating);
    ps.setString(5, name);
    if (ps.executeUpdate() != 1) {
    throw new EJBException("Failed in updating database in ejbSotre().");
    catch (javax.naming.NamingException ne) {
    throw new EJBException (ne);
    catch (SQLException sqle) {
    throw new EJBException(sqle);
    finally {
    try {
    if (ps != null) ps.close();
    if (con != null) con.close();
    catch (SQLException sqle) {
    sqle.printStackTrace();
    * ejbRemove method
    public void ejbRemove() {
    Connection con = null;
    PreparedStatement ps = null;
    try {
    con = this.getConnection();
    ps = con.prepareStatement("delete from TESTMEN where name = ?");
    ps.setString(1, name);
    if (ps.executeUpdate() != 1) {
    throw new EJBException("cannot remove by name = " + name);
    catch (javax.naming.NamingException ne) {
    throw new EJBException (ne);
    catch (SQLException sqle) {
    throw new EJBException(sqle);
    finally {
    try {
    if (ps != null) ps.close();
    if (con != null) con.close();
    catch (SQLException sqle) {
    sqle.printStackTrace();

  • When to use the EJB tier?

    Could someone explain in simple and general terms when, say for a brand new application, you should or should not use the EJB tier over retaining everything in the web tier? Obviously it will depend on what the application is, but how so?

    I generally use EJBs when i want propper transactional support in my webapps as you get lots of stuff thrown in ejbs thats saves me having to write code.
    There are loads of reasons why you should / shouldnt use ejbs though so have a look at these for a bit more info.
    When to use EJBs :
    http://www.jguru.com/faq/view.jsp?EID=126400
    or alternatively .....
    When not to use EJBs
    http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=11&t=006707
    Hope thats helps.
    Gaz.

  • Why do we use EJBs?

    Hi all,
    I am a student and trying my Hands on J2EE. The only question i have is why do we use EJBs when similar kind of functions can be done using Servlets also ...
    I may be sounding dumb, but i really wanna know when to use EJBs and when not to use them.
    Thanks,
    Raghuveer

    rags_raghuveer wrote:
    Hi all,
    I am a student and trying my Hands on J2EE. The only question i have is why do we use EJBs when similar kind of functions can be done using Servlets also ...
    Wrong. Servlets handle HTTP requests. There are four flavors of EJBs. Are you saying that servlets duplicate all that functionality? You understand neither servlets nor EJBs.
    I may be sounding dumb, Yes.
    but i really wanna know when to use EJBs and when not to use them.That's actually a very good question. Here's one person's answer:
    http://www.amazon.com/Expert-One-One-Development-without/dp/0764558315/ref=sr_1_1/104-2273680-6808730?ie=UTF8&s=books&qid=1192283013&sr=8-1
    %

  • Thred Safety when Injecting an EJB into servlet using @EJB

    Hi , i have read that
    when EJBs were injected into servlets using @EJB it is said that there will be no Thread Safety here ??
    Is this true.

    Hi,
    You can directly call the methods in the bean without using bean model
    see this thread
    Webdynpro and Oracle
    Regards
    Rohit

  • When to Use Servlet only, EJB only and, both Servlet And EJB???

    I have a question, If I want to build a Web application. Sometime i found that there is no need to use EJB as Servlet already provide that functionality.
    Can anyone tell me when do I use Pure Servlet? When do I use Pure EJB and, When do I use Both EJB and Servlet???
    I am very interesting in it.
    Thanks.
    Jack

    EJB and servlets are two different things. Trying to compare them doesn't really make sense. A web application is always going to need some way to process Http requests and this in Java is done accomplished through Servlets (or jsp pages which in turn also use servlets).
    EJBs are the basis of distributed cross-platform objects. They are handled through name services and stored in databases. An Entity Bean can be "brought back to life" as long as it's info is stored in a database. Regardless of a server being restarted. All this database support behind an Entity Bean (transactions, queries etc.) are very transparent to the programmer. They are provided by the EJB vendor.
    The class properties of a Servlet (or class-level member variables) are shared among all requests (running thread instances of the service method). That's why they should be used with a lot of caution. This feature can be very useful but if used improperly can also be very vulnerable to security and synchronization issues.
    I'm not an EJB expert but this explanation should give you some idea of what EJBs are all about.

  • When we use jsf can we also use hibernate or ejb for database visting.

    when we use jsf can we also use hibernate or ejb for database visting.

    http://blog.exadel.com/?p=8
    http://java.sys-con.com/read/46977.htm
    http://www.javaworld.com/javaworld/jw-07-2004/jw-0719-jsf.html
    http://www.jroller.com/page/ksevindik/?anchor=lookup_tables_with_hibernate_and
    Sergey : http://jsfTutorials.net

  • Why using EJB when we have BC4J ?

    Hello everybody
    When I heared about EJB two years ago, I read couple of articles about it and found it useful. Now, when I read about BC4J and the ability they give us, a question pops up into my mind. Why should we use EJB ?
    We can simple use BC4J and they are very good. I think there is something about EJBs that I dont know and thats why I think this way.
    I'll appriciate any help.
    Thanx in adnvace.

    BC4J is a J2EE framework that lets you get down to business and focus on building your application.
    It then gives you a choice of deploying your application using simple Java classes, or using an EJB Session Bean if you want to take advantage of EJB Session Bean's container-managed transaction (for example, to particpate in a transaction with another bean you didn't write), or method-level security.
    The key points are that it saves J2EE developers tons of time by allowing them to not waste their time on writing application plumbing code to implement the many J2EE Design Patterns that all real-world applications need.
    Around 800 Oracle Applications developers inside Oracle are using the BC4J framework to get their self-service web applications to market with better features in faster time than their competitors. The framework is filled with nuts-and-bolts application-building features that our own developers have told us are the boring, mundane, plumbing-kinda code that they don't WANT to write, debug, and test themselves.
    It gives you a big advantage and allows you do decide whether or not to use EJB at deployment time instead of forcing you to make that decision up front.

Maybe you are looking for

  • Ajuda com Iphone 4S Agenda Icloud

    Bom dia! De um determinado tempo ate hoje notei que meus contatos nao estao mais sincronizando com o icloud! Gostaria de uma ajuda para a devida configuracao se alguem poder me ajudar!

  • My iPhone says that it needs to cool down and it's really hot

    IIt's really hot I just woke up and the phone was on the table... I'm so confused it was not been used!

  • How do I get old Pictures back?

    I´ve just downloaded Apple ITunes and after installing and Configuring it suddendly all was gone. How do I get my old pictures back?

  • Storing music on my iPhone by album name

    All of my iTunes albums are stored alphabetically on my mac but are by album title on my iPhone 4s. How can I get them both to look the same?

  • Question about gamma correction

    I recently ordered prints from iPhoto that came directly off my Canon EOS Rebel XT (at max quality). The resulting photos looked very much as if they were printed at PC gamma correction rather than at Mac gamma. Is there something else I need to set