Spring里通过注解开启事物
方式1
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<import resource="classpath:applicationContext-dao.xml" />
<bean id="bbtForum"
class="com.baobaotao.service.impl.BbtForumImpl"
p:forumDao-ref="forumDao"
p:topicDao-ref="topicDao"
p:postDao-ref="postDao"/> <bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource"/> <aop:config>
<aop:pointcut id="serviceMethod"
expression="execution(* com.baobaotao.service.*Forum.*(..))" />
<aop:advisor pointcut-ref="serviceMethod"
advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" >
<tx:attributes>
<tx:method name="get*" read-only="false"/>
<tx:method name="add*" rollback-for="PessimisticLockingFailureException"/>
<tx:method name="update*"/>
</tx:attributes>
</tx:advice>
</beans>
方式2
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [
<!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">
]> <beans>
<!-- 配置hibernate相关数据库的操作 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 数据库的驱动 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<!-- 数据库的Id,用户名与密码 -->
<property name="url" value="jdbc:mysql://hil729xy.zzcdb.dnstoo.com:5505/yxasgvco"/>
<property name="username" value="yxasgvco_f"/>
<property name="password" value="a123456"/>
</bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<!-- 配置不同数据库的方言 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- 其实这个参数的作用主要用于:自动创建|更新|验证数据库表结构。如果不是此方面的需求建议set value="none".
其它几个参数的意思,我解释一下:
validate 加载hibernate时,验证创建数据库表结构
create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。
create-drop 加载hibernate时创建,退出是删除表结构
update 加载hibernate自动更新数据库结构 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- 是否显示sql语句 -->
<prop key="hibernate.show_sql">true</prop>
<prop key="hiberante.format_sql">true</prop>
</props>
</property>
<property name="configLocations">
<list>
<value>
<!-- 读取和实体相关的xml -->
classpath*:com/tgb/web/controller/hibernate/hibernate.cfg.test.xml
</value>
</list>
</property>
</bean>
<!-- 定义事务管理器(声明式的事务) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <bean id="transactionBese" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true">
<!-- 配置事务管理器 -->
<property name="transactionManager" ref="transactionManager"></property>
<!-- 配置事务管理器 -->
<property name="transactionAttributes">
<props> <!-- 下面就开始配置各个模块所必须的部分,在各自的applicationContext-XXX-beans.xml配置的对于事务管理的详细信息。 首先就是配置事务的传播特性,如下: --> <!-- 配置事务传播特性 --> <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="get*">PROPAGATION_NEVER</prop>
</props>
</property>
</bean> </beans>
Spring里通过注解开启事物的更多相关文章
- 关于如何使用Spring里@AliasFor注解进行注解的封装
不知道大家每次使用Spring boot的时候有没有看过它启动类里 @SpringBootApplication这个注解呢?众所周知,这个注解是一个复合注解,但是注解是不能继承元注解的属性的,也就是说 ...
- Spring中 使用注解+c3p0+事物 《模拟银行转账》
使用注解的方式 模拟转账 要么都成功 要么都失败 !保持一致性! 准备工作: jar包: 需要的类: UserDao: package com.hxzy.spring.c3p0.Dao ...
- Spring转账业务_注解配置事物控制
1.beans.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h ...
- spring里的事物设置
有的人说事物在spring里设置有两种,其实事物设置在spring配置文件中共有五种方式:第一种方式:每个Bean都有一个代理第二种方式:所有Bean共享一个代理基类第三种方式:使用拦截器第四种方式: ...
- Spring的相关注解
说明写在最前面:摘录于 博客园--受伤滴小萝卜 文章 文章链接受伤滴小萝卜文章--Spring注解 本文章只用作学习和帮助其他人学习记录使用 Spring 注解学习笔记 声明Bean的注解: ...
- spring 、spring boot 常用注解
@Profile 1.用户配置文件注解. 2.使用范围: @Configration 和 @Component 注解的类及其方法, 其中包括继承了 @Component 的注解: @Service. ...
- Spring中@相关注解的意义
1.@controller 控制器(注入服务) 用于标注控制层,相当于struts中的action层 2.@service 服务(注入dao) 用于标注服务层,主要用来进行业务的逻辑处理 3.@rep ...
- spring.xml及注解
spring.xml配置文件中配置注解: 开启注解(及自动扫描包中bean): 1:<context:component-scan base-package="com.bzu" ...
- Spring/SpringBoot常用注解总结
转自:[Guide哥] 0.前言 可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用法,掌握搞懂,使 ...
随机推荐
- webpack+babel+ES6+react环境搭建
webpack+babel+ES6+react环境搭建 步骤: 1 创建项目结构 注意: 先创建一个项目目录 react 这个名字自定义,然后进入到这个目录下面 mkdir app //创建app ...
- ubuntu运行android studio出错unable to run mksdcard sdk tool
原因:缺少lib 解决方法: sudo apt-get install lib32z1 lib32ncurses5 lib32stdc++6 完美解决.
- POJ 2635 The Embarrassed Cryptographer (千进制,素数筛,同余定理)
The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15767 A ...
- Codeforces 920G List Of Integers 二分 + 容斥
题目链接 题意 给定 \(x,p,k\),求大于 \(x\) 的第 \(k\) 个与 \(p\) 互质的数. 思路 参考 蒟蒻JHY. 二分答案 \(y\),再去 \(check\) 在 \([x,y ...
- 【Visual Studio】error c4996: 'fopen': This function or variable may be unsafe(转)
原文转自 http://blog.csdn.net/zhangyuehuan/article/details/12012635 [解决方案]项目 =>属性 =>c/c++ =>预处 ...
- UART接口介绍
1. 简介 UART, Universal Asynchronous Receiver-Transmitter, 通用异步收发传输器 UART协议规定了通信双方所遵守的规定,属于数据链路层RS232接 ...
- ios 地图,系统升级为12后,进入地图,大头针全部默认展开问题,以及在选择不同距离的情况下,如何刷新地图的区域范围
1.第一个问题,大头针在ios12,默认展开问题,需要设置大头针视图的默认选中属性为NO - (MKAnnotationView *)mapView:(MKMapView *)mapView view ...
- Codeforces 429D Tricky Function(平面最近点对)
题目链接 Tricky Function $f(i, j) = (i - j)^{2} + (s[i] - s[j])^{2}$ 把$(i, s[i])$塞到平面直角坐标系里,于是转化成了平面最近点 ...
- CVPR 2019|PoolNet:基于池化技术的显著性检测 论文解读
作者 | 文永亮 研究方向 | 目标检测.GAN 研究动机 这是一篇发表于CVPR2019的关于显著性目标检测的paper,在U型结构的特征网络中,高层富含语义特征捕获的位置信息在自底向上的传播过 ...
- Java NIO.2 Files类的常用方法
Files类是一个工具类,提供了大量的静态方法来操作文件.文件夹. Files类常用方法: long size(Path path) //返回文件大小 boolean isDirectory ...