您的位置:首页 > 编程语言 > Java开发

Spring之事物处理

2018-03-22 21:51 302 查看

Spring之事物处理

事物分2种:

编程式事务控制

自己手动控制事务,就叫做编程式事务控制。

Jdbc代码:

Conn.setAutoCommite(false);  // 设置手动控制事务


Hibernate代码:

Session.beginTransaction();    // 开启一个事务
【细粒度的事务控制: 可以对指定的方法、指定的方法的某几行添加事务控制】
(比较灵活,但开发起来比较繁琐: 每次都要开启、提交、回滚.)


声明式事物控制

Spring提供了对事务的管理, 这个就叫声明式事务管理。

Spring提供了对事务控制的实现。用户如果想用Spring的声明式事务管理,只需要在配置文件中配置即可; 不想使用时直接移除配置。这个实现了对事务控制的最大程度的解耦。

Spring声明式事务管理,核心实现就是基于Aop。

【粗粒度的事务控制: 只能给整个方法应用事务,不可以对方法的某几行应用事务。】

(因为aop拦截的是方法。)

Spring声明式事务管理器类:

Jdbc技术:DataSourceTransactionManager
Hibernate技术:HibernateTransactionManager


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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

</beans>


C3P0的数据源:

<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///ice?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"></property>
<property name="user" value="root"></property>
<property name="password" value="wen123"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="10"></property>
<property name="maxStatements" value="100"></property>
<property name="acquireIncrement" value="2"></property>
</bean>


添加事物管理类

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="c3p0"></property>
</bean>


配置事物管理类

<tx:advice transaction-manager="txManager" id="tx">
<tx:attributes >
<tx:method name="get*" read-only="true" />
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice><tx:advice transaction-manager="txManager" id="tx">
<tx:attributes >
<tx:method name="get*" read-only="true" />
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>


创建Bean实例

<bean id="userDao" class="tx.Jdbc.UserDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg type="javax.sql.DataSource" ref="c3p0"></constructor-arg>
</bean>

<bean id="service" class="tx.Jdbc.ServiceAdmin">
<property name="userDao" ref="userDao"></property>
</bean>


AOP配置

<aop:config proxy-target-class="true">
<aop:pointcut expression="execution(* tx.Jdbc.ServiceAdmin.*(..))" id="rf"/>
<aop:advisor advice-ref="tx" pointcut-ref="rf"/>
</aop:config>


注解

<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

</beans>


C3P0的数据源:

C3P0的数据源:
<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///ice?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"></property>
<property name="user" value="root"></property>
<property name="password" value="wen123"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="10"></property>
<property name="maxStatements" value="100"></property>
<property name="acquireIncrement" value="2"></property>
</bean>


添加事物管理类:

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="c3p0"></property>
</bean>


JdbcTemplate工具类实例:

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="c3p0"></property>
</bean>


注解扫描:

<context:component-scan base-package="auto.Jdbc"></context:component-scan>


事物注解声明:

<tx:annotation-driven transaction-manager="txManager"/>


UserDao



Service



Transactional注解是声明该类所有操控数据访问的事物

它可以在方法中使用

事物的属性说明:

@Transactional(
readOnly = false,  // 读写事务
timeout = -1,       // 事务的超时时间不限制
noRollbackFor = ArithmeticException.class,  // 遇到数学异常不回滚
isolation = Isolation.DEFAULT,              // 事务的隔离级别,数据库的默认
propagation = Propagation.REQUIRED          // 事务的传播行为
)
public void save(Dept dept){
deptDao.save(dept);
int i = 1/0;
deptDao.save(dept);
}


事务传播行为:

Propagation.REQUIRED[/b]

指定当前的方法必须在事务的环境下执行;如果当前运行的方法,已经存在事务, 就会加入当前的事务;

Propagation.REQUIRED_NEW

指定当前的方法必须在事务的环境下执行;如果当前运行的方法,已经存在事务: 事务会挂起; 会始终开启一个新的事务,执行完后; 刚才挂起的事务才继续运行。

举例:

Class Log{
Propagation.REQUIRED
insertLog();
}

Propagation.REQUIRED
Void  saveDept(){
insertLog();    // 加入当前事务
.. 异常, 会回滚
saveDept();
}

Class Log{
Propagation.REQUIRED_NEW
insertLog();
}

Propagation.REQUIRED
Void  saveDept(){
insertLog();    // 始终开启事务
.. 异常, 日志不会回滚
saveDept();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  308