Trasnction management in DAO

Hello

I am using DAO for persistent layer in my project. There are cases when i need to access more than one DAO to manage transaction .

To achive this i am passing Connection object in every method which is

needed in transaction .

But for this i have to duplicate every method of every DAO .

I like to remove this code duplication.

I need something like this :

interface Transaction{

begin();

commit();

rollback();

}

How we can design this so that i do not need to pass connection object

in every DAO.

Thanksd for your suggestions.

[626 byte] By [rajnishea] at [2007-11-27 5:36:37]
# 1

1. Configure a JNDI Datasource with the web server or application server you are using.

2. Select a Database Connection Pool

3. In DAO, access Connection via Datasource

public void method() {

try{

Context ctx = new InitialContext();

if(ctx == null ) {

throw new Exception("No Context");

}

DataSource ds =

(DataSource)ctx.lookup(

"java:comp/env/jdbc/DATABASE001");

if (ds != null) {

Connection conn = ds.getConnection();

if(conn != null) {

// Do database stuff

}

}

}catch(Exception e) {

e.printStackTrace();

}

}

GhostRadioTwoa at 2007-7-12 15:07:33 > top of Java-index,Other Topics,Patterns & OO Design...
# 2
Thanks for your reply.But it seems good when there is only one DAO object in transaction butif there are more than one DAO say CutomerDAO , SupplierDAO then how we will manage transaction
rajnishea at 2007-7-12 15:07:33 > top of Java-index,Other Topics,Patterns & OO Design...
# 3
If you have more than one DAO involved in a transaction, then you cannot have the transaction management code in the DAO. You have to move the transaction demarcation higher.See http://java.sun.com/products/ejb/javadoc-3_0-fr/
GhostRadioTwoa at 2007-7-12 15:07:33 > top of Java-index,Other Topics,Patterns & OO Design...