JiveJdon Community Forums
在线376人   首页   主题总表   培训咨询   精华   查搜   注册    登陆
首页 » 论坛 » J2EE/JavaEE/JEE/EJB/JSF等技术讨论
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表
???en_US.forumThreadNext.name??? 下一主题
Go 总共有 1 回复 / 1
 发表新帖子   回复该主题贴
lyojbuilder

悄悄话
发表文章: 195
注册时间: 2003年03月14日 00:34
spring的事务不起作用?无法和当前sessin绑定? 2007年06月05日 15:43 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
标签列表
我使用 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的! 说明配置事务没有成功!但是我的配置已经和例子一摸一样了啊~ 哪位朋友能帮忙找出错误? 谢谢!
banq

悄悄话
发表文章: 9312
注册时间: 2002年08月03日 17:08
回复:spring的事务不起作用?无法和当前sessin绑定? 2007年06月06日 15:03 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
>getCurrentSession改成 openSession 就可以使用
getCurrentSession和openSession 是很不同的,常用的是openSession。

你必须使用SpringHibernateSupport类来使用Hibernate,这样最方便,否则以后总是需要协调SpringHibernate的Session,非常麻烦。

可以说:Spring+Hibernate只有一条正确使用之道,其他方式也可以,但是非常困难麻烦,这也是框架的优点或者缺点。
这个主题有 1 回复 / 1Go
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表    返回页首返回页首
???en_US.forumThreadNext.name??? 下一主题
热点TAG: AOP cache 缓存 DDD EJB 集群 设计模式 Hibernate IOC JiveJdon OO RBAC Seam Spring Struts
正在读取,请等待...
google yahoo 新浪ViVi 365Key网摘 天极网摘 CSDN网摘 添加到百度搜藏 POCO网摘 博采网摘
查询本论坛内 回复超过的热门帖子
     回复该主题贴
标题
 
粗体 斜体 下划线 插入图片 插入代码 插入url链接 插入附件
内容
 

手机阅读 add to google add to yahoo
解惑之道在J道 ,打造中国最具影响力的的企业软件社区
OpenSource JIVEJDON v3.0 Powered by JdonFramework Code © 2002-08 jdon.com
anti spam