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

  • LazyInitializationException

    Hi,
    I changed my XML in accordance with the book "Spring Live":

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    	
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="configLocation">
        		<value>hibernate.cfg.xml</value>
    		</property>
    		
    	</bean>
    	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory">
    			<ref local="sessionFactory"/>
    		</property>
    	</bean>
    	<bean id="eventoDAO" class="br.gov.pa.tj.evento.dao.EventoDAOImpl">
    		<property name="sessionFactory">
    			<ref local="sessionFactory"/>
    		</property>
    	</bean>
    	<bean id="eventoFacade" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager">
    			<ref local="transactionManager"/>
    		</property>
    		<property name="target">
    			<ref local="eventoFacadeTarget"/>
    		</property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="inserir*">PROPAGATION_REQUIRED</prop>
    				<prop key="alterar*">PROPAGATION_REQUIRED</prop>
    				<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    			</props>
    		</property>
    	</bean>
    	<bean id="eventoFacadeTarget" class="br.gov.pa.tj.evento.facade.EventoFacadeImpl">
    		<property name="eventoDAO">
    			<ref local="eventoDAO"/>
    		</property>
    	</bean>
    </beans>
    With this, my method insert functions:

    Code:
    public class EventoFacadeImplTest extends TestCase &#123;
    	private ApplicationContext ctx;
    	private EventoFacade efacade;
    	private Evento evento;
    
    	protected void setUp&#40;&#41; throws Exception &#123;
    		String&#91;&#93; paths = &#123; "spring-hibernate.xml" &#125;;
    		ctx = new ClassPathXmlApplicationContext&#40;paths&#41;;
    		efacade = &#40;EventoFacade&#41; ctx.getBean&#40;"eventoFacade"&#41;;
    	&#125;
    
    	protected void tearDown&#40;&#41; throws Exception &#123;
    		evento = null;
    		efacade = null;
    	&#125;
    
    	public void testInserir&#40;&#41; throws Exception&#123;
    		evento = new Evento&#40;&#41;;
    		evento.setComponente&#40;"componente"&#41;;
    		evento.setDescricao&#40;"descricao"&#41;;
    		evento.setIcone&#40;"icone"&#41;;
    		
    		evento = &#40;Evento&#41; efacade.inserir&#40;evento&#41;;
    		
    		assertTrue&#40;evento.getId&#40;&#41;!=null&#41;;
    	&#125;
    But in my method update occur an exception LazyInitializationException:

    Method update:
    Code:
    	public void testUpdate&#40;&#41; throws Exception &#123;
    		evento = &#40;Evento&#41; efacade.selecionar&#40;new Integer&#40;4&#41;&#41;;
    		evento.setComponente&#40;"novo componente"&#41;;
    		
    		evento = &#40;Evento&#41; efacade.alterar&#40;evento&#41;;
    		
    	&#125;
    Exception:
    Code:
    09/08/2005 10&#58;32&#58;06 org.hibernate.LazyInitializationException <init>
    SEVERE&#58; could not initialize proxy - the owning Session was closed
    org.hibernate.LazyInitializationException&#58; could not initialize proxy - the owning Session was closed
    	at org.hibernate.proxy.AbstractLazyInitializer.initialize&#40;AbstractLazyInitializer.java&#58;53&#41;
    	at org.hibernate.proxy.AbstractLazyInitializer.getImplementation&#40;AbstractLazyInitializer.java&#58;84&#41;
    	at org.hibernate.proxy.CGLIBLazyInitializer.intercept&#40;CGLIBLazyInitializer.java&#58;134&#41;
    	at br.gov.pa.tj.evento.classe.Evento$$EnhancerByCGLIB$$a0abedcd.setComponente&#40;<generated>&#41;
    	at br.gov.pa.tj.evento.testes.EventoFacadeImplTest.testUpdate&#40;EventoFacadeImplTest.java&#58;52&#41;
    	at sun.reflect.NativeMethodAccessorImpl.invoke0&#40;Native Method&#41;
    	at sun.reflect.NativeMethodAccessorImpl.invoke&#40;Unknown Source&#41;
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke&#40;Unknown Source&#41;
    	at java.lang.reflect.Method.invoke&#40;Unknown Source&#41;
    	at junit.framework.TestCase.runTest&#40;TestCase.java&#58;154&#41;
    	at junit.framework.TestCase.runBare&#40;TestCase.java&#58;127&#41;
    	at junit.framework.TestResult$1.protect&#40;TestResult.java&#58;106&#41;
    	at junit.framework.TestResult.runProtected&#40;TestResult.java&#58;124&#41;
    	at junit.framework.TestResult.run&#40;TestResult.java&#58;109&#41;
    	at junit.framework.TestCase.run&#40;TestCase.java&#58;118&#41;
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests&#40;RemoteTestRunner.java&#58;478&#41;
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run&#40;RemoteTestRunner.java&#58;344&#41;
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main&#40;RemoteTestRunner.java&#58;196&#41;
    Someone can help me?
    Regards,

  • #2
    The problem here is that each method call into the facade object is creating a new transaction and a new session. You can get around this in a variety of ways in Spring. For a unit test, you should use the Spring test classes as shown in Chapter 22 of the reference manual. This will open a transaction for your entire test and Hibernate session will associated with this transaction and will stay open for the entire test.

    Rob

    Comment


    • #3
      Originally posted by robh
      The problem here is that each method call into the facade object is creating a new transaction and a new session. You can get around this in a variety of ways in Spring

      Rob
      which are those variety ways? I need long sessions@service layer...

      Comment


      • #4
        For session management in the service layer you can either perform all your processing inside of transaction that has the Session bound it (like with Spring-managed transactions), or you can use the Open Session In View pattern. Check out the JavaDoc for OpenSessionInFilter for more information.

        Rob

        Comment


        • #5
          re: opensessioninview

          Originally posted by robh
          For session management in the service layer you can either perform all your processing inside of transaction that has the Session bound it (like with Spring-managed transactions), or you can use the Open Session In View pattern. Check out the JavaDoc for OpenSessionInFilter for more information.

          Rob
          do you mean openSessionInViewFiletr? probably...

          The problem is, that I can't use it @ service layer...
          deducted from the name, it'appropriate for MVC, but I'm writing services- without view (gui) and M. and C.

          Comment


          • #6
            Then take a look at OpenSessionInViewInterceptor.

            Comment


            • #7
              Originally posted by costin
              Then take a look at OpenSessionInViewInterceptor.
              OK, I've read the api, but I'm still confused about the 'request' words... at the level I need single sessions I have no requests at all!

              Comment


              • #8
                See the samples from the official distribution - they are very useful. Search the forums also to see what questions were previously answered.

                Comment


                • #9
                  Originally posted by costin
                  See the samples from the official distribution - they are very useful. Search the forums also to see what questions were previously answered.
                  Ok, I've looked at examples, and discovered, that OpenSessionInView is not applicable at service layer(at least in my case). It needs a request, etc.. Moreover it sets automatically hibernate's flush mode to 'never', which can cause problems using jdbctemplate & hibernatetemplate together (for example, hibernate cumulates the inserts in session, while a jdbctemplates's select assumes data's have been flushed).

                  Comment

                  Working...
                  X