用Spring进行事务管理一直没有效果呢?不知道错在哪里?~~
只有手动进行事务管理才有数据进入, 我觉得可能代码上没有问题吧,
一开始以为是 事务没有配置好 但是查看了几遍, 也没看到什么错。
我是这样想的
用一个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);
}
....
}