社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
数据库事务(Database Transaction) ,是指作为单个逻辑工作单元执行的一系列操作,
要么完全地执行,要么完全地不执行。
一个逻辑工作单元要成为事务,必须满足所谓的ACID(原子性、一致性、隔离性和持久性)属性。
事务必须是原子工作单元;对于其数据修改,要么全都执行,要么全都不执行。(整体)
事务在完成时,必须使所有的数据都保持一致状态。(完成)
由并发事务所做的修改必须与任何其它并发事务所做的修改隔离。(并发)
事务完成以后,它对于系统的影响是永久性的。该修改即使出现致命的系统故障已将一直保持。(结果)
一个事务读到另一个事务没有提交的数据。
一个事务读取到另一个事务已提交的数据(update)。
一个事务读取到另一个事务已提交的数据(insert)。
Connection conn = null;
try{
//1 数据库链接
conn = ...
//2 开启事物
conn.setAutoCommit(false);
操作A
操作B
操作C
操作D
//3 提交事务
conn.commit();
} catche() {
//4 回滚事务
conn.rollback();
}
Connection conn = null;
//保存点,记录操作的当前位置,之后可以回滚到指定的位置.(可以回滚一部分)
Savepoint savepoint = null;
try{
//1 数据库链接
conn = ...
//2 开启事物
conn.setAutoCommit(false);
操作A
操作B
savepoint = conn.setSavepoint();
操作C
操作D
//3 提交事务
conn.commit();
} catche() {
//4 回滚事务
if(savepoint != null) { // CD异常
//回滚到CD之前
conn.rollback(savepoint)
//提交AB
conn.commit();
} else {
//回滚AB
conn.rollback();
}
}
DataSourceTransactionManager jdbc开发时事物管理器,采用JdbcTemplate。
HibernateTransactionManager hibernate开发时事物管理器,整合hibernate。
void commit(TransactionStatus status) 根据状态提交
void rollback(TransactionStatus status) 根据状态回滚
//传播行为
int PROPAGATION_REQUIRED = 0;
int PROPAGATION_SUPPORTS = 1;
int PROPAGATION_MANDATORY = 2;
int PROPAGATION_REQUIRES_NEW = 3;
int PROPAGATION_NOT_SUPPORTED = 4;
int PROPAGATION_NEVER = 5;
int PROPAGATION_NESTED = 6;
//隔离级别
int ISOLATION_DEFAULT = -1;
int ISOLATION_READ_UNCOMMITTED = 1;
int ISOLATION_READ_COMMITTED = 2;
int ISOLATION_REPEATABLE_READ = 4;
int ISOLATION_SERIALIZABLE = 8;
//默认超时时间。默认值 -1.使用数据库底层的超时时间
int TIMEOUT_DEFAULT = -1;
int getPropagationBehavior(); 传播行为
int getIsolationLevel(); 隔离级别
int getTimeout(); 获得超时时间
boolean isReadOnly(); 是否只读(增删改:读写; 查询:只读)
String getName(); 配置事务详情名称。一般方法名称。例如:save,add* 等。
传播行为:在两个业务之间如何共享事务。
经常使用
PROPAGATION_REQUIRED PROPAGATION_REQUIRES_NEW PROPAGATION_NESTED
<?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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"
<!--组件扫描-->
<context:component-scan base-package="com.springdemo"></context:component-scan>
<bean id="dataSourceId" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=UTF-8"></property>
<property name="user" value="root"></property>
<property name="password" value=""></property>
</bean>
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSourceId"></property>
</bean>
<bean id="accountDaoId" class="com.springdemo.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="template"></property>
</bean>
<bean id="serviceId" class="com.springdemo.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDaoId"></property>
</bean>
<!--事务管理-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceId"></property>
</bean>
<!--
事务详情
<tx:attributes> 用于配置事务详情(属性属性)
<tx:method name=""/> 详情具体配置
propagation 传播行为 , REQUIRED:必须;REQUIRES_NEW:必须是新的
isolation 隔离级别
-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!--事务名称-->
<tx:method name="transfer" propagation="REQUIRED" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!--AOP编程,目标类有ABCD(4个连接点),切入点表达式-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.springdemo.service.impl.AccountServiceImpl.*(..))"></aop:advisor>
</aop:config>
</beans>
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!