目录

1.分类

事务控制的方式:

  • 编程式事务控制:编程灵活,但是开发繁琐,每次都要开启、回滚等操作

    • jdbc:Conn.setAutoCommite(false); //设置手动事务控制
    • hibernate:Session.beginTransaction(); //开启事务
  • 声明式事务控制:使用配置文件实现,使用时添加配置,不需要时候移除配置即可。核心是aop,所以控制粒度在方法。也就是对方法来应用事务,不能对方法中的某几行代码实现。对于事务的控制应该放到Server层来处理。

    • jdbc:DataSourceTransactionManager

    • hibernate:


2.Spring对jadc事务管理

2.1xml方式

2.1.1首先定义Dao对象和Server对象

package per.liyue.spring.jdbc_trancation_xml;

import org.springframework.jdbc.core.JdbcTemplate;

/**
* Created by liyue on 2016/11/30.
*/
public class RoleDao
{
private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
{
this.jdbcTemplate = jdbcTemplate;
} public void save(String sql)
{
jdbcTemplate.update(sql);
}
}
package per.liyue.spring.jdbc_trancation_xml;

/**
* Created by liyue on 2016/11/30.
*/
public class RoleService
{
private RoleDao roleDao; public void setRoleDao(RoleDao roleDao)
{
this.roleDao = roleDao;
} public void save(String sql)
{
roleDao.save(sql);
int i = 10 / 0;//这里执行失败要回滚成功的操作
roleDao.save(sql);
}
}

2.1.2配置文件实现事务管理

配置中要注意:

  • 配置文件中对于事务的命名空间(xmlns:tx="http://www.springframework.org/schema/tx")要引入
  • 对于jdbc来说核心是通过org.springframework.jdbc.datasource.DataSourceTransactionManager来实现事务控制
  • 对于DataSourceTransactionManager的实现还需要配置tx:advice来指明对于哪些方法有哪些权限的操作
  • 对于拦截方法可以用*来模糊匹配
<?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/beans http://www.springframework.org/schema/beans/spring-beans.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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--1.jdbc的基本配置-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///hi"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="roleDao" class="per.liyue.spring.jdbc_trancation_xml.RoleDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="roleService" class="per.liyue.spring.jdbc_trancation_xml.RoleService">
<property name="roleDao" ref="roleDao"></property>
</bean> <!--2.事务配置--> <!--2.1事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!--2.2事务控制 transaction-manager:jdbc的事务管理器 method:配置需要管理的方法和属性,这里可以用*来模糊匹配 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" read-only="false"/>
</tx:attributes>
</tx:advice> <!--2.3事务AOP aop:advisor:指定事务控制和切入点 -->
<aop:config>
<aop:pointcut id="pt"
expression="execution(* per.liyue.spring.jdbc_trancation_xml.RoleService.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"></aop:advisor>
</aop:config>
</beans>

2.2注解方式

注解方式实现相对简单很多!

需要改动的点:

  • 开启注解
  • 指定注解的事务管理类
  • 对各个类增加注解(@Repository,@Service,@Resource)
  • 对于需要事务管理的位置增加注解@Transactional:
    • 对方法增加:对方法实现事务管理

    • 对类增加:对类实现事务管理

2.2.1对象类

package per.liyue.spring.jdbc_trancation_annotation;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository; /**
* Created by liyue on 2016/11/30.
*/
@Repositorypublic
class RoleDao
{
private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
{
this.jdbcTemplate = jdbcTemplate;
} public void save(String sql)
{
jdbcTemplate.update(sql);
}
}
package per.liyue.spring.jdbc_trancation_annotation;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; /**
* Created by liyue on 2016/11/30.
*/
@Servicepublic
class RoleService
{
@Resource
private RoleDao roleDao; public void setRoleDao(RoleDao roleDao)
{
this.roleDao = roleDao;
} @Transactional
public void save(String sql)
{
roleDao.save(sql);
int i = 10 / 0;//这里执行失败要回滚成功的操
roleDao.save(sql);
}
}

2.2.2配置文件

<?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/beans http://www.springframework.org/schema/beans/spring-beans.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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--1.jdbc的基本配置-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///hi"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="roleDao" class="per.liyue.spring.jdbc_trancation_annotation.RoleDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="roleService" class="per.liyue.spring.jdbc_trancation_annotation.RoleService">
<property name="roleDao" ref="roleDao"></property>
</bean> <!--2.事务配置--> <!--2.1事务管理器-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!--2.2开启注解扫描-->
<context:component-scan
base-package="per.liyue.spring.jdbc_trancation_annotation"></context:component-scan> <!--2.3开启注解事务模式-->
<tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven>
</beans>

2.3事务的属性

事务对于Dao的操作,这里举例如:

package per.liyue.spring.jdbc_trancation_annotation;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; /**
* Created by liyue on 2016/11/30.
*/
@Servicepublic
class RoleService
{
@Resource
private RoleDao roleDao; public void setRoleDao(RoleDao roleDao)
{
this.roleDao = roleDao;
} @Transactional(readOnly = false, //只读
timeout = -1, //超时,但是最终是由底层数据库来决定
noRollbackFor = ArithmeticException.class, //对于指定异常不执行回滚操作,这里的示例对于数学异常不会滚 )
public void save(String sql)
{
roleDao.save(sql);
int i = 10 / 0;//这里执行失败要回滚成功的操作
roleDao.save(sql);
}
}

propagation:

isolation:


3.

最新文章

  1. jQuery静态方法globalEval使用和源码分析
  2. python 实时遍历日志文件
  3. PLSQL_Oracle PLSQL内置函数大全(概念)
  4. 学习总结 html图片热点,网页划区,拼接,表单
  5. cocos2d-x ScrollView、TableView
  6. 【荐】Redis学习资料汇总
  7. SQL(笔试题)网站上的广告位是轮播的,每天某一广告位最多可轮播的广告数量是有限制的,比如A广告位,每天只能轮播三个广告,但销售人员在销售广告位时并不考虑此限制,要求查询出合同表中,超过广告位轮播数量的合同。
  8. error in invoking target &#39;mkldflags ntcontab.o nnfgt.o&#39; of makefile
  9. JVM(一) OpenJDK1.8源码在Ubuntu16.04下的编译
  10. Bootstrap框架的了解和使用之栅格系统
  11. C#在PDF中如何以不同颜色高亮文本
  12. 异常:Caused by: java.sql.SQLException: Field &#39;cust_id&#39; doesn&#39;t have a default value
  13. [转]GitHub for Windows 安装失败,An error occurred attempting to install github 的解决办法
  14. 数据加密之DES加密
  15. MVC详解(转)
  16. shell 13文件包含
  17. 尽量少嵌套无用的div;外部文件尽量使用link而不要使用用@import
  18. golang build 的简单用法.(菜鸟初学)
  19. css -- css选择器
  20. 使用 Flask 框架写用户登录功能的Demo时碰到的各种坑(一)——创建应用

热门文章

  1. mysql数据库的基本操作命令整理
  2. shell脚本监测DNS链接状态给传给zabbix值
  3. java 加密工具类(MD5、RSA、AES等加密方式)
  4. (32)forms组件(数据校验)
  5. tomcat访问错误调试方法
  6. 第一章 HTML+CSS(中)
  7. SpringSecurity-FilterSecurityInterceptor的作用
  8. (转载)o(1), o(n), o(logn), o(nlogn) 时间复杂度
  9. 三元运算&amp;匿名函数lambda
  10. SqlServer常用内置函数