You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by ebaxt <eb...@broadpark.no> on 2008/03/28 16:24:55 UTC
Transaction when testing CRUD on facade
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: http://www.nabble.com/Transaction-when-testing-CRUD-on-facade-tp16351025p16351025.html
Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: Transaction when testing CRUD on facade
Posted by ebaxt <eb...@broadpark.no>.
Thank you David. I changed the transaction-type to TRANSACTION and it all
works fine.
-Erik
David Blevins wrote:
>
> 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:
> - http://openejb.apache.org/3.0/jpa-usage.html#JPAUsage-Bedetachaware
>
> -David
>
>
> 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:
>> http://www.nabble.com/Transaction-when-testing-CRUD-on-facade-tp16351025p16351025.html
>> Sent from the OpenEJB User mailing list archive at Nabble.com.
>>
>>
>
>
>
--
View this message in context: http://www.nabble.com/Transaction-when-testing-CRUD-on-facade-tp16351025p16368576.html
Sent from the OpenEJB User mailing list archive at Nabble.com.
Re: Transaction when testing CRUD on facade
Posted by David Blevins <da...@visi.com>.
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
persisted.
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:
- http://openejb.apache.org/3.0/jpa-usage.html#JPAUsage-Bedetachaware
-David
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: http://www.nabble.com/Transaction-when-testing-CRUD-on-facade-tp16351025p16351025.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>
>