openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeremy Bauer <techhu...@gmail.com>
Subject Re: Entitymanager lookup using JNDI on webshpere
Date Tue, 13 Jan 2009 16:21:51 GMT
Devu,
Apologies. I should have clarified.  You can do the lookup from your POJO as
long as the POJO method is called from a method of your EJB.

example:

MYEjb {

       public void mymethodwhichneedsacachevalue() {
                 CachePOJO.getValue(key);
      }

}

CachePOJO {

       private void initialize() {
             InitialContext ctx = new InitialContext();
             Entitymanger mgr = ctx.lookup("java:comp/env/MyPCJndi");
             //use mgr to load values from DB.
             ....
      }

    public String getValue(String key) {
    // initialize is called if not initialized?
    ...
    }
}

Question - does your cache store a static reference to the entity manager or
does it only get used once during initialization?  If it is a one time
usage, this pattern should work fine.  If you need to store the entity
manager, this pattern likely will not work.  (There are various transaction
and thread safety issues to consider.)  Alternately, you'll could look up
the entity manager upon each usage or switch to an application managed
design.

-Jeremy

On Tue, Jan 13, 2009 at 2:37 AM, devu213 <devusmail@gmail.com> wrote:

>
> Hi Kevin /Jeremy,
>
> Thanks for your replies.
>
> In the code that you mention, I would have to follow a pattern like this...
>
> MYEjb {
>
>      EntityManager em = null;
>        InitialContext ctx = new InitialContext();
>        em = (EntityManager)ctx.lookup("java:comp/env/MyPCJndi");
>
>         public void mymethodwhichneedsacachevalue() {
>                  Cache.getValue(key, manager); // the manager is passed to
> the cache. Don't want this
>       }
>
> }
>
> CachePOJO {
>


>
>     public String getValue(String key, EntityManager manager) {
>             //singleton...uses manager to initialize from database for the
> first time....
>    }
>
> }
>
>
> What I really want is
>
>
> CachePOJO {
>
>        private void initialize() {
>              InitialContext ctx = new InitialContext();
>              Entitymanger mgr = ctx.lookup(jndinameofmgr);
>              //use mgr to load values from DB.
>       }
>
> }
>
> Yes, the cache is accessed from an EJB, but I don't want to be passing the
> manager around. In fact, I have this pattern problem repeatedly because we
> have DAO classes that always now need to get initialized with the manager
> which is passed from the EJB class (session facade). I end up having to
> pass
> my entitymanager around.
>
>
> In a previous project with hibernate, I remember being able to get the
> sessions directly from the container using JNDI and they would be bound to
> a
> transaction if there was one existing.  I want to be able to work with a
> similar pattern but stumble since the only place I am able to get a manager
> from right now is inside an EJB which I then have to pass around to lower
> layers.
>
> Thanks in advance.
>
> Devu.
>
> Jeremy, I don't have an option to go to WAS 7 right now, I will have to
> live
> with the fix packs. Will try out the method in Kevin's link.
>
>
>
> Kevin Sutter wrote:
> >
> > Devu213,
> > Besides Jeremy's references below, I want to be aware of WebSphere's
> > support
> > of OpenJPA.  I started to cover this the WebSphere and Java Persistence
> > blog
> > [1], but to cover your specific situation...  You are correct that the
> > version shipped with the WebSphere EJB3 Feature Pack was OpenJPA 1.0.
> > But,
> > we have created a service stream for this release in OpenJPA (1.0.x).  If
> > any problems are discovered in the 1.0 release, then we can fix them in
> > OpenJPA 1.0.x.  This updated version of OpenJPA will then get picked up
> > for
> > WebSphere iFixes and FixPacks.  So, you are not "stuck" with OpenJPA 1.0.
> > It will get updated just like the rest of your WebSphere offering as
> > additional FixPacks are applied.
> >
> > WebSphere v7 shipped a version of 1.2 OpenJPA.  This version will get
> > supported via the 1.2.x branch of OpenJPA.  You could follow Jeremy's
> > references below to use this newer version of OpenJPA with the EJB3
> > Feature
> > Pack, but then you no longer have access to the WebSphere extensions that
> > sit on top of the OpenJPA binaries.  It is an option though.
> >
> > Good luck,
> > Kevin
> >
> > [1]
> >
> http://webspherepersistence.blogspot.com/2008/09/support-for-openjpa.html
> >
> >
> > On Mon, Jan 12, 2009 at 10:42 AM, Jeremy Bauer <techhusky@gmail.com>
> > wrote:
> >
> >> Hi devu213,
> >> To use JNDI to lookup an EM or EMF in WebSphere app server you can
> define
> >> a
> >> class level annotation (or via XML in your ejb module deployment
> >> descriptor)
> >> on your session bean or servlet and use the "name" attribute to specify
> >> the
> >> JNDI name.  For example:
> >>
> >> @Stateless
> >> @PersistenceContext(unitName="MyPC", name="MyPCJndi")
> >> public class JPABean implements JPALocal, JPARemote {
> >> ...
> >>    @Resource SessionContext sessionCtx;
> >>
> >> public void beanMethod() {
> >>
> >> // 1) Lookup via comp/env namespace of the initial/current context
> >>        EntityManager em = null;
> >>        InitialContext ctx = new InitialContext();
> >>        em = (EntityManager)ctx.lookup("java:comp/env/MyPCJndi");
> >>
> >> //  or 2) Use direct lookup from the injected session ctx of the session
> >> bean
> >>        em = (EntityManager)sessionCtx.lookup("MyPCJndi");
> >>
> >> }
> >>
> >> Looking up an EntityManagerFactory via @PersistenceUnit works similarly.
> >>
> >> Notice though, that lookup is via the ENC of the EJB.  I don't know
> >> how/if
> >> global JNDI can be used to get a factory. (Anyone?) So, if you
> initialize
> >> your POJO cache within the context of the EJB, the lookup will work.  If
> >> you
> >> need to initialize the cache outside of a container,
> >> Persistence.createEntityManagerFactory should work for you.  I'd verify
> >> that
> >> all of your jars specified in your jar-file entry are available on the
> >> classpath of your application.  You may need to add the jars to the
> class
> >> path entry in the manifest of your module.
> >>
> >> Here's an IBM infocenter document that explains how to configure an
> >> alternate persistence provider[1].  The key items when using an
> alternate
> >> version of OpenJPA is to make sure you've included all the necessary
> >> OpenJPA
> >> jars in your application or via shared library and the application
> >> classloader is configured to load PARENT_LAST.  This allows the OpenJPA
> >> jars
> >> provided by the application (or shared lib) to be loaded before those
> >> provided by the app server.
> >>
> >> [1]
> >>
> >>
> http://publib.boulder.ibm.com/infocenter/wasinfo/v6r1/index.jsp?topic=/com.ibm.websphere.ejbfep.multiplatform.doc/info/ae/ae/tejb_jpa3rdparty.html
> >>
> >> Hope this helps,
> >> -Jeremy
> >>
> >> On Sat, Jan 10, 2009 at 4:25 AM, devu213 <devusmail@gmail.com> wrote:
> >>
> >> >
> >> > Hi Kevin,
> >> >
> >> > Thanks for taking interest despite this being a Websphere question.
> >> Here
> >> is
> >> > the information that might help:
> >> >
> >> > 1. Yes am using the EJB3 featurepack. My server is at version 6.1.0.17
> >> with
> >> > EJB and Webservices FP installed.
> >> > 2. The code snippet Persistence.createEntityManager() works on
> >> stand-alone
> >> > even with the jar tags(but fails in the container).
> >> > 3. I am using the openJPA shipped with Websphere.
> >> >
> >> > Having given you the above it looks from your answer that I have erred
> >> by
> >> > not stating my question properly. The issue is, I don't know what name
> >> is
> >> > used to bind the factory and the manager. So the issue is not that I'm
> >> not
> >> > able to lookup but that I don't know the JNDI name to look up and so
> >> > haven't
> >> > even tried it. Somehow, I did not find any documentation on the
> >> Websphere
> >> > sites or google.
> >> >
> >> > Also on similar lines, I would like to see some documentation on how
> to
> >> > override the default openJPA shipped with WAS because I want to be
> able
> >> to
> >> > use the latest version but the one being picked up by the classloader
> >> is
> >> > always the 1.0 version.
> >> >
> >> > Thanks in advance.
> >> >
> >> >
> >> >
> >> > Kevin Sutter wrote:
> >> > >
> >> > > Hi devu213,
> >> > > Just to clarify...  Are you using the EJB3 Feature Pack on top of
> >> > > WebSphere
> >> > > v6.1.  Are you using the version of OpenJPA that ships with the
> >> Feature
> >> > > Pack, or something newer?  It sounds like you are using
> >> container-managed
> >> > > persistence (ie. the injection support), so it sounds like you are
> >> using
> >> > > the
> >> > > EJB3 Feature Pack.  But, I just want to make sure.
> >> > >
> >> > > With the EJB3 Feature Pack, the JNDI lookup of an EntityManager
> >> should
> >> > > work
> >> > > as well as the injection support.  You mention that you are getting
> a
> >> SAX
> >> > > parser error with your persistence.xml file.  Do you get any parsing
> >> > > errors
> >> > > when running OpenJPA standalone?  Or, is all of your testing within
> a
> >> > > container?  Can you provide more details on the specific parsing
> >> errors
> >> > > you
> >> > > are getting?
> >> > >
> >> > > You should also be able to use application-managed persistence
> within
> >> the
> >> > > Container.
> >> > >
> >> > > Off the top of my head, I'm not aware of any problems with what you
> >> are
> >> > > attempting.  Unfortunately, I can't highlight anything you're doing
> >> > > incorrectly.  If you can provide some additional details, maybe we
> >> can
> >> > > help
> >> > > get you running.
> >> > >
> >> > > FYI...  Just a reminder.  This forum is for questions and issues
> >> specific
> >> > > to
> >> > > OpenJPA.  Since some of us are intimate with WebSphere, we attempt
> to
> >> > help
> >> > > with related WebSphere questions.  In this particular case, it
> sounds
> >> > like
> >> > > you might be hitting a problem with the EJB Container within
> >> WebSphere.
> >> > > We
> >> > > can help with the triage of this case, but we won't be able to
> >> resolve
> >> > the
> >> > > problem with OpenJPA (if it turns out to be a problem with the
> >> > Container).
> >> > >
> >> > > Thanks,
> >> > > Kevin
> >> > >
> >> > > On Wed, Jan 7, 2009 at 2:40 AM, devu213 <devusmail@gmail.com>
> wrote:
> >> > >
> >> > >>
> >> > >> Hi,
> >> > >>
> >> > >> I need to lookup an entitymanager using JNDI on websphere 6.1.
My
> >> > >> persistence.xml is configured to use JPA. Although I use injection
> >> > (which
> >> > >> works) in almost all cases, I have a case where I would like the
> use
> >> of
> >> > >> entitymanager in a POJO and therefore would like to get it from
> JNDI
> >> > >> directly.
> >> > >>
> >> > >> The case is I have a cache which should be initialized with values
> >> from
> >> > >> the
> >> > >> database the first time it is accessed. The cache class is a POJO
> >> class.
> >> > >>
> >> > >> I don't seem to find any documentation regarding this. Is this
a
> >> > >> websphere
> >> > >> limitation ? Also, currently this class is trying to create a
> >> factory
> >> of
> >> > >> its
> >> > >> own within the container using
> >> > >> Persistence.createEntityManagerFactory(unitname) but that fails
as
> >> for
> >> > >> some
> >> > >> reason a SAX exception is thrown during parsing Persistence.xml
> file
> >> at
> >> > >> the
> >> > >> point where it tries to load the jar files containing the various
> >> entity
> >> > >> classes.
> >> > >>
> >> > >> So my second question is, is there some sort of restriction on
the
> >> use
> >> > of
> >> > >> Persistence.createEntityManager() inside the container?
> >> > >>
> >> > >> Have uploaded the class as well as the persistence.xml file.
> >> > >> http://n2.nabble.com/file/n2121433/ParamValuesCache.java
> >> > >> ParamValuesCache.java
> >> > http://n2.nabble.com/file/n2121433/persistence.xml
> >> > >> persistence.xml
> >> > >> --
> >> > >> View this message in context:
> >> > >>
> >> >
> >>
> http://n2.nabble.com/Entitymanager-lookup-using-JNDI-on-webshpere-tp2121433p2121433.html
> >> > >> Sent from the OpenJPA Users mailing list archive at Nabble.com.
> >> > >>
> >> > >>
> >> > >
> >> > >
> >> >
> >> > --
> >> > View this message in context:
> >> >
> >>
> http://n2.nabble.com/Entitymanager-lookup-using-JNDI-on-webshpere-tp2121433p2137356.html
> >> > Sent from the OpenJPA Users mailing list archive at Nabble.com.
> >> >
> >> >
> >>
> >
> >
>
> --
> View this message in context:
> http://n2.nabble.com/Entitymanager-lookup-using-JNDI-on-webshpere-tp2121433p2150209.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message