Spring笔记——配置Hibernate框架事务
原文:http://www.iteye.com/topic/1123347
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笔记——配置Hibernate框架事务的更多相关文章
- spring boot配置mybatis和事务管理
spring boot配置mybatis和事务管理 一.spring boot与mybatis的配置 1.首先,spring boot 配置mybatis需要的全部依赖如下: <!-- Spri ...
- Spring中配置Hibernate事务管理
<!-- transationManager --> <bean id="transactionManager" class="org.springfr ...
- Spring笔记⑤--整合hibernate代码测试
String整合hibernate代码测试 在上节生成的表中插入数据: 注意:使用myeclipse2014生成的整合项目可能存在问题需要我们自己导入. 第一步 我们写dao接口 packag ...
- Spring笔记(5) - 声明式事务@EnableTransactionManagement注解源码分析
一.背景 前面详解了实现Spring事务的两种方式的不同实现:编程式事务和声明式事务,对于配置都使用到了xml配置,今天介绍Spring事务的注解开发,例如下面例子: 配置类:注册数据源.JDBC模板 ...
- 笔记:Hibernate 框架配置说明
下载 Hibernate ,打开地址 www.hibernate.org ,点击 Hibernate ORM -> Downloads 下载 4.3.11 版本,要使用Hibernate 需要把 ...
- MyEclipse配置Hibernate框架(基础篇)
一.创建java project项目 二.项目右键Configure Facets -- Install Hibernate Facet 三.项目添加对应数据库的jar包 四.编写实体类 packag ...
- 深入浅出Struts2+Spring+Hibernate框架
一.深入浅出Struts2 什么是Struts2? struts2是一种基于MVC的轻量级的WEB应用框架.有了这个框架我们就可以在这个框架的基础上做起,这样就大大的提高了我们的开发效率和质量,为公司 ...
- 整合spring和hibernate框架
一)整合spring和hibernate框架整合要点:(1)数据源配置在Spring的配置文件中,供Spring和Hibernate框架共同使用:(2)不再需要hibernate.hbm.xml配置文 ...
- spring整合mybatis(hibernate)配置
一.Spring整合配置Mybatis spring整合mybatis可以不需要mybatis-config.xml配置文件,直接通过spring配置文件一步到位.一般需要具备如下几个基本配置. 1. ...
随机推荐
- 从JetBrains公司产品给我的商业模式启示
JetBrains是捷克一家公司,专门从事IDE工具的开发,运营的产品有十几个.我因为使用JavaScript IDE工具而了解了WebStorm.进而了解了开发WebStorm的公司JetBrian ...
- Log4Net学习【二】
Log4Net结构详解 当我们在描述为系统做日志这个动作的时候,实际上描述了3个点:做日志,其实就是在规定,在什么地方 用什么日志记录器 以什么样的格式做日志.把三个最重要的点抽取出来,即什么地方,日 ...
- 文档对象模型(DOM)
文档对象模型(DOM) DOM可以将任何HTML或XML文档描绘成一个由多层节点构成的结构.节点分为几种不同的类型:文档型节点.元素节点.特性节点.注释节点等共有12种节点类型.DOM1级定义了 ...
- android开发 wifi开发不稳定性测试
场景:工厂定制机器,要求一个设备创建wifi热点,一个设备去连接.但是现在发现wifi连接很不稳定,主要以下3方面: 1.连接之前,不容易连接上 2.连接上之后,连不到外网 3.连接上之后,稳定性不好 ...
- OS X 使用技巧——不用鼠标就能打开应用程序
如果要打开的应用程序没有保留在Dock栏里,一种快速启动它的办法是按住Control+Space键后再输入应用程序的名称.按Control+Space键会开启聚光灯(Spotlight)搜索工具,它会 ...
- Google Guava学习笔记——基础工具类Preconditions类的使用
Preconditions类是一组静态方法用来验证我们代码的状态.Preconditons类很重要,它能保证我们的代码按照我们期望的执行,如果不是我们期望的,我们会立即得到反馈是哪里出来问题,现在我们 ...
- css3动画实例
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 【BZOJ】【3907】网格
组合数学/python 3907: 网格 Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 162 Solved: 76[Submit][Status][ ...
- Centos最小化安装后联网配置
Centos最小化安装默认不开启网络,只需进行简单配置就可以上网了. 1. 查看/etc/sysconfig/network-scripts/下面的文件,这里会有一个ifcfg-en******(这 ...
- CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。
CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 主要方法 public CountDownLatch(int count); pu ...