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
-
DAO Pattern (6 messages)
- Posted by: Prasanna K
- Posted on: February 26 2004 08:52 EST
Threaded Messages (6)
- DAO Pattern by Andy Grove on February 26 2004 10:06 EST
- DAO Pattern by Paul Strack on February 26 2004 11:26 EST
- DAO Pattern by Sean Sullivan on February 26 2004 13:37 EST
- DAO Pattern by Prasanna K on March 09 2004 01:38 EST
- DAO Pattern by Palash Ghosh on February 27 2004 11:41 EST
- DAO Pattern - Problem by Prasanna K on March 10 2004 23:25 EST
-
DAO Pattern[ Go to top ]
- Posted by: Andy Grove
- Posted on: February 26 2004 10:06 EST
- in response to Prasanna K
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 -
DAO Pattern[ Go to top ]
- Posted by: Paul Strack
- Posted on: February 26 2004 11:26 EST
- in response to Prasanna K
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). -
DAO Pattern[ Go to top ]
- Posted by: Sean Sullivan
- Posted on: February 26 2004 13:37 EST
- in response to Paul Strack
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/ -
DAO Pattern[ Go to top ]
- Posted by: Prasanna K
- Posted on: March 09 2004 01:38 EST
- in response to Paul Strack
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 -
DAO Pattern[ Go to top ]
- Posted by: Palash Ghosh
- Posted on: February 27 2004 11:41 EST
- in response to Prasanna K
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 -
DAO Pattern - Problem[ Go to top ]
- Posted by: Prasanna K
- Posted on: March 10 2004 23:25 EST
- in response to Prasanna K
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