JDBC如何实现事务

JDBC如何实现事务

JDBC中处理事务,都是通过Connection完成的。

同一事务中所有的操作,都在使用同一个Connection对象。

JDBC中的事务

Connection的三个方法与事务有关:

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:task="http://www.springframework.org/schema/task"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.1.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.1.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

http://www.springframework.org/schema/task

http://www.springframework.org/schema/task/spring-task-3.1.xsd">

<context:property-placeholder location="classpath:jdbc.properties"/>

<!--配置c3p0连接池-->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<property name="driverClass" value="${jdbc.Driver}"/>

<property name="jdbcUrl" value="${jdbc.URL}"/>

<property name="user" value="${jdbc.USERNAME}"/>

<property name="password" value="${jdbc.PASSWD}"/>

</bean>

<!--配置业务层类-->

<bean id="accountService" class="com.spring.demo4.AccountServiceImp">

<property name="accountDao" ref="accountDao"/>

</bean>

<!--配置Dao的类-->

<bean id="accountDao" class="com.spring.demo4.AccountDaoImp">

<property name="dataSource" ref="dataSource"/>

</bean>

<!--配置事务管理器-->

<bean id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource"/>

</bean>

<!--开启注解事务 打开事务驱动-->

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

</beans>setAutoCommitboolean:设置是否为自动提交事务,如果true(默认值为true)表示自动提

交,也就是每条执行的SQL语句都是一个单独的事务,如果设置为false,那么相当于开启了事务

了; con.setAutoCommit(false) 表示开启事务。

commit():提交结束事务。

rollback():回滚结束事务。

嵌套事务实现

spring 事务嵌套:外层事务TraB,内层事务TraATraC

示例代码

try{

con.setAutoCommit(false);//开启事务

......

con.commit();//try的最后提交事务

} catch() {

con.rollback();//回滚事务

}

场景1

TraATraC @Transactional(默认REQUIRED

TraB:

traA.update(order1); (traA.update throw new RuntimeException();)

traC.update(order2);

结果:内外层事务全部回滚;

场景2

TraATraC @Transactional(默认REQUIRED

TraB:

traA.update(order1); (traA.update throw new RuntimeException();try catch

traC.update)

traC.update(order2);

结果:内外层事务全部不回滚,traAtry catch后的事务提交;

场景3

TraATraC @Transactional(默认REQUIRED

TraB: try{(traA.update throw new RuntimeException();

在外层TraB try catch TraA)

traA.update(order1);

}catch(){}

traC.update(order2);

结果:内外层事务全部回滚,内层的异常抛出到外层捕获也会回滚;

场景4

TraA @Transactional(propagation=Propagation.REQUIRES_NEW)TraC

@Transactional(默认REQUIRED

TraB:

traA.update(order1); (traA.update throw new RuntimeException();)

traC.update(order2);

结果:内层事务回滚,外层事务继续提交

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伟大先锋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值