Spring 配置 事务的几种方式
Spring配置文件中关于事务配置总是由三个组成部分,DataSource、TransactionManager和代理机制这三部分,无论是那种配置方法,一般变化的只是代理机制这块!
首先我创建了两个类,一个接口一个实现:
- package com.dao;
- public interface UserDao {
- public void getUser();
- }
实现:
- package com.dao.impl;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import com.dao.UserDao;
- public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
- public void getUser(){
- }
- }
第一种:每个Bean都有一个代理:
- <?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: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-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="dataSource"
- class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url"
- value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8" />
- <property name="username" value="root" />
- <property name="password" value="root" />
- <!-- 连接池启动时的初始值 -->
- <property name="initialSize" value="10" />
- <!-- 连接池的最大值 -->
- <property name="maxActive" value="10" />
- <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
- <property name="maxIdle" value="20" />
- <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
- <property name="minIdle" value="10" />
- <property name="defaultAutoCommit" value="true" />
- </bean>
- <!-- 会话工厂 -->
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="mappingLocations">
- <list>
- <value>classpath:/com/nms/entity/**/*.hbm.xml</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.MySQL5Dialect
- </prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.format_sql">true</prop>
- </props>
- </property>
- </bean>
- <!-- 定义事务管理器 -->
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <!-- 配置服务层 -->
- <bean id="userDaoAgency" class="com.dao.impl.UserDaoImpl">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <bean id="userDao"
- class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
- <!-- 配置事务管理器 -->
- <property name="transactionManager" ref="transactionManager" />
- <property name="target" ref="userDaoAgency" />
- <property name="proxyInterfaces" value="com.dao.UserDao" />
- <!-- 配置事务属性 -->
- <property name="transactionAttributes">
- <props>
- <prop key="*">PROPAGATION_REQUIRED</prop>
- </props>
- </property>
- </bean>
- </beans>
第二种:所有Bean共享一个代理:
- <?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: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-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="dataSource"
- class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url"
- value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8" />
- <property name="username" value="root" />
- <property name="password" value="root" />
- <!-- 连接池启动时的初始值 -->
- <property name="initialSize" value="10" />
- <!-- 连接池的最大值 -->
- <property name="maxActive" value="10" />
- <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
- <property name="maxIdle" value="20" />
- <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
- <property name="minIdle" value="10" />
- <property name="defaultAutoCommit" value="true" />
- </bean>
- <!-- 会话工厂 -->
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="mappingLocations">
- <list>
- <value>classpath:/com/nms/entity/**/*.hbm.xml</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.MySQL5Dialect
- </prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.format_sql">true</prop>
- </props>
- </property>
- </bean>
- <!-- 定义事务管理器 -->
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <!-- 定义事务 -->
- <bean id="base"
- class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
- lazy-init="true" abstract="true">
- <!-- 配置事务管理器 -->
- <property name="transactionManager" ref="transactionManager" />
- <!-- 配置事务属性 -->
- <property name="transactionAttributes">
- <props>
- <prop key="*">PROPAGATION_REQUIRED</prop>
- </props>
- </property>
- </bean>
- <!-- 配置服务层 -->
- <bean id="userDao"
- class="com.dao.impl.UserDaoImpl">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <!-- 代理对象 -->
- <bean id="userDaoAgency" parent="base">
- <property name="target" ref="userDao" />
- </bean>
- </beans>
第三种:拦截器:
- <?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: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-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="dataSource"
- class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url"
- value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8" />
- <property name="username" value="root" />
- <property name="password" value="root" />
- <!-- 连接池启动时的初始值 -->
- <property name="initialSize" value="10" />
- <!-- 连接池的最大值 -->
- <property name="maxActive" value="10" />
- <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
- <property name="maxIdle" value="20" />
- <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
- <property name="minIdle" value="10" />
- <property name="defaultAutoCommit" value="true" />
- </bean>
- <!-- 会话工厂 -->
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="mappingLocations">
- <list>
- <value>classpath:/com/nms/entity/**/*.hbm.xml</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.MySQL5Dialect
- </prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.format_sql">true</prop>
- </props>
- </property>
- </bean>
- <!-- 定义事务管理器(声明式的事务) -->
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <!-- 定义事务 -->
- <bean id="transactionInterceptor"
- class="org.springframework.transaction.interceptor.TransactionInterceptor">
- <property name="transactionManager" ref="transactionManager" />
- <!-- 配置事务属性 -->
- <property name="transactionAttributes">
- <props>
- <prop key="*">PROPAGATION_REQUIRED</prop>
- </props>
- </property>
- </bean>
- <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
- <property name="beanNames">
- <list>
- <value>*DaoImpl</value>
- </list>
- </property>
- <property name="interceptorNames">
- <list>
- <value>transactionInterceptor</value>
- </list>
- </property>
- </bean>
- <!-- 配置服务层 -->
- <bean id="userDaoAgency" class="com.dao.impl.UserDaoImpl">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- </beans>
第四种:使用tx标签配置的拦截器:
- <?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: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-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <!-- 数据源 -->
- <bean id="dataSource"
- class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url"
- value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8" />
- <property name="username" value="root" />
- <property name="password" value="root" />
- <!-- 连接池启动时的初始值 -->
- <property name="initialSize" value="10" />
- <!-- 连接池的最大值 -->
- <property name="maxActive" value="10" />
- <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
- <property name="maxIdle" value="20" />
- <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
- <property name="minIdle" value="10" />
- <property name="defaultAutoCommit" value="true" />
- </bean>
- <!-- 会话工厂 -->
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="mappingLocations">
- <list>
- <value>classpath:/com/nms/entity/**/*.hbm.xml</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.MySQL5Dialect
- </prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.format_sql">true</prop>
- </props>
- </property>
- </bean>
- <context:annotation-config />
- <context:component-scan base-package="com.dao" />
- <!-- 定义事务管理器 -->
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <!-- 定义事务 -->
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="*" propagation="REQUIRED" />
- </tx:attributes>
- </tx:advice>
- <!-- 定义切面 -->
- <aop:config>
- <aop:pointcut id="interceptorPointCuts" expression="execution(* com.dao.*.*(..))" />
- <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
- </aop:config>
- </beans>
第五种:注解:
- <?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: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-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <!-- 数据源 -->
- <bean id="dataSource"
- class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url"
- value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8" />
- <property name="username" value="root" />
- <property name="password" value="root" />
- <!-- 连接池启动时的初始值 -->
- <property name="initialSize" value="10" />
- <!-- 连接池的最大值 -->
- <property name="maxActive" value="10" />
- <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
- <property name="maxIdle" value="20" />
- <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
- <property name="minIdle" value="10" />
- <property name="defaultAutoCommit" value="true" />
- </bean>
- <!-- 会话工厂 -->
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="mappingLocations">
- <list>
- <value>classpath:/com/nms/entity/**/*.hbm.xml</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.MySQL5Dialect
- </prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.format_sql">true</prop>
- </props>
- </property>
- </bean>
- <context:annotation-config />
- <!-- 使用注解的包路径 -->
- <context:component-scan base-package="com.dao" />
- <!-- 支持 @Transactional 标记 -->
- <tx:annotation-driven transaction-manager="transactionManager"/>
- <!-- 定义事务管理器 -->
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- </beans>
如果使用了注解,那么实现类应该这样写:
- package com.dao.impl;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import org.springframework.stereotype.Component;
- import org.springframework.transaction.annotation.Transactional;
- import com.dao.UserDao;
- @Transactional
- @Component("userDaoAgency")
- public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
- /**
- * 为方法增加事务处理特性
- */
- @Transactional(readOnly=true)
- public void getUser(){
- }
- }
这样每个方法都能自己定义自己的事务处理!
Spring 配置 事务的几种方式的更多相关文章
- Spring配置事务的五种方式
Java事务的类型有三种: JDBC事务. 可以将多个 SQL 语句结合到一个事务中.JDBC 事务的一个缺点是事务的范围局限于一个数据库连接.一个 JDBC 事务不能跨越多个数据库 JTA(Java ...
- spring事务之——spring配置事务的五种方式
Spring配置文件中关于事务配置总是由三个部分组成,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分. DataSo ...
- spring配置属性的两种方式
spring配置属性有两种方式,第一种方式通过context命名空间中的property-placeholder标签 <context:property-placeholder location ...
- Spring 实现事务的三种方式
事务:保证数据的运行不会说A给B钱,A钱给了B却没收到. 实现事务的三种方式(重要代码): 1.aspectJ AOP实现事务: <bean id="dataSourceTransac ...
- SSH2配置事务的两种方式
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 【Spring】19、spring配置数据源的4种方式
不管采用何种持久化技术,都需要定义数据源.Spring中提供了4种不同形式的数据源配置方式: spring自带的数据源(DriverManagerDataSource),DBCP数据源,C3P0数据源 ...
- Spring配置dataSource的三种方式 数据库连接池
1.使用org.springframework.jdbc.dataSource.DriverManagerDataSource 说明:DriverManagerDataSource建立连接是只要有连接 ...
- 浅谈spring配置定时任务的几种方式
网上看到好多关于定时任务的讲解,以前只简单使用过注解方式,今天项目中看到基于配置的方式实现定时任务,自己做个总结,作为备忘录吧. 基于注解方式的定时任务 首先spring-mvc.xml的配置文件中添 ...
- Spring Boot配置过滤器的两种方式
过滤器(Filter)是Servlet中常用的技术,可以实现用户在访问某个目标资源之前,对访问的请求和响应进行拦截,常用的场景有登录校验.权限控制.敏感词过滤等,下面介绍下Spring Boot配置过 ...
随机推荐
- SmartOS技术常见问题
什么是默认用户名和密码? 当SmartOS Live Image第一次启动时,系统将提示您设置root密码. 如果在不导入Zpool的情况下启动SmartOS,则需要默认的root密码. 当使用noi ...
- input:file属性
1.accept 只能选择png和gif图片 <input id="fileId1" type="file" accept="image/png ...
- rabbitMQ 常用命令
启动监控管理器:rabbitmq-plugins enable rabbitmq_management 关闭监控管理器:rabbitmq-plugins disable rabbitmq_manage ...
- np.eye()
今天在完成深度学习的相关编程作业的时候,发现代码中出现了一个关于np.eye()的函数,这个函数的用法非常的简单,但是在预制的代码中,这个函数的用法并非单单制造一个对角矩阵,而是通过其来将一个labe ...
- ROS学习笔记三(理解ROS节点)
要求已经在Linux系统中安装一个学习用的ros软件包例子: sudo apt-get install ros-indigo-ros-tutorials ROS图形概念概述 nodes:节点,一个节点 ...
- sublime text3 配置
1.在sublime text官网下载安装文件并安装 http://www.sublimetext.com/ 2.安装sublime的package control 参考网址:https://pack ...
- Spring框架之什么是IOC的功能?
1. 什么是IOC的功能? * IoC -- Inverse of Control,控制反转,将对象的创建权反转给Spring!! * 使用IOC可以解决的程序耦合性高的问题!!
- Vmware迁移以后eth0消失,无法上网
一个再普通不过的大神帮助小菜做虚拟机镜像的事情: 小张:帮我做个Vmware下的Ubuntu镜像吧,大神. 小黄:好啊,等我一下,下午发给你. 经过一番操作,小黄顺利的做出了一个虚拟机操作系统 小黄: ...
- PHP-GTK的demo在windows下运行出现的问题
I am trying to use Firebird 2.5.2.26539 with wamp,When i enable the extensions of firebird in php: - ...
- <Linux多线程服务端编程>学习记录
使用智能指针解决多线程下 类的解析冲突问题 有这样一个场景 使用StockFactory记录Stock的信息 容器是map<string,smart_ptr>; 代码如下: #inclu ...