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

   If you're using Spring with EclipseLink you must copy spring-tomcat-weaver.jar into $CATALINA_HOME/server/lib see the Spring Manual for details.  Tomcat absolutely will not support dynamic weaving in Spring (or anything else for that matter) unless you use a technique like this.  You didn't mention you did this so don't think you did.  Once you have this in place and you've setup your Spring config properly, Spring will do @PersistenceContext injection.  Take a look at the TopLink Essentials JPA version of the Spring PetClinic for an example.


Tim Hollosy wrote:
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:
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



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()?


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:

<context:component-scan base-package="com.redacted" />

       <tx:annotation-driven mode="aspectj"/>

   <bean id="transactionManager"

       <bean id="dataSource"
               <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 id="jpaAdapter"
  <property name="databasePlatform"
  <property name="showSql" value="true" />

 <bean id="loadTimeWeaver"

 <bean id="entityManagerFactory"
      <property name="persistenceUnitName" value="ProofOConcept"/>
      <property name="dataSource" ref="dataSource"/>
      <property name="jpaVendorAdapter" ref="jpaAdapter"/>
      <property name="loadTimeWeaver" ref="loadTimeWeaver"/>


       <util:list id="annotatedClasses">

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;

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();


eclipselink-users mailing list

eclipselink-users mailing list

eclipselink-users mailing list


Shaun Smith | Principal Product Manager, TopLink | +1.905.502.3094
Oracle Fusion Middleware
110 Matheson Boulevard West, Suite 100
Mississauga, Ontario, Canada L5R 3P4

Back to the top