springmvc+hibernate4事务管理配置
1、事务的特性
事务的四种特性:
原子性:体现一个事务的操作的不可分割,要么权执行,要么全不执行。
一致性:事务的执行结果必须从一种一致性状态变到另一种一致性状态。最典型的就是转账,两个账户A、B总金额为5000,不管A、B如何转账,转几次,当事务结束A、B账户总金额还为5000。
隔离型:即并发执行的事务操作同一张表时相互之间不能相互影响。举例说明就是对于任意两个并发的事务T1和T2,在事务T1看来,T2要么在T1开始之前就已经结束,要么在T1结束之后才开始,不能出现交叉执行。
持久性:指事务一旦被提交,对数据库中数据的改变时永久性的。
2、事务的隔离性(解决脏读、不可重读和幻读问题)
隔离级别所解决的问题:
脏读(Dirty Reads):一个事务读取了另一个事务还未提交的数据,这里主要指事务读取了另一个事务未回滚前的数据。
不可重读(Non-RepeatableReads):不可重复读是指在对于数据库中的某个数据,一个事务范围内多次查询却返回了不同的数据值。例如事务T1在读取某一数据,而事务T2立马修改了这个数据并且提交事务给数据库,事务T1再次读取该数据就得到了不同的结果,此时发生了不可重复读。
幻读:指事务A对符合条件的n条数据进行批量修改,在此过程中(事务A还未提交)事务B向数据库中插入了一条符合事务A条件的数据,此时事务A再读取时发现有未修改的数据,此时便产生幻读。
注意:不可重读和幻读均读取了另一个事务已提交的数据,这一点区别于脏读,但不可重读针对一条数据,而幻读则针对批量数据。
四种隔离级别:
SERIALIZABLE:可避免脏读、不可重复读、幻读的发生
REPEATABLE_READ:可避免脏读、不可重复读的发生
READ_COMMITTED:可避免脏读的发生
READ_UNCOMMITTED:最低级别,任何情况都无法保证
3、事务的传播性(解决事务创建时机的问题)
七种传播级别:
PROPAGATION_REQUIRED:如果上下文中已经存在事务,那么就加入到事务中执行;如果当前上下文中不存在事务,则新建事务执行。
PROPAGATION_SUPPORTS:如果上下文存在事务,则支持事务加入事务,如果没有事务,则使用非事务的方式执行
PROPAGATION_MANDATORY:该级别的事务要求上下文中必须要存在事务,否则就会抛出异常!配置该方式的传播级别是有效的控制上下文调用代码遗漏添加事务控制的保证手段。比如一段代码不能单独被调用执行,但是一旦被调用,就必须有事务包含的情况,就可以使用这个传播级别
PROPAGATION_REQUIRES_NEW:每次都会新建一个事务,并且同时将上下文中的事务挂起,执行当前新建事务完成以后,上下文事务恢复再执行
PROPAGATION_NOT_SUPPORTED:若上下文中存在事务,则挂起事务,执行当前逻辑,结束后恢复上下文的事务
PROPAGATION_NEVER:上下文中不能存在事务,一旦有事务,就抛出runtime异常
PROPAGATION_NESTED:如果上下文中存在事务,则嵌套事务执行,如果不存在事务,则新建事务
一、配置代码
web.xml
<!--spring打开session及事务配置-->
<filter>
<filter-name>SpringOpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SpringOpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
1、注解方式
applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--spring的配置文件 与springMVC的配置文件对包的重复扫描装配会照成失效在主容器中(applicationContext.xml),将Controller的注解排除掉-->
<context:component-scan base-package="example">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan> <!-- 创建sessinFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">
<list>
<value>example.entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hbm2ddl.auto">update</prop>
</props>
</property>
</bean> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> </beans>
然后在service层的类或方法上加@Transactional即可。
注解方式需添加<tx:annotation-driven transaction-manager="transactionManager"/>,且@Transactional只能放在public修饰的方法或类,其它访问权限前添加无效。
事务的隔离级别及传播方式可通过下面方式配置(propagation配置传播方式默认为REQUIRED,isolation配置隔离级别,默认为DEFAULT,用的为数据库的默认隔离级别,大部分数据库默认隔离级别为READ_COMMITTED,mysql为REPEATABLE_READ)
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
2、aop方式
applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--spring的配置文件 与springMVC的配置文件对包的重复扫描装配会照成失效在主容器中(applicationContext.xml),将Controller的注解排除掉-->
<context:component-scan base-package="example">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/spring_hibernate"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean> <!-- 创建sessinFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">
<list>
<value>example.entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hbm2ddl.auto">update</prop>
</props>
</property>
</bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut expression="execution(* example.service..*.*(..))" id="fooServiceOperation"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/>
</aop:config> </beans>
事务的隔离级别及传播方式可通过下面方式配置(propagation配置传播方式,isolation配置隔离级别)
<tx:method name="*" propagation="REQUIRED" isolation="READ_COMMITTED"/>
二、配置后事务不起作用原因
1、dao层需使用sessionFactory.getCurrentSession()方法获得当前session,不能使用sessionFactory.openSession()
原因:
采用getCurrentSession()创建的Session会绑定到当前的线程中去、而采用OpenSession()则不会。
采用getCurrentSession()创建的Session在commit或rollback后会自动关闭,采用OpenSession()必须手动关闭。
2、applicationContext.xml中需添加下面配置
<!--spring的配置文件 与springMVC的配置文件对包的重复扫描装配会照成失效在主容器中(applicationContext.xml),将Controller的注解排除掉-->
<context:component-scan base-package="example">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
SpringMVC+Hibernate4 导致事务失效不提交的可能原因
springmvc+hibernate4事务管理配置的更多相关文章
- Spring事务管理配置以及异常处理
Spring事务管理配置: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...
- spring,mybatis事务管理配置与@Transactional注解使用[转]
spring,mybatis事务管理配置与@Transactional注解使用[转] spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是至关 ...
- SpringMVC+MyBatis 事务管理二
前言 上篇主要从编程式事务和声明式事务注解的形式来了解了事务,而这篇我们针对AOP的方式来实现事务.先回顾下事务的基础知识事务的隔离级别和事务的传播行为.使用aop 配置事务时注意引用aspectjw ...
- springMVC+mybatis事务管理总结
1.spring,mybatis事务管理配置与@Transactional注解使用: 概述事务管理对于企业应用来说是至关重要的,即使出现异常情况,它也可以保证数据的一致性.Spring Framewo ...
- 事务管理配置与@Transactional注解使用
spring,mybatis事务管理配置与@Transactional注解使用 概述 事务管理对于企业应用来说是至关重要的,即使出现异常情况,它也可以保证数据的一致性. Spring Framewor ...
- spring,mybatis事务管理配置与@Transactional注解使用
spring,mybatis事务管理配置与@Transactional注解使用[转] spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是 ...
- spring + springmvc+ mybatis 事务管理及控制
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- spring3.0事务管理配置
转载:http://war-martin.iteye.com/blog/1396335 第一种配置方法:基于XML的事务管理 这种方法不需要对原有的业务做任何修改,通过在XML文件中定义需要拦截方法的 ...
- JTA事务管理--配置剖析(二)
Spring引用Tomcat的 JTA事务 Tomcat是Servlet容器,但它提供了JNDI的实现,因此用户可以象在Java EE应用程序服务器中一样,在Tomcat中使用JNDI查找JD ...
随机推荐
- flume监控一个linux指定的一个文件夹的文件信息
1.编辑一个配置文件 flume-app.conf 拷贝至fulme的安装目录的conf下 # The configuration file needs to define the sources, ...
- CentOS7最小化安装连接到网络的解决方案
周末在家想装个虚拟机学一下Hadoop啥的,因为网速小水管比较慢所以下载的是CentOS的minimal版本的: 这个版本安装完之后默认是没有安装net-tools的,也就是说ifconfig不能用: ...
- Win7下SQLPlus登录时报错"SP2-1503:无法初始化Oracle调用界面"
Oracle安装完毕,使用SQLPlus登录的时候报错"SP2-1503:无法初始化Oracle调用界面",然后一闪界面关闭,报错界面如下: 这个是因为在Win7需要管理员权限,右 ...
- Verilog笔记.6.FIFO
FIFO,First In First Out ,是一种先进先出的数据缓存器. 没有外部读写地址线,只能顺序写入数据,顺序的读出数据, 其数据地址由内部读写指针自动加1完成. 不能像普通存储器那样可以 ...
- ogg使用语句
create tablespace ogg datafile '/oracle/oradata/DRMT/ogg01.dbf' size 50M autoextend on; edit params ...
- 使用迭代法穷举1到N位最大的数
这是何海涛老师剑指offer上面第12题,这题首先注意不能使用整数int型作为操作对象,因为N很大时明显会溢出.这种大数据一般都是使用的字符串来表示. 直接法就是:1.针对字符串的加法,涉及循环进位及 ...
- 用Centos7搭建小微企业Samba文件共享服务器【转】
转自 用Centos7搭建小微企业Samba文件共享服务器 - 今日头条(www.toutiao.com)http://www.toutiao.com/i6436937837660078593/ 最近 ...
- PHP 面向对象 final类与final方法
final---用于类.方法前. final类---不可被继承. final方法---不可被覆盖. final类不能被继承. 如果我们不希望一个类被继承,我们使用final来修饰这个类.这个类将无法被 ...
- golang基础之三-字符串,时间,流程控制,函数
strings和strconv的使用 strings strings.HasPrefix(s string,preffix string) bool:判断字符串s是否以prefix开头 stirngs ...
- kNN算法笔记
kNN算法笔记 标签(空格分隔): 机器学习 kNN是什么 kNN算法是k-NearestNeighbor算法,也就是k邻近算法.是监督学习的一种.所谓监督学习就是有训练数据,训练数据有label标好 ...