We use a ReentrantReadWriteLock so you can call yourself as many times as you like without deadlock.

The trick with the code below is essentially, beans are not available for business method invocations until after they are constructed (i.e. PostConstruct is finished).

So basically your construction code is waiting for your construction code to finish.

You could cheat as Richard suggests and launch a thread to call your init() method over your business interface. You're technically not allowed to do that, but in this case it's actually pretty safe. It'd be identical to having the init() method annotated with @Asynchronous -- once we get that support in.

Having a public init() method is really the big PITA here. As soon as we get the tx support in for singleton callbacks, you'll want to go straight in and remove that method.

-David

On Oct 16, 2009, at 1:43 AM, Quintin Beukes wrote:

Hey,

Yes. The first one was almost what I had in mind.

I have a related question though (in the sense that pondering this
problem resulted in it).

Is the following allowed by the spec. I haven't read the complete
spec, so trying to find an answer was a bit difficult (being over 600
pages in total).
@Singleton
@Startup
public class MySingletonBean implements MySingletonBeanLocal
{
 @EJB
 private MySingletonBeanLocal self;

 @PostConstruct
 public void startup()
 {
   self.init();
 }

 public void init() {}
}

As you can see I inject the bean into itself (which should be fine,
being a singleton and all). But then I invoke a business method.
Currently this creates a deadlock in OpenEJB.

Like I mentioned I couldn't find an answer, but if the spec does in
fact not cover it, maybe it would be a good idea to do so, to ensure
consistent behavior among app servers.

Quintin Beukes



On Fri, Oct 16, 2009 at 4:20 AM, David Blevins <[email protected]> wrote:
I'm not sure if I understand the overall goal.

Is this a case where the code that intends to use the PeristenceContext (EntityManager) does not know the name of the persistence unit and therefore
cannot look it up or have it injected?

If so, it still should be possible to create a component that has an
@PersistenceContext reference to the unit and then can pass it directly or indirectly to this more generic "work with any EntityManager" code. Perhaps
with a Singleton pattern like this one http://bit.ly/vklEb

You could also do a more generic version like this:  http://bit.ly/10AsNt


Hope that helps!


-David


On Oct 15, 2009, at 4:03 PM, Quintin Beukes wrote:

Basically I need a way to progammatically retrieve a unit manager.
Information regarding the PU is supplied to a module manager in an
core module which has no knowledge of the module until it's notified
of it.

So, from your last e-mail, I assume it would then be possible to
annotate the persistence context in the Startup singleton of the
module, and then specify the class and field name to the module
manager, which would then use this information to construct a lookup
backwards, and then have it wrapped in a transaction (since the call
to the module manager is a business method invocation) - unless I
misunderstood the code.

Though... 10 seconds ago I also thought of an option of doing a JNDI
lookup of the Singleton itself and then invoke on this reference -
this should wrap it in a transaction, which I can then use to pass in
an injected EntityManager reference?

Quintin Beukes



On Fri, Oct 16, 2009 at 12:29 AM, Quintin Beukes <[email protected] >
wrote:

OK. So just to confirm that things are still the same.

I know that unit names need not be unique, so I assume you still
generate the unique ID suffix. How does OpenEJB then decide what to do with it. I tried looking around the code, but am a bit lost on where it injects these entity managers and how it decides which persistence
unit to use. Could you perhaps tell me where to find this?

Quintin Beukes



On Thu, Oct 15, 2009 at 5:33 PM, Jean-Louis MONTEIRO
<[email protected]> wrote:

Hi Quintin,

you can use JNDI to get a reference.
Have a look here

http://www.nabble.com/Lookup-EntityManager-from-Stateless-EJB-Session-Bean-td24708299.html#a24737449

http://www.nabble.com/Lookup-EntityManager-from-Stateless-EJB-Session-Bean-td24708299.html#a24737449

Hope it helps.
Jean-Louis


Q Beukes wrote:

Hey,

When I have an EntityManager injected while having multiple
persistence units, I do it by specifying the unit name, something
like:
@PersistenceContext(unitName="Some-PU")
private EntityManager em;

Is it possible to do this not by injection, but fetching it by name
through an API of some sort?

Quintin Beukes



--
View this message in context:
http://www.nabble.com/Fetching-a-Persistence-Context-tp25910491p25910759.html
Sent from the OpenEJB User mailing list archive at Nabble.com.








Reply via email to