用Spring进行事务管理一直没有效果呢?不知道错在哪里?~~

cart55free99 2011-03-05 07:10:02

只有手动进行事务管理才有数据进入, 我觉得可能代码上没有问题吧,
一开始以为是 事务没有配置好 但是查看了几遍, 也没看到什么错。

我是这样想的
用一个BaseDAO 作为父类 其他DAO均extends这个父类
该父类有sessionFactory属性 并有相应的get set方法
那么BaseDAO 的子类应该也有sessionFactory对象了吧

我这里贴出了配置文件 和 2个 BaseDAO 和StuDAOImpl

是在不知道哪里会错啊

首先是application.xml ===========================================================

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">



<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="search*" propagation="SUPPORTS" />
<tx:method name="show*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="exp1" expression="execution(* dao.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="exp1" />
</aop:config>
<!--事务管理配置结束 -->




<bean id="baseDAO" class="dao.BaseDAO">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="userDAO" class="dao.impl.TbUserDAOImpl">
</bean>

<!-- stu -->
<bean id="stuDAO" class="dao.impl.TbStuDAOImpl">
</bean>
<bean id="stuAction" class="action.StuAction">
<property name="stuDAO" ref="stuDAO" />
</bean>

<!-- tea -->
<bean id="teaDAO" class="dao.impl.TbTeaDAOImpl">
</bean>
<bean id="teaAction" class="action.TeaAction">

</bean>


<!-- admin -->
<bean id="AdminAction" class="action.AdminAction">
<property name="stuDAO" ref="stuDAO" />
</bean>
</beans>


baseDAO==================================

public class BaseDAO{
private SessionFactory sessionFactory;

public SessionFactory getSessionFactory() {
return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

public Session getSession() {
return HibernateSessionFactory.getSession();
}

}

stuDAOImpl==========================================================

public class TbStuDAOImpl extends BaseDAO implements TbStuDAO {

public void save(TbStu transientInstance) {
getSession().save(transientInstance);
}
....
}

...全文
139 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
cart55free99 2011-03-06
  • 打赏
  • 举报
回复
我基本上是从 http://www.360doc.com/content/10/0312/16/968587_18497905.shtml
这里照搬 怎么弄dataSource? 那不是和hibernate.cfg.xml 中的配置重复了?
magicluo 2011-03-06
  • 打赏
  • 举报
回复
你的sessionFactory中没有配置dataSource嘛?

奇怪你怎么能手动提交成功的?
cart55free99 2011-03-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zuxianghuang 的回复:]

数据访问类要继承org.springframework.orm.hibernate3.support.HibernateDaoSupport
[/Quote]
我的dao都是自己写的, 不是自动生成 , 继承这个之后sessionFactory 就没有对应的get方法了
因为HibernateDaoSupport 有一个同名字的final方法。

而且我已经有很多个DAO了
小蝸牛 2011-03-06
  • 打赏
  • 举报
回复
hibernateDaoSupport是必须要继承的!!!
cart55free99 2011-03-06
  • 打赏
  • 举报
回复
我知道了

public Session getSession() {
return sf.getCurrentSession();
}

不能直接openSession
k10509806 2011-03-05
  • 打赏
  • 举报
回复
xmlns:tx="http://www.springframework.org/schema/tx"
zuxianghuang 2011-03-05
  • 打赏
  • 举报
回复
数据访问类要继承org.springframework.orm.hibernate3.support.HibernateDaoSupport
cart55free99 2011-03-05
  • 打赏
  • 举报
回复
少一个什么属性啊? 是配置文件中有什么没有配吗?

话说 expression="execution(* dao.impl.*.*(..))" 不应该是对dao.impl包中的类中的
<tx:method name="save*" 。。。。。 这些方法进行事务管理吗?

今天郁闷了一天了啊
jackey_ctdw 2011-03-05
  • 打赏
  • 举报
回复
少一个属性吧 好长时间没用 了 记的不是很清楚了

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