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.

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
>


Back to the top