我使用 Spring的声明式的事务,但是运行的时候报错:
代码
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)
我在配置文件中明明配置了事务的!:
代码
<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>
我在代码中注入的sessionFactory可以得到,说明IOC式起作用的,但是代码不能使用getCurrentSession方法,如果使用就报上面的错误!我的代码:
代码
public static List testquery(){
List list=new ArrayList();
TestUtil tu=new TestUtil();
Session hsession=tu.getSessionFactory().getCurrentSession();
list=hsession.createQuery("from UserImpl as u").list();
hsession.close();
return list;
}
把其中的 getCurrentSession改成 openSession 就可以使用! 很明显是因为那个 allowCreate的问题,当没有事务启动的时候,getCurrentSession是无法创建Session的! 说明配置事务没有成功!但是我的配置已经和例子一摸一样了啊~ 哪位朋友能帮忙找出错误? 谢谢!