I have HttpServlet and it has a doPost() method in which a spring bean is used. That spring has a EntityManager defined with @PersistenceContext() defined. When the doPost() method receives a request, I get during runtime
javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call
Code is something like this (pardon for writing the skeleton code):
class MyServlet extends HttpServlet {
@Autowired
SpringBean springBean;
@Override
public void init(ServletConfig config) throws javax.servlet.ServletException{
super.init(config);
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
@Override
doPost() {
springBean.foo();
}
.
.
.
}
@Service
class SpringBean {
@PersistenceContext(unitName = someUnitName)
EntityManager entityManager;
public void foo(){
entityManager.persist(someEntity);
}
}
web.xml has the required parameters for component scan and ContextLoaderListener to make spring context available servlet. (There is an other bean without EntityManager and that's working fine)