Announcement Announcement Module
Collapse
No announcement yet.
No session or session was closed Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • No session or session was closed

    I'm trying to understand why I get this exception:

    Code:
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.stone.gym.domain.User.subscriptions, no session or session was closed
    	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
    	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
    	at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
    	at org.hibernate.collection.AbstractPersistentCollection.write(AbstractPersistentCollection.java:183)
    	at org.hibernate.collection.PersistentBag.add(PersistentBag.java:274)
    	at com.stone.gym.view.UserView$4.handleEvent(UserView.java:256)
    	at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
    	at org.eclipse.swt.widgets.Display.sendEvent(Unknown Source)
    	at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
    	at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
    	at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
    	at org.eclipse.swt.widgets.Widget.notifyListeners(Unknown Source)
    	at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
    	at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
    	at com.stone.gym.controller.FrontController.run(FrontController.java:29)
    	at com.stone.gym.StoneGym.main(StoneGym.java:22)
    When calling the save method from this DAO:

    Code:
    @Repository
    @Transactional
    
    public class UserDAOImpl implements UserDAO {
    
    	private EntityManager entityManager;
    
    	@PersistenceContext(unitName="stoneGym")
    	public void setEntityManager(EntityManager entityManager) {
    		this.entityManager = entityManager;
    	}
    
    	public List<User> getUsers() {
    		return entityManager.createQuery("from User user").getResultList();
    	}
    	
    	public void saveUser(User user) {
    		if( user.getId() == null)
    			entityManager.persist(user);
    		else 
    			entityManager.merge(user);
    	}
    
    	public void removeUser(User user) {
    		entityManager.remove(user);
    	}
    
    }
    This is my config:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd"
    	default-autowire="byName">
    	<bean id="stoneGym" class="com.stone.gym.StoneGym">
    		<constructor-arg ref="frontController"></constructor-arg>
    	</bean>
    	<bean id="personDao" class="com.stone.gym.dao.impl.UserDAOImpl" />
    	<bean id="programDao" class="com.stone.gym.dao.impl.ProgramDAOImpl" />
    	<bean id="frontController" class="com.stone.gym.controller.FrontController">
    	</bean>
    	<bean id="userController" class="com.stone.gym.controller.UserController">
    		<property name="userDao" ref="personDao"></property>
    		<property name="programDao" ref="programDao"></property>
    	</bean>
    	<bean id="settingsController" class="com.stone.gym.controller.SettingsController">
    		<property name="programDao" ref="programDao"></property>
    	</bean>
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    		destroy-method="close">
    		<!--   <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />
    		<property name="url" value="jdbc:derby://localhost:1527/stoneGym;create=true" />
    -->
    		<property name="driverClassName" value="org.h2.Driver" />
    		<property name="url" value="jdbc:h2:mem:test;" />
    	</bean>
    
    	<bean id="entityManagerFactory"
    		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="persistenceUnitName" value="stoneGym" />
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="showSql" value="true" />
    				<property name="databasePlatform" value="org.hibernate.dialect.H2Dialect" />
    			</bean>
    		</property>
    	    <property name="jpaProperties">
                <props>
                    <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
                    <prop key="hbm2ddl.auto">create-drop</prop>
                </props>
            </property>
    	</bean>
    
    	<bean
    		class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" />
    	<tx:annotation-driven transaction-manager="transactionManager" />	
    	
    </beans>
    Normally spring should take care of the session management and make sure a session is open right ? Any clues why this error occurs?

  • #2
    The problem is that your collection "subscriptions" is managed with lazy loading, therefore if you try to access it, the system notify you with such exception (i.e. transaction is completed). If you are in web environment you can use the OpenSessionInViewFilter.
    Try to search in the forum...

    Comment

    Working...
    X