Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2014
    Posts
    7
    Rep Power
    0

    Default Injecting EJB and PersistenceContext in Jersey Servlet Container?

    Hello! I'm new to this forum and Java EE. I need help understanding the how implementations of JAX-RS, EJB and JPA tie in together. In particular, I recently changed my JAX-RS implementation to Jersey from Apache Wink and suddenly, my EntityManager is null and fields annotated with @EJB are not being injected.

    This is a similar problem someone else had: java ee - Injecting EJB, PersistenceContext into @Stateless Jersey Resource - Stack Overflow

    Here is my post: java ee - How to inject EntityManager in Jersey Container? - Stack Overflow [Note that I temporarily solved the @EJB issue with @InjectParam - but it is not preferable because I @InjectParam throws an exception when I use the interface as the type].

    In the beginning, everything "just worked" right out of the box - that is to say, when I did not touch the default implementations provided by Websphere Liberty Profile (you installed an eclipse plugin and during project creation, the library is automatically loaded for you).

    So my more concrete questions:
    1. How do I inject the PersistenceContext? I'm pretty desperate. I've been trying to get this to work for two days.
    2. Why did everything start failing? I understood that the major advantage of java was 'loose-coupling' in that implementations could be removed and inserted easily. So why did my application break when I replaced the JAX-RS implementation? As far as I can tell, I did not touch whatever the EJB and JPA implementations are. (As I understand it, JPA provides the PersistenceContext injection).


    Thank you very much!

  2. #2
    Join Date
    Aug 2014
    Posts
    7
    Rep Power
    0

    Default Re: Injecting EJB and PersistenceContext in Jersey Servlet Container?

    So I found a small work-around:

    @PostConstruct
    public void init() {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("ConsoleMan agement");
    em = emf.createEntityManager();
    }

    However; now I need to manage transactions myself by setting transaction-type to RESOURCE_LOCAL.

    Also: as I now understand it, Jersey is simply a servlet and the application server actually implements EJBs and injection. So one step forward and two steps back in that now I haven't even a clue why injection is not working particularly because I did not touch anything pertaining to it!

  3. #3
    Hevipeikko is offline Member
    Join Date
    Jul 2014
    Location
    Finland
    Posts
    2
    Rep Power
    0

    Default Re: Injecting EJB and PersistenceContext in Jersey Servlet Container?

    I had similar issue with WebSockets in Glassfish, though I did just used SomeBean someBean = (SomeBean) new InitialContext().lookup("java:global/app/SomeBean"); to access bean.

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,368
    Rep Power
    6

    Default Re: Injecting EJB and PersistenceContext in Jersey Servlet Container?

    That's how you lookup a bean in a class which is not managed by the server; basically it is a manual lookup through JNDI. Its not really a "nice" thing to do as you then have to hardcode the JNDI name in your code, but sometimes there is no way around it.

    The original problem of the OP is that injections stopped working in a resource which IS server managed (a JAX-RS service). That indicates a deeper failure that is not going to be solved with a manual JNDI lookup. Probably a configuration file was screwed up, we can't know as we don't have access to the server and the OP removed the related stackoverflow thread - probably because it was just a silly mistake.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Join Date
    Aug 2014
    Posts
    7
    Rep Power
    0

    Default Re: Injecting EJB and PersistenceContext in Jersey Servlet Container?

    Quote Originally Posted by gimbal2 View Post
    That's how you lookup a bean in a class which is not managed by the server; basically it is a manual lookup through JNDI. Its not really a "nice" thing to do as you then have to hardcode the JNDI name in your code, but sometimes there is no way around it.

    The original problem of the OP is that injections stopped working in a resource which IS server managed (a JAX-RS service). That indicates a deeper failure that is not going to be solved with a manual JNDI lookup. Probably a configuration file was screwed up, we can't know as we don't have access to the server and the OP removed the related stackoverflow thread - probably because it was just a silly mistake.
    Actually no this was never solved. I ended up going back to the IBM Servlet container. I'm by no means an expert but I only changed the servlet container class in web.xml and it started working so I don't believe I made a mistake in a configuration. I deleted it because I thought that the servlet container perhaps does [edit]not[/edit] provide an ejb injector (in this case because I was using jersey 1). This is backed by absolutely nothing except for the fact that I could find nothing that said jersey did support ejb.
    Last edited by 012anonymousxyz; 08-19-2014 at 11:29 PM.

  6. #6
    Join Date
    Aug 2014
    Posts
    7
    Rep Power
    0

    Default Re: Injecting EJB and PersistenceContext in Jersey Servlet Container?

    After some more looking, for the persistence context, additional configuration is required, as per the javadocs: https://jersey.java.net/apidocs/2.11...Container.html.
    I still haven't a clue for EJB.

Similar Threads

  1. Does WAMP include servlet container?
    By Ivan in forum Java Servlet
    Replies: 0
    Last Post: 09-13-2013, 03:06 PM
  2. Replies: 2
    Last Post: 05-14-2012, 12:56 PM
  3. Replies: 2
    Last Post: 12-22-2011, 12:49 PM
  4. Online JSP/Servlet container
    By an24 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-17-2011, 07:24 AM
  5. Replies: 2
    Last Post: 07-24-2009, 02:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •