spring 编程式事务配置

1、事务切面:

package com.hibernate.util;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

public class TxAspect {

    private PlatformTransactionManager platformTransactionManager;
    public void setPlatformTransactionManager(
            PlatformTransactionManager platformTransactionManager) {
        this.platformTransactionManager = platformTransactionManager;
    }
    public Object tx(ProceedingJoinPoint joinPoint) throws Throwable{
        Object rev = null;
        //TransactionDefinition的实现类
        DefaultTransactionDefinition td = new DefaultTransactionDefinition();
        //开始事物,传入TransactionDefinition对象,用于指定事物相关属性(隔离、传播、超时、只读 )
        TransactionStatus ts = platformTransactionManager.getTransaction(td);
        try {
            rev = joinPoint.proceed();//直接使用原来的参数调用目标方法;
            //提交事务
            platformTransactionManager.commit(ts);
        } catch (Exception e) {
            // 回滚事务
            System.out.println(e);
            platformTransactionManager.rollback(ts);
        }
        return rev;
    }
}

 

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

    <!-- <context:component-scan base-package="com.hibernate"></context:component-scan> -->
    <!-- 配置数据源 -->
    <context:property-placeholder location="classpath:db.properties"/>
<!-- 配置C3p0 -->
    <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="user" value="${jdbc.user}"></property>
            <property name="password" value="${jdbc.password}"></property>
            <property name="driverClass" value="${jdbc.driverClass}"></property>
            <property name="jdbcUrl" value="${jdbc.url}"></property>
            <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
    </bean>
    <!-- 配置hibernate 的 session Factory 实例 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="datasource"></property>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <!-- <property name="mappingLocations" value="classpath:com/hibernate/entities/*.hbm.xml"></property> -->
    </bean>
    
    <bean id="bookDao" class="com.hibernate.dao.BookDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <bean id="bookService" class="com.hibernate.service.BookServiceImpl">
        <property name="bookDao" ref="bookDao"></property>
    </bean>
    
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <tx:advice id="txAdvice" transaction-manager="transactionManager"> 
        <tx:attributes>
            <tx:method name="get*" propagation="REQUIRED" read-only="false"/>
        </tx:attributes>
    </tx:advice>

    <!--1、配置事物管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    
<!-- 编程式事务管理 注解开启 -->
    <bean id="txAspect" class="com.hibernate.util.TxAspect">
        <property name="platformTransactionManager" ref="transactionManager"></property>
    </bean>
    <aop:config>
        <aop:pointcut expression="execution(* com.hibernate.service.*.*(..))" id="fkpc"/>
        <!-- 将txAspect当成aspect使用 -->
        <aop:aspect ref="txAspect" >
            <aop:around method="tx" pointcut-ref="fkpc"/>
        </aop:aspect>
    </aop:config>
<!-- 编程式事务管理结束 -->


<!-- 申明式事务管理 注解开启-->    
        <!--  
    配置事物属性
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    
    
    配置事物切点
    <aop:config>
        <aop:pointcut expression="execution(* com.hibernate.service.*.*(..))" id="txPointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config> -->
    <!-- 申明式事务管理 结束-->    
</beans>
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值