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.

Tim,
	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.

	Tomcat:
	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...
@PersistenceContext(unitName="unified",type=PersistenceContextType.TRANSACTION)

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).


Using...
	@PersistenceContext(unitName="statJPA",type=PersistenceContextType.TRANSACTION)	
	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(NamingContext.java:770)
	at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
	at org.apache.catalina.util.DefaultAnnotationProcessor.lookupFieldResource(DefaultAnnotationProcessor.java:208)
	at org.apache.catalina.util.DefaultAnnotationProcessor.processAnnotations(DefaultAnnotationProcessor.java:152)
	at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1119)
	at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:808)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
	at java.lang.Thread.run(Thread.java:619)


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/DefaultAnnotationProcessor.java class

	JBoss:
	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

	/michael

-----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:

<!DOCTYPE aspectj PUBLIC
"-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd";>
<aspectj>
    <weaver options="-debug -verbose -showWeaveInfo
-XmessageHandlerClass:org.springframework.aop.aspectj.AspectJWeaverMessageHandler">
        <include within="com.redacted.*"/>
    </weaver>
     <aspects>
        <aspect
name="org.springframework.transaction.aspectj.AnnotationTransactionAspect"/>
    </aspects>
</aspectj>


BUT, doing some more reading about weaving I see: at
http://wiki.eclipse.org/EclipseLink/Examples/JPA/Tomcat_Web_Tutorial#Limitations_to_JPA

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
directly
    * No @PersistenceContext injection of a container managed
persistence unit is available - use
Persistence.createEntityManagerFactory(JTA_PU_NAME)


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.

./tch



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="org.eclipse.persistence.platform.database.oracle.OraclePlatform"
>>> />
>>>   <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
>>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>>>
>> _______________________________________________
>> eclipselink-users mailing list
>> eclipselink-users@xxxxxxxxxxx
>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>>
>
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


Back to the top