Discussions

EJB design: DAO Pattern

  1. DAO Pattern (6 messages)

    Hi,
      I am using DAO pattern. I have Stateless Session Beans, which call DAOs for Database operations. I have a situation where multiple DAOs will be invoked from a session bean to perform database actions like insert/update/delete. My question is should I explicitly maintain the Transaction or the transaction attribute specified for the bean will take care of Transaction. If I want to explicitly maintain the transaction, I need to pass Connection Object from Session Bean to DAO. Is this correct? Please let me know.

    Thanks,
    Prasanna

    Threaded Messages (6)

  2. DAO Pattern[ Go to top ]

    Here's an example of a transaction across multiple DAO using JDBC connection and handling transactions in code (not using container managed transactions).

    http://www.codefutures.com/products/firestorm/faq/jdbc/transactions.html
  3. DAO Pattern[ Go to top ]

    The only thing you need to do in your DAO is to get your connections from a DataSource that you retrieve via JNDI.

    InitialContext context = new InitialContext();
    DataSource datasource = context.lookup("jdbc/[your-datasource-name]");
    Connection connection = datasource.getConnection();

    The EJB server is responsible for ensuring that the same connection is returned by the datasource within a single transaction. Since the EJB server does this for you, there is no need to pass the connection object around yourself.

    And as a real, you should use Container Managed Transaction (CMP) whenever you can, because they are much simpler. Only use Bean Managed Transactions (BMP) if you absolutely have to (and you really know what you are doing).
  4. DAO Pattern[ Go to top ]

    The only thing you need to do in your DAO is to get your connections from a DataSource that you retrieve via JNDI.

    >
    > InitialContext context = new InitialContext();
    > DataSource datasource = context.lookup("jdbc/[your-datasource-name]");
    > Connection connection = datasource.getConnection();
    >

    Here's some example code:

    http://daoexamples.sourceforge.net/

    http://www-106.ibm.com/developerworks/java/library/j-dao/
  5. DAO Pattern[ Go to top ]

    Hi Paul,
      I still have the same problem. I am using JBOSS 3.0 and Oracle 8i with Thin Driver.
    I have a table Customer with two fields
    CUSTOMER_ID NUMBER PRIMARY KEY
    CUSTOMER_NAME VARCHAR2(50)
    I have a DAO which will update this table. In my Stateless Session bean I have called 2 times the same DAO. Since the primary key is same for both records I get UNIQUE KEY violated exception. This is correct. But when I see the data in the table, the firt record is already committed. Data is not getting rolled back when there is a exception. I have set the transaction attribute as REQUIRED for this bean. Please let me know what would be the problem. Do I need to do any other extra thing? Is there any thing to configure in Connection Pool?

    Thanks in advance,
    Prasanna
  6. DAO Pattern[ Go to top ]

    Hi ,

    If ur application demands that u need multiple DAO method call
    (it will be from single DAO/multiple DAO) in a single method
    of statele session bean.Pl go ahead with JTA.I mean u need to
    manage ur own transaction.If u really requre I can send u some
    example code.

    Palash
  7. DAO Pattern - Problem[ Go to top ]

    I still have the same problem. I am using JBOSS 3.0 and Oracle 8i with Thin Driver. I have a table Customer with two fields
    CUSTOMER_ID NUMBER PRIMARY KEY
    CUSTOMER_NAME VARCHAR2(50)
    I have a DAO which will update this table. In my Stateless Session bean I have called 2 times the same DAO. Since the primary key is same for both records I get UNIQUE KEY violated exception. This is correct. But when I see the data in the table, the firt record is already committed. Data is not getting rolled back when there is a exception. I have set the transaction attribute as REQUIRED for this bean. Please let me know what would be the problem. Do I need to do any other extra thing? Is there any thing to configure in Connection Pool?
    Can anybody please look into this....

    Thanks,
    Prasanna