Announcement Announcement Module
Collapse
No announcement yet.
Spring transaction don't work? not allow creation of non-transactional Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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...

    Comment


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

      Comment


      • #4
        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();

        Comment

        Working...
        X