Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [eclipselink-users] Spring: Attempting to execute an operation on a closed EntityManager.

	Hi, All these instructions below and on the Tomcat tutorial wiki are for configurations without a Spring container - weaving should be available in a Spring-Tomcat combination.
		A) Spring + Tomcat --> should support load time weaving
		A) Tomcat (only) --> no load time weaving

	From my own investigations into running EclipseLink on Tomcat 6 and JBoss 4/5  - they both are not able to dynamically weave entites during predeploy() - you will need to statically weave classes before jaring them into your war(Tomcat) or ejb.jar(JBoss).
	In regards to weaving - if Tomcat does not supply a temporary class loader to the weaver via PersistenceUnitInfo.getNewTempClassLoader() - only static weaving is possible.

	On my servlet in Tomcat I managed the entitymanager directly using the bootstrap class [Persistence] as there are no session beans available to do a CM injection of the EM via...

To test injection, I retrofited my Tomcat WAR to use EntityManager injection via @PersistenceContext like I do on all the other servers but I cannot get past the following Naming exception.
Normally I put the annotation on the session bean, in tomcat this bean is just a class.  I also put the annotation directly on the servlet - same exception.
The only way I found to get the entitymanager on tomcat is to use the bootstrap class ersistence.createEntityManagerFactory(JTA_PU_NAME).

	private EntityManager entityManager;

