Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EntityManager must be access within a transaction

 
rrowdy rathode
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi all,

I am currently working on Spring + JPA + JBOSS 6 integration and facing some issues in it.

Following is the error message in console when i use entityManager.persist

17:09:48,703 ERROR [STDERR] javax.persistence.TransactionRequiredException: EntityManager must be access within a transaction
17:09:48,703 ERROR [STDERR] at org.jboss.jpa.deployment.ManagedEntityManagerFactory.verifyInTx(ManagedEntityManagerFactory.java:157)
17:09:48,703 ERROR [STDERR] at org.jboss.jpa.deployment.PersistenceUnitDeployment.verifyInTx(PersistenceUnitDeployment.java:318)
17:09:48,703 ERROR [STDERR] at org.jboss.jpa.impl.tx.TransactionScopedEntityManager.verifyInTx(TransactionScopedEntityManager.java:105)
17:09:48,703 ERROR [STDERR] at org.jboss.jpa.impl.tx.TransactionScopedEntityManager.persist(TransactionScopedEntityManager.java:205)
17:09:48,703 ERROR [STDERR] at com.test.dao.LoginDao.persistUser(LoginDao.java:61)
17:09:48,703 ERROR [STDERR] at com.test.service.impl.LoginServiceImpl.persistUser(LoginServiceImpl.java:28)
17:09:48,703 ERROR [STDERR] at org.apache.jsp.jsp.login_jsp._jspService(login_jsp.java:103)
17:09:48,703 ERROR [STDERR] at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
17:09:48,703 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
17:09:48,703 ERROR [STDERR] at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
17:09:48,703 ERROR [STDERR] at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:326)
17:09:48,703 ERROR [STDERR] at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:253)
17:09:48,703 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
17:09:48,703 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324)
17:09:48,703 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242)
17:09:48,703 ERROR [STDERR] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
17:09:48,703 ERROR [STDERR] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
17:09:48,703 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181)
17:09:48,703 ERROR [STDERR] at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285)
17:09:48,703 ERROR [STDERR] at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261)
17:09:48,703 ERROR [STDERR] at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88)
17:09:48,703 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100)
17:09:48,703 ERROR [STDERR] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
17:09:48,703 ERROR [STDERR] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
17:09:48,703 ERROR [STDERR] at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
17:09:48,703 ERROR [STDERR] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
17:09:48,703 ERROR [STDERR] at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53)
17:09:48,703 ERROR [STDERR] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362)
17:09:48,703 ERROR [STDERR] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
17:09:48,703 ERROR [STDERR] at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654)
17:09:48,703 ERROR [STDERR] at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951)
17:09:48,703 ERROR [STDERR] at java.lang.Thread.run(Unknown Source)

Here is my persistence.xml



here is my applicationContext.xml



This is the service class



And this is my DAO

 
rrowdy rathode
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any kind of help on above error would be of great help....Thank you in advance
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You need that if you want to use @Transactional.


Also why are your beans scoped to prototype? I would remove that scope attribute altogether I can't think of very many good reasons why a DAO or service class should need to be scoped prototype.

A more typical configuration would be to use the LocalContainerEntityManagerFactoryBean

below is a very simplistic example googling around will result in more complete ones...



now where you define your datasource that would be what you would look up from jndi.

After Spring is done doing its thing you can use your entity manager in code like this



FYI you dont need getters and setters for the entity manger either.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic