|
View:
New views
14 Messages
—
Rating Filter:
Alert me
|
|
|
Fetching a Persistence ContextHey,
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 |
|
|
Re: Fetching a Persistence ContextHi 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 Hope it helps. Jean-Louis
|
|
|
Re: Fetching a Persistence ContextOK. 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 <jean-louis.monteiro@...> 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. > > |
|
|
Re: Fetching a Persistence ContextOn Oct 15, 2009, at 8:33 AM, Jean-Louis MONTEIRO 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 Wish I could delete that thread from the archive -- I should never have given out those internal details. It's not portable and it is not even guaranteed to work in OpenEJB in the future. That whole hashcode thing is a really ugly workaround and I'd really like to find another way to do that chunk of code. The @PersistenceContext can be used to make things available in JNDI for lookup and is what should be used -- that or a <persistence- context-ref> entry in xml. I'm not sure why this page is formatted so strangely, but it has the right info: http://openejb.apache.org/3.0/persistence-context-ref.html This is the wiki version which looks better: http://cwiki.apache.org/confluence/display/OPENEJBx30/persistence-context -David > 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. > > |
|
|
Re: Fetching a Persistence ContextBasically 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 <quintin@...> 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 > <jean-louis.monteiro@...> 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. >> >> > |
|
|
Re: Fetching a Persistence ContextI'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 <quintin@... > > 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 >> <jean-louis.monteiro@...> 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. >>> >>> >> > |
|
|
Re: Fetching a Persistence ContextHey,
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 <david.blevins@...> 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 <quintin@...> >> 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 >>> <jean-louis.monteiro@...> 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. >>>> >>>> >>> >> > > |
|
|
Re: Fetching a Persistence ContextJust to correct my wording of the following paragraph:
> 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. It should read ... But then I invoke a business method in it's @PostConstruct method... This creates a deadlock, because I assume the invocation thread waits on the construction to complete, which again waits for the invocation to return. Quintin Beukes |
|
|
Re: Fetching a Persistence ContextDoes it still deadlock when you annotate the bean
@ConcurrencyManagement(BEAN)? If it does, you can always spawn a new thread in your @PostConstruct method which invokes your business method. -- Richard Rak <richard.rak@...> On Fri, 2009-10-16 at 10:44 +0200, Quintin Beukes wrote: > Just to correct my wording of the following paragraph: > > > 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. > > It should read > > ... But then I invoke a business method in it's @PostConstruct method... > > This creates a deadlock, because I assume the invocation thread waits > on the construction to complete, which again waits for the invocation > to return. > > Quintin Beukes |
|
|
Re: Fetching a Persistence ContextHi David,
Sorry for that. It's an overall need that's why i pointed your post. It seems to me the correct link is http://openejb.apache.org/3.0/persistence-context.html Anyway, shouldn't be possible to add the module name (or the jar name) instead of an hash. I should be more predictable for end users. I mean something like myModule.jar#myPU Jean-Louis
|
|
|
Re: Fetching a Persistence ContextI think the bigger problem is the portability. Whether a hash or
friendly name, neither is portable. So if you wanted to move to another implementation, or write a work-anywhere solution you would have to make accommodations in code, where using a portable solution would possibly only require deploytime differences (like descriptors). Quintin Beukes On Fri, Oct 16, 2009 at 9:36 PM, Jean-Louis MONTEIRO <jean-louis.monteiro@...> wrote: > > Hi David, > > Sorry for that. It's an overall need that's why i pointed your post. > > It seems to me the correct link is > http://openejb.apache.org/3.0/persistence-context.html > http://openejb.apache.org/3.0/persistence-context.html > > Anyway, shouldn't be possible to add the module name (or the jar name) > instead of an hash. > I should be more predictable for end users. > > I mean something like myModule.jar#myPU > > Jean-Louis > > > > David Blevins wrote: >> >> >> On Oct 15, 2009, at 8:33 AM, Jean-Louis MONTEIRO 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 >> >> Wish I could delete that thread from the archive -- I should never >> have given out those internal details. It's not portable and it is >> not even guaranteed to work in OpenEJB in the future. That whole >> hashcode thing is a really ugly workaround and I'd really like to find >> another way to do that chunk of code. >> >> The @PersistenceContext can be used to make things available in JNDI >> for lookup and is what should be used -- that or a <persistence- >> context-ref> entry in xml. >> >> I'm not sure why this page is formatted so strangely, but it has the >> right info: >> >> http://openejb.apache.org/3.0/persistence-context-ref.html >> >> This is the wiki version which looks better: >> >> >> http://cwiki.apache.org/confluence/display/OPENEJBx30/persistence-context >> >> >> -David >> >>> 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. >>> >>> >> >> >> > > -- > View this message in context: http://www.nabble.com/Fetching-a-Persistence-Context-tp25910491p25931122.html > Sent from the OpenEJB User mailing list archive at Nabble.com. > > |
|
|
Re: Fetching a Persistence ContextOn Oct 16, 2009, at 12:36 PM, Jean-Louis MONTEIRO wrote: > Sorry for that. It's an overall need that's why i pointed your post. No worries. > It seems to me the correct link is > http://openejb.apache.org/3.0/persistence-context.html > http://openejb.apache.org/3.0/persistence-context.html > > Anyway, shouldn't be possible to add the module name (or the jar name) > instead of an hash. > I should be more predictable for end users. > > I mean something like myModule.jar#myPU Tried to go for that (and related), but the thing that kills it is that in most testing scenarios the module names are exactly the same. In maven all the module names end up being "classes" or "test- classes". Similar for Eclipse, Intellij and Netbeans. Seems in a build environment the classes are almost always output to a directory of a standard name. I'm hoping the global JNDI functionality in Java EE 6 might open up some doors here. It would still be something that the user has to configure for themselves, but there will at least be a way to bind all your persistence units to the new java:app/ or java:module/ namespaces so they can be looked up by fellow components in that app or module. We could have a feature to do that automatically (perhaps with more JNDI name formatting magic), thought it won't necessarily be portable. If they go to another container that doesn't have the feature they would still have to declare <persistence-context-ref> elements (or via annotation) in the app or module namespaces. -David > David Blevins wrote: >> >> >> On Oct 15, 2009, at 8:33 AM, Jean-Louis MONTEIRO 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 >> >> Wish I could delete that thread from the archive -- I should never >> have given out those internal details. It's not portable and it is >> not even guaranteed to work in OpenEJB in the future. That whole >> hashcode thing is a really ugly workaround and I'd really like to >> find >> another way to do that chunk of code. >> >> The @PersistenceContext can be used to make things available in JNDI >> for lookup and is what should be used -- that or a <persistence- >> context-ref> entry in xml. >> >> I'm not sure why this page is formatted so strangely, but it has the >> right info: >> >> http://openejb.apache.org/3.0/persistence-context-ref.html >> >> This is the wiki version which looks better: >> >> >> http://cwiki.apache.org/confluence/display/OPENEJBx30/persistence-context >> >> >> -David >> >>> 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. >>> >>> >> >> >> > > -- > View this message in context: http://www.nabble.com/Fetching-a-Persistence-Context-tp25910491p25931122.html > Sent from the OpenEJB User mailing list archive at Nabble.com. > > |
|
|
Re: Fetching a Persistence ContextWe 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 > <david.blevins@...> 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 <quintin@... >>> > >>> 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 >>>> <jean-louis.monteiro@...> 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. >>>>> >>>>> >>>> >>> >> >> > |
|
|
Re: Fetching a Persistence Context> 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. This is exactly what I was thinking caused it. In fact, I was suspecting something bad might happen when I was writing it exactly because I'm trying to use it before construction. > 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. It doesn't feel right. I was doing this in hope of getting past the 2 beans for startup. But I eventually got everything going with your first suggestion of having an EntityManager injected and passing in it's reference to the data initialization bean, together with the spring XML resource details. > 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. Luckily I don't even need it with the method I'm using now. I wasn't sure how transactions worked and until I ran into the problem thought it was linked to the injected instance, where it's actually in the ThreadContext. So injecting the bean and then passing it into the Local business bean works just as well. Thanks for brilliant software. I honestly can't think of a single situation in OpenEJB where I had to settle for second best - this would have been the first, but ended up being done in the same way I would have done it anyway. Do you smell that? I smell signs of well designed software. Q |
| Free embeddable forum powered by Nabble | Forum Help |