Hi everyone:
I want to use spring declare transaction.when I run my jsp,tomcat throw exception:
My spring config is: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 java code: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 test 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; }
If I change the "getCurrentSession()" to "openSession()" ,it could work property.So the reason is that the transaction don't take effect. But why?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();
Is there any mistake in my code? Thks!


Reply With Quote