Transaction management difficulty in Composite Entity Design

According to J2EE Design pattern book, I抦 trying to design better 揅omposite?Entity Bean. The book says, 揈ntity beans are not intended to represent every persistent object in the object model. Entity beans are better suited for coarse-grained persistent business objects?(310 Core J2EE Patterns). They call it 揅omposite Entity.? Here is little explanation about composite entity.

http://www.ciol.com/content/technology/j2ee/

Sounds interesting and the book convinces me. However, as I designed my 揷omposite?entity beans, I encountered some difficulty. The example source codes in the book use a couple of model objects called 揤alue Object?and DAOs. It means that one entity bean doesn抰 simply represent one row data in one table. It represents 揷omposite?data from many tables as name indicates.

And here is my question: how can we manage transactions in composite entity? In entity bean specification, only CMP entity beans support transactions (managed by container). However, composite entity obviously doesn't fit in CMP, so a natural choice would be BMP. But we cannot use JDBC transaction or JTA transaction in BMP entity beans, right? (Actually, I failed.) If we use a session bean and it directly accesses database through DAO, it wouldn抰 violate EJB specification since session beans support transactions (Sun抯 pet store below, for example).

http://java.sun.com/j2ee/sdk_1.2.1/techdocs/guides/ejb/examples/warehouse/WarehouseEJB.java

But this is not the case here. I need a entity bean to support synchronization and concurrent accesses to data.

The followings are example codes from 揅ore J2EE Patterns?book (325). As you can see, if second or third DAO抯 deleting operation were failed, we would lose data consistency. This ejbRemove operation must remove all data from three tables or rollback operations and throw an exception.

publicclass ResourceEntityimplements EntityBean{

...

publicvoid ejbRemove(){

try{

//Remove dependent objects

if (this.skillSets !=null){

SkillSetDAO skillSetDAO = getSkillSetDAO();

skillSetDAO.setResourceID(employeeId);

skillSetDAO.deleteAll();

skillSets =null;

}

if (this.blockoutTime !=null){

BlockOutTimeDAO = blockouttimeDAO = getBlockOutTimeDAO();

blockouttimeDAO.setResourceID(employeeId);

blockouttimeDAO.deleteAll();

blockOutTimes =null;

}

//Remove the resource from the persistent store

ResourceDAO resourceDAO =new ResourceDAO(employeeId);

resourceDAO.delete();

}catch (ResourceException ex){

...

}

}

...

}

What is a better design in this case? I really appreciate any opinions and arguments.

Thanks.

-tom

[3632 byte] By [t_noda] at [2007-9-27 17:29:48]
# 1
in my opinion the composite entity patterns greatest value was in reducing the netowrk load, however in ejb 2.0 with local interfaces there are other ways of doing that. i am pondering the posibility that this pattern is obsolete, but i am still looking into the whole thing.
kajaherink at 2007-7-6 12:35:59 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

Exactly. Considering network traffic, any remote-call beans are really expensive. That was my first concern and that's why I'm digging myself into composite entity design.

But you're right. We now have local interfaces. I totally overlooked it. In ResourceEntity example case, we could make three local entity beans instead of one composite entity bean. And we could have a session bean that contains business logic and transaction management for those three.

Thank you for your idea. If you have other idea, please let me know. It must be helpful for me.

-tom

t_noda at 2007-7-6 12:35:59 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

Hi guys,

I have a idea.

In DAO component{

operation(){

Connection conn = ...

conn.setAutoCommit(false);

// perform delete operation

// don't commit().

// don't close connection

}

commit(){

conn.commit();

}

rollback(){

conn.rollback();

}

}

From EJBean{

try{

new dao1().operation();

new dao2.operation();

new dao3.operation();

dao1.commit();

dao2.commit();

dao3.commit();

}

catch(Exception ex){

dao1.rollback();

dao2.rollback(); ....

}

}

If our connection object doesn't become stale. Hope this will solve the problem... Obviously our transaction must be of short period .

BTW, does it mean instead of composite entity we must use a session bean that manage the workflow with DAO components.. ? :)

Some of the projects are still developed using EJB 1.1

Pls give your feedback..

-Thanks

skalidas at 2007-7-6 12:35:59 > top of Java-index,Other Topics,Patterns & OO Design...