instead of....
    // Local application managed EMF and EM
    public EntityManagerFactory emf  = null;
    public EntityManager entityManager = null;

    public ApplicationService() {
        emf  = Persistence.createEntityManagerFactory(JTA_PU_NAME);
        entityManager = emf.createEntityManager();

causes the following exception in Tomcat 6

[EL Finer]: 2008.09.26 10:19:28.521--Thread(Thread[http-8080-3,5,main])--JavaSECMPInitializer - transformer is null.
[EL Finest]: 2008.09.26 10:19:28.521--ServerSession(5035392)--Thread(Thread[http-8080-3,5,main])--Begin predeploying Persistence Unit statJPA; state Predeployed; factoryCount 0
[EL Finest]: 2008.09.26 10:19:28.521--ServerSession(5035392)--Thread(Thread[http-8080-3,5,main])--End predeploying Persistence Unit statJPA; state Predeployed; factoryCount 1
Sep 26, 2008 10:19:28 AM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet FrontController as unavailable
Sep 26, 2008 10:19:28 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet FrontController
javax.naming.NameNotFoundException: Name org.eclipse.persistence.example.unified.presentation.FrontController is not bound in this Context
	at org.apache.naming.NamingContext.lookup(
	at org.apache.naming.NamingContext.lookup(
	at org.apache.catalina.util.DefaultAnnotationProcessor.lookupFieldResource(
	at org.apache.catalina.util.DefaultAnnotationProcessor.processAnnotations(
	at org.apache.catalina.core.StandardWrapper.loadServlet(
	at org.apache.catalina.core.StandardWrapper.allocate(
	at org.apache.catalina.core.StandardWrapperValve.invoke(
	at org.apache.catalina.core.StandardContextValve.invoke(
	at org.apache.catalina.core.StandardHostValve.invoke(
	at org.apache.catalina.valves.ErrorReportValve.invoke(
	at org.apache.catalina.core.StandardEngineValve.invoke(
	at org.apache.catalina.connector.CoyoteAdapter.service(
	at org.apache.coyote.http11.Http11Processor.process(
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(

However, this exception when using @PersistenceContext in the servlet may just need to be prefixed differently - it looks like tomcat cannot find the servlet context when translating the @PersistenceContext annotation.
Injection of the pu may be possible in Tomcat 6, I just have to go through the new java/org/apache/catalina/util/ class

	The upcoming JBoss 5.0CR2 release in two weeks is fully JEE5 compliant so I will revisit weaving and adjust the tutorial page if its implementation of javax.persistence is fixed.

	Hope this helps you a bit
	thank you


-----Original Message-----
From: Tim Hollosy [mailto:hollosyt@xxxxxxxxx]
Sent: Friday, September 26, 2008 08:55
To: EclipseLink User Discussions
Subject: Re: [eclipselink-users] Spring: Attempting to execute an
operation on a closed EntityManager.

So no luck em.isOpen returns true.

I can't get my friggin AspectJ Weaver to output anything even though I
have an aop.xml like this:

"-//AspectJ//DTD//EN" "";>
    <weaver options="-debug -verbose -showWeaveInfo
        <include within="com.redacted.*"/>

BUT, doing some more reading about weaving I see: at

As Tomcat is not a JEE5 compatible server, there are some limitiations to JPA.

    * No dynamic weaving (instrumentation) - static weaving of
entities is still available via EclipseLink
    * No @EJB injection of a session bean (containing the
EntityManager) is available - use the persistence factory and manager
    * No @PersistenceContext injection of a container managed
persistence unit is available - use

Does that mean i can't use the @PersistenceContext annotation with
EclipseLink in Tomcat 6???

I'm confused as to weather Tomcat 6 supports LTW or not, since there
are instructions all over for getting it to weave with Spring...

I may set up an OC4J Instance and see if I have better luck, but right
now I'm confused as all get out.


On Fri, Sep 26, 2008 at 6:58 AM, Tim Hollosy <hollosyt@xxxxxxxxx> wrote:
> Mohsen,
> I didn't actually call isOpen, I instead inspected it with the
> debugger, and noticed the isOpen boolean was true. I'll try your
> obvious suggestion when I get in to the office this morning.
> My hunch is something odd is going on with weaving, so I'm going to
> put some weaving logging in as well.
> I guess this is just growing pains and it doesn't always help when I
> choose bleeding edge -- pure annotations & eclipselink for my first
> foray into Spring -- but I just can't stand XML, so I've made my bed
> :)
> Thanks
> ./tch
> On Fri, Sep 26, 2008 at 6:54 AM, Mohsen Saboorian <mohsens@xxxxxxxxx> wrote:
>> I didn't test it with EclipseLink, but with Hibernate session is
>> closed after transaction is committed, and it produces a lot of
>> frustrations with lazy loading, since you usually need to fetch a list
>> after a transaction is committed.
>> Spring provides a OpenSessionInViewFilter to work around this. It
>> keeps session open durig a request life-cycle.
>> How did you verified that EM is open? EM.isOpen()?
>> Mohsen.
>> On Thu, Sep 25, 2008 at 9:58 PM, Tim Hollosy <hollosyt@xxxxxxxxx> wrote:
>>> I'm hoping some Spring gurus can help me out, whenever I try to
>>> execute a query I get the error: Attempting to execute an operation on
>>> a closed EntityManager.
>>> I'm using Spring 2.5 + Tomcat6.
>>> The weird thing is find's work fine. I'm launching tomcat with the
>>> spring-aspects.jar for weaving. I'm new to this whole spring web stuff
>>> so I am probably missing something, but the only thing I could find
>>> online was to make sure I have the @Transactional annotations used
>>> everywhere in my DAO class, which I do.
>>> If I run in the debugger it looks like the EM is open as well, so I'm
>>> kind of stumped. I suspect some weaving shenanigans, but I'm too much
>>> of a neophyte at this right now to debug much more.
>>> Many thanks :)
>>> Here's my config:
>>> applicationcontext.xml;
>>> <context:component-scan base-package="com.redacted" />
>>>        <tx:annotation-driven mode="aspectj"/>
>>>    <bean id="transactionManager"
>>> class="org.springframework.orm.jpa.JpaTransactionManager"
>>>          p:entityManagerFactory-ref="entityManagerFactory"/>
>>>        <bean id="dataSource"
>>>                class="org.apache.commons.dbcp.BasicDataSource">
>>>                <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
>>>                <property name="url"
>>> value="jdbc:oracle:thin:@redacted:1521:redacted" />
>>>                <property name="username" value="redacted" />
>>>                <property name="password" value="redacted" />
>>>        </bean>
>>>        <bean id="jpaAdapter"
>>> class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
>>>   <property name="databasePlatform"
>>> value=""
>>> />
>>>   <property name="showSql" value="true" />
>>> </bean>
>>>  <bean id="loadTimeWeaver"
>>> class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
>>>  <bean id="entityManagerFactory"
>>> class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
>>>       <property name="persistenceUnitName" value="ProofOConcept"/>
>>>       <property name="dataSource" ref="dataSource"/>
>>>       <property name="jpaVendorAdapter" ref="jpaAdapter"/>
>>>       <property name="loadTimeWeaver" ref="loadTimeWeaver"/>
>>>    </bean>
>>>        <util:list id="annotatedClasses">
>>>                <value>com.redacted.*</value>
>>>        </util:list>
>>> Here's my DAO class:
>>> import java.util.List;
>>> import javax.persistence.EntityManager;
>>> import javax.persistence.PersistenceContext;
>>> import javax.persistence.Query;
>>> import org.eclipse.persistence.jpa.JpaEntityManager;
>>> import org.eclipse.persistence.jpa.JpaHelper;
>>> import org.springframework.stereotype.Repository;
>>> import org.springframework.transaction.annotation.Transactional;
>>> @Transactional
>>> @Repository
>>> public class BasicDao {
>>>        @PersistenceContext(unitName = "ProofOConcept")
>>>        private EntityManager em;
>>>        @Transactional(readOnly = true)
>>>        public <T> T find(Class<T> entityClass, Object primaryKey) {
>>>                T result = em.find(entityClass, primaryKey);
>>>                return result;
>>>        }
>>>        @Transactional(readOnly = true)
>>>        public <T> List<T> selectAll(Class<T> clazz) {
>>>                JpaEntityManager jpaEm = JpaHelper.getEntityManager(em);
>>>                Query query = jpaEm.createQuery(null, clazz);
>>>                return (List<T>) query.getResultList();
>>>        }
>>> }
>>> ./tch
>>> _______________________________________________
>>> eclipselink-users mailing list
>>> eclipselink-users@xxxxxxxxxxx
>> _______________________________________________
>> eclipselink-users mailing list
>> eclipselink-users@xxxxxxxxxxx
eclipselink-users mailing list

Back to the top