Inject EntityManagerFactory in a servlet dispatcher (non managed)

I want to create a servlet dispatcher capable of doing some JPA operations. This servlet must not have any dependencies on Spring or EJB. So, my intention is to inject EntityManagerFactory to it programatically. Then, the dispatcher must handle transactions and create/open/close EntityManager when required.

So I did this code in a sample application, using Spring 3 and EclipseLink:

public class Initializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext context) { AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext(); appContext.scan(...); appContext.refresh(); EntityManagerFactory emf = appContext.getBean(EntityManagerFactory.class); MyDispatcher dispatcher = new MyDispatcher(emf); context.addServlet("my", dispatcher).addMapping("/my/*"); } } public class MyDispatcher extends HttpServlet { ... protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { emf.createEntityManager(); // java.lang.IllegalStateException: Attempting to execute an operation on a closed EntityManagerFactory. } }

The problem is when this dispatcher needs to create an EntityManager an IllegalStateExceptionis thrown. Debugging I have seen that EntityManagerFactoryImpl.close() is called automatically after all beans are instantiated.

What am I doing wrong? Is there some way of achieving what I need?


Category:java Views:0 Time:2012-01-23

Related post

  • Servlet Dispatcher throwing exception 2010-02-14

    I have a simple Spring web flow application created. When the application starts, I get the following error on my Tomcat server: SEVERE: Servlet.service() for servlet dispatcher threw exception java.lang.NoSuchFieldError: WEB_APPLICATION_CONTEXT_ATTR

  • My properties are not injected in my perso-servlet.xml (Maven2) 2010-09-07

    I don't understand why my properties are not injected in my perso-servlet.xml : <!-- Facebook OAuth helper --> <bean id="FacebookOAuthHelper" class="com.myapp.businessservices.common.facebook.FacebookOAuthHelper" p:apiKey="${facebook.apiKey}

  • Servlet dispatcher is currently unavailable 2010-12-22

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="" xmlns:xsi="" xmlns:p="" xsi:schemaLocation="http://www.springfr

  • HTTP Status 404 - Servlet dispatcher is not available 2010-12-22

    <project xmlns="" xmlns:xsi="" xsi:schemaLocation=""> <modelVersion>4.0.0</modelVers

  • Injecting jms resource in servlet & best practice for MDB 2010-05-03

    using ejb 3.1, servlet 3.0 (glassfish server v3) Scenario: I have MDB that listen to jms messages and give processing to some other session bean (Stateless). Servelet injecting jms resource. Question 1: Why servlet can`t inject jms resources when the

  • EJB not getting injected into the tested servlet 2010-11-04

    I am using Cactus to test a servlet which has an injected ejb (EJB 3.0) using @EJB annotation. The servlet is working fine when executed as part of the web application but while running the cactus test it is failing to inject the ejb. I am getting a

  • CDI Injection Outside Filters and Servlets in a Servlet 3.0 Container 2011-08-20

    I finally decided to have a look at Weld in Tomcat. When I deploy my app I see in the log: "Tomcat 7 detected, CDI injection will be available in Servlets and Filters" How can for example create an instance of a bean using the BeanManager outside a F

  • Servlet - Dispatching request when the url-patern is like 2010-11-28

    I have some questions about dispatching a request in a servlet. To sum it up, I deployed a website on a public server which forces me to have a url-pattern like /servlet/* for all my servlets (I heard that it was a default configuration anyway). The

  • Servlet : After servlet Dispatch servlet action name in url 2011-05-06

    I created a simple login page. If the user entered right username and password the page ill be redirected to the success page otherwise it will be redirected to the index page. In login page i given the form submit action to the servlet. Once the ser

  • Why can't I inject 2 EJB's into 2 different managed beans that inject each other? 2011-02-11

    ContactsBean @Named(value = "contactsBean") @SessionScoped public class ContactsBean implements Serializable { @EJB ContactsFacade contactsEJB; private List<Contacts> contacts = new ArrayList<Contacts>(); @Inject DetailsBean detailsBean;

  • Inject JSF request parameter in a bean managed by Spring using annotations 2014-03-19

    Right now I use the f:viewParam tag to inject the request parameter into a field of my bean <f:viewParam name="id" value="#{}" /> But I would much rather prefer to use annotations for this. I know about the @Value annotation,

  • Injecting EntityManager Vs. EntityManagerFactory 2009-08-21

    A long question, please bear with me. We are using Spring+JPA for a web application. My team is debating over injecting EntityManagerFactory in the GenericDAO (a DAO based on Generics something on the lines provided by APPFUSE, we do not use JpaDaosu

  • Spring 2.5 managed servlets: howto? 2010-04-14

    Correct me if anything is wrong. As I understand, all Spring functionality, namely DI works when beans are got thru Spring Context, ie getBean() method. Otherwise, none can work, even if my method is marked @Transactional and I will create the owning

  • JSFUnit issue - EJB not getting injected into servlet being tested 2010-11-03

    In my application I have an EntryServlet which loads default data and sets up the security based on the logged in user id. This servlet is using EJB's through dependency injection. It works fine when I execute it as a normal web application but when

  • Injecting same EJB into two different managed beans not working 2011-06-30

    I have a stateless EJB and two different SessionScoped managed beans. e.g. EJB as follows - @Stateless public classs MyEjb implements MyEjbLocal { @PersistentContext(unitName="myPU") private EntityManager em; and a managed bean - @ManagedBean @Sessio

  • How to inject Hibernate Interceptor class in entityManagerFactory 2014-02-27

    I want to inject hibernate Interceptor class in my entity manager factory class. But I couldn't find any property in which I can inject hibernate Intercpetor class reference. Entity Manager Factory Bean: <bean id="entityManagerFactory" class="org.

  • How to inject ConversationScoped beans in a Servlet 2011-01-28

    I need to inject a ConversationScoped bean into a servlet. i use the standard simple @Inject tag and I invoke the servlet with the cid parameter but when it invokes any method in the injected bean I get the following error: org.jboss.weld.context.Con

  • GWT - GIN - GWTP - Dispatcher Injection Problem 2011-09-13

    I am in a situation where someone might be already in. I am using GWTP in my application. GWTP is using GIN as a client side dependency injection. GWTP also uses Dispatcher mechanism for sending request to server side. Now, There are some classes (PR

  • Weld CDI on Google App Engine: Injection in servlet not happening 2012-05-01

    This is my first time working with GAE and I'm trying to get CDI working. Long story short: The @Inject field in my servlet is not getting injected (it's always null). I'm working in Eclipse and debug the application on the local test server included

Copyright (C), All Rights Reserved.

processed in 0.168 (s). 11 q(s)