Transactions in Embedded OpenEJB

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Transactions in Embedded OpenEJB

Chris Miles
Hi,

 

I am currently using Embedded OpenEJB within a thick Swing application to
reuse my business components and I have a problem where when I am pulling
out an entity over JPA, the entity is being detached, and therefore the full
object graph is being pulled out.

 

I imagine my problem is because my EJB call is not done within a transaction
so I ask the following:

 

I have seen the code example which creates the TransactionBean but that only
allows me to scope that block of code into the transaction. What I need to
be able to do is have a transaction which exists across the lifetime of the
form (JFrame) so I can pull Entities out, update them and persist them
again.

 

Thank You.

 

Chris

Reply | Threaded
Open this post in threaded view
|

Re: Transactions in Embedded OpenEJB

Jacek Laskowski
On Sat, Dec 11, 2010 at 1:07 AM, Chris Miles <[hidden email]> wrote:

> I have seen the code example which creates the TransactionBean but that only
> allows me to scope that block of code into the transaction. What I need to
> be able to do is have a transaction which exists across the lifetime of the
> form (JFrame) so I can pull Entities out, update them and persist them
> again.

Hi,

I believe what you're asking is a stateful session bean that will use
extended persistence unit. You'll keep a handle to the bean somewhere
in your code and have a transaction open throughout the bean's
lifecycle. It may also extend the visibility of entities for too long,
but it's worth to give it a try and see whether more tweaks are
needed.

You may also use @Lazy annotation to mark simple fields not to be
fetched upon detachment - collection-based fields are by default lazy.

Jacek

--
Jacek Laskowski
Notatnik Projektanta Java EE - http://jaceklaskowski.pl
Reply | Threaded
Open this post in threaded view
|

RE: Transactions in Embedded OpenEJB

Chris Miles
Thanks Jacek.

At the moment my architecture is as follows:

ShopService Local Bean.
ProductService Local Bean.

Within my standalone swing form I fetch products with
shopservice.getCategories() which in turn fetches the list of categories
from the ProductService Bean.

As soon as the list of categories is called from my application it takes a
long time to fetch 5 categories, as it seems it is ignoring the lazy
properties and fetching the full tree

So each category has a list of sub categories which each have a list of
product so every single object is being fetched from the database.

I am assuming that because I am calling shopService from a Swing form which
is outside of the EJB environment this is why it is detaching it.

So in essence what I am trying to do is extend a transaction across the
lifecycle of the swing form. So an entity fetched with one button and then
saved with another button is within the same transaction.

Thanks

Chris

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Jacek
Laskowski
Sent: 11 December 2010 13:32
To: [hidden email]
Subject: Re: Transactions in Embedded OpenEJB

On Sat, Dec 11, 2010 at 1:07 AM, Chris Miles <[hidden email]> wrote:

> I have seen the code example which creates the TransactionBean but
> that only allows me to scope that block of code into the transaction.
> What I need to be able to do is have a transaction which exists across
> the lifetime of the form (JFrame) so I can pull Entities out, update
> them and persist them again.

Hi,

I believe what you're asking is a stateful session bean that will use
extended persistence unit. You'll keep a handle to the bean somewhere in
your code and have a transaction open throughout the bean's lifecycle. It
may also extend the visibility of entities for too long, but it's worth to
give it a try and see whether more tweaks are needed.

You may also use @Lazy annotation to mark simple fields not to be fetched
upon detachment - collection-based fields are by default lazy.

Jacek

--
Jacek Laskowski
Notatnik Projektanta Java EE - http://jaceklaskowski.pl