tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Blevins <>
Subject Re: Transaction when testing CRUD on facade
Date Fri, 28 Mar 2008 21:36:17 GMT
Hi Erik,

Your pattern looks fine.  The act of wrapping the EntityManager with a  
session bean essentially ensures that all calls to the EntityManager  
are within a transaction (assuming you don't explicitly change the  
transaction semantics of the session bean via the  
@TransactionAttribute or @TransactionManagement annotations).

I'd guess what's going on is that in your persistence.xml file you  
have your persistence-unit declared with transaction- 
type="RESOURCE_LOCAL" which basically means you have to manually use  
the javax.persistence.EntityTransaction API to mark your  
transactions.  The common side effect of using RESOURCE_LOCAL as the  
transaction-type and not using the EntityTransaction API to begin/ 
commit around access to the EntityManager is that no data will be  

I'd recommend transaction-type="TRANSACTION" which will allow your  
EntityManager to be enrolled in the container managed transactions  
that are created automatically around calls to your session bean.

As a side note, if you run into any exceptions that complain of an  
object being "detached", check out this doc for some techniques:


On Mar 28, 2008, at 8:24 AM, ebaxt wrote:
> Hi.
> I'm new to openEJB and EJB3 so please forgive me if this is a stupid
> question.
> Say I want to write a unitTest to test some CRUD methods on a  
> facade. I have
> a Session Bean with a method:
> public X createNewX(String name) {
> ... creates newX
> xxEAO.create(newX);
> return newX;
> }
> The xxEAO is another Session Bean (the EAO pattern as explained in  
> "EJB3 in
> Action").
> @PersistenceContext
> EntityManager em;
> ..
> public void create(X newX) {
>  em.persist(newX)
>  em.flush();
> }
> So the Facade creates a new X and delegates the EnitityManager  
> actions to
> the xxEAO.
> In my unitTest(using embedded openEJB) I call the createNewX on the  
> facade.
> Now, my question is, do the facade create a transaction, and is the  
> xxEAO
> create() method invoked within this transaction? Or do I have to  
> start a
> transaction in the unitTest?
> The reason I'm asking is that the test succeeds, but the data isn't
> committed to the database.
> Thanks
> Erik
> -- 
> View this message in context:
> Sent from the OpenEJB User mailing list archive at

View raw message