Spring 整合hibernate 事物不起作用

spring配置文件

 <context:component-scan base-package="com.yy.cms"/>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/cms" />
        <property name="user" value="root" />
        <property name="password" value="accp" />

        <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
        <property name="initialPoolSize" value="3" />
        <!--连接池中保留的最小连接数。Default: 3 -->
        <property name="minPoolSize" value="3" />
        <!--连接池中保留的最大连接数。Default: 15 -->
        <property name="maxPoolSize" value="5" />
        <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
        <property name="acquireIncrement" value="3" />
        <!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
        <property name="maxStatements" value="8" />
        <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
        <property name="maxStatementsPerConnection" value="5" />
        <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
        <property name="maxIdleTime" value="1800" />    
    </bean>

    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="txManager" />

BaseDao代码

 package com.yy.cms.dao.impl;

import java.lang.reflect.ParameterizedType;
import java.util.List;



import javax.inject.Inject;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.yy.cms.dao.BaseDao;
@SuppressWarnings({ "rawtypes", "unchecked" })
public class BaseDaoImpl<T> implements BaseDao<T> {

    @Inject
    private SessionFactory sessionFactory;
    // 泛型的真正类型
    private Class clazz;

    // 通过反射获得泛型的真实类型
    public BaseDaoImpl() {
        ParameterizedType type = (ParameterizedType) this.getClass()
                .getGenericSuperclass();
        clazz = (Class) type.getActualTypeArguments()[0];
    }

    /** 得到Session */
    protected Session getSession() {
        return sessionFactory.getCurrentSession();
    }

    /**
     * 保存实体
     * 
     * @param t
     *            将被保存的实体
     * @return 被保存的实体
     */
    public T save(T t) {
        getSession().save(t);
        return t;
    }

测试类

 @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/beans.xml")
@Transactional
@Repository
public class BaseDaoImplTest {

    @Inject
    private UserDao userDao;

    @Test
    public void testSave(){
        userDao.save(new User("test"));
    }

    @Test
    public void testUpdate(){
        User user=new User(2,"update");
        userDao.update(user);
    }

http://blog.csdn.net/yangchaofeng1229/article/details/7937734

如果上面blog解决不了,你可以尝试用注解的方式,但是我之前出现这个问题的时候,是因为命名问题,要对哪些方法加事务,你要看好

<!--  配置事务通知规则  -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />   
        <tx:method name="del*" propagation="REQUIRED" rollback-for="Exception" />   
        <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception" />
        <tx:method name="save*" propagation="REQUIRED" rollback-for="Exception" />
    </tx:attributes>
</tx:advice>




你这里用的是采用注解的方式管理事务,到上面需要加@Repository注解

少了配置
<!-- 使用hibernate的事务管理器
-->

  • 要把sessionFactory交给spring才对呀


tx:attributes






/tx:attributes
/tx:advice

aop:config


/aop:config

 <!-- spring aop的使用,控制事务 -->
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref bean="sessionFactory" />
    </property>
</bean>
<tx:advice id="TestAdvice" transaction-manager="transactionManager">
    <!--  配置事务传播特性(根据事务管理器配置通知) -->
    <tx:attributes>
        ……
    </tx:attributes>
</tx:advice>

<!-- 配置参与所有service事务的类 -->
<aop:config>
    <aop:pointcut id="allTestServiceMethod" expression="execution(* cn.xx.service.*.impl.*.*(..))" />
    <aop:advisor advice-ref="TestAdvice" pointcut-ref="allTestServiceMethod" />
</aop:config>

你没有配置要扫描的包,既然你用了扫描注解方式图片说明

     Article a = articleService.findById(43);
        a.setArticleTitle("--------test数据----------------");
       String[] labels = new String[]{"1","2","3"};

不能使用 ,如果要使用Spring事务的话

不能使用 引入Hibernate配置文件方式,如果要使用Spring事务的话