Results 1 to 4 of 4

Thread: Spring transaction don't work? not allow creation of non-transactional

  1. #1
    Join Date
    Sep 2004
    Posts
    5

    Exclamation Spring transaction don't work? not allow creation of non-transactional

    Hi everyone:

    I want to use spring declare transaction.when I run my jsp,tomcat throw exception:
    Code:
    org.hibernate.HibernateException: No Hibernate Session bound to thread, and conf
    iguration does not allow creation of non-transactional one here
            at org.springframework.orm.hibernate3.AbstractSessionFactoryBean$Transac
    tionAwareInvocationHandler.invoke(AbstractSessionFactoryBean.java:296)
            at $Proxy1.getCurrentSession(Unknown Source)
            at com.liferay.portlet.lyo.service.persistence.TestUtil.testquery(TestUt
    il.java:54)
            at org.apache.jsp.html.portlet.login.view_jsp._jspService(view_jsp.java:
    316)
    My spring config is:
    Code:
    <bean id="com.liferay.portlet.lyo.service.persistence.TestUtil" class="com.liferay.portlet.lyo.service.persistence.TestUtil" lazy-init="true">
    		
    		<property name="sessionFactory">
    			<ref bean="liferaySessionFactory" />
    		</property>
    	</bean>
    <bean id="myProductService"
          class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true">
        <property name="transactionManager" ref="liferayTransactionManager"/>
        <property name="target">
          <bean class="com.liferay.portlet.lyo.service.persistence.TestUtil">
          </bean>
        </property>
        <property name="transactionAttributes">
          <props>
            <prop key="test*">PROPAGATION_REQUIRED,readOnly</prop>
          </props>
        </property>
      </bean>
    My java code:
    Code:
    public  static List testquery(){
    		List list=new ArrayList();
    		TestUtil tu=new TestUtil();
    		Session hsession=tu.getSessionFactory().getCurrentSession();
    		list=hsession.createQuery("from com.liferay.portal.model.impl.UserImpl as u").list();
    		hsession.close();
    		return list;
    		
    	}
    My test code:
    Code:
    org.springframework.context.ApplicationContext ctx
            = new org.springframework.context.support.ClassPathXmlApplicationContext(new String []{"META-INF/ext-spring-professional.xml","classpath*:META-INF/portal-spring-professional.xml","classpath*:META-INF/counter-spring-professional.xml"});
    		Object objBean=ctx.getBean("com.liferay.portlet.lyo.service.persistence.TestUtil");
    		out.println("get bean: "+objBean);
    com.liferay.portlet.lyo.service.persistence.TestUtil th=(com.liferay.portlet.lyo.service.persistence.TestUtil)objBean;
    java.util.List list=th.testquery();
    If I change the "getCurrentSession()" to "openSession()" ,it could work property.So the reason is that the transaction don't take effect. But why?
    Is there any mistake in my code? Thks!

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    static methods aren't being intercepted, change your signature to be non static. Also why are you creating a new TestUtil inside of your testUtil class?! A strange test imho...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Mar 2007
    Posts
    515

    Default

    You are using an unproxied instance of your bean: 'com.liferay.portlet.lyo.service.persistence.TestU til', instead of 'myProductService' which has transactional behavior.

  4. #4
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    You do need to look up the trasnsactional bean instead, but as Marten said if you create an instance using the new operator then this won't work. So remove this and the static on the method.
    Code:
    TestUtil tu=new TestUtil();

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •