开发者社区> 问答> 正文

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);
    }

展开
收起
小旋风柴进 2016-03-17 09:44:10 2112 0
1 条回答
写回答
取消 提交回答
  • <!--  配置事务通知规则  -->
    <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>
    2019-07-17 19:04:32
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多