事物管理

jdbc事务管理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: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-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">

<!-- 自动扫描与装配bean -->
<context:component-scan base-package="cn.itcast.spring.p_jdbc"></context:component-scan>

<!-- 加载外部的配置文件 -->
<context:property-placeholder location="classpath:cn/itcast/spring/p_jdbc/jdbc.properties"/>

<!-- 一、配置数据库连接池 ComboPooledDataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 基本的连接信息 -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="userName" value="${username}"></property>
<property name="password" value="${password}"></property>
<!-- 一些管理的配置 -->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--连接池中保留的最小连接数。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="5"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!--最大空闲时间,1800秒内未使用则连接被丢弃,若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>

<!-- 二、配置JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 三、配置声明式事务管理 -->

<!-- 声明“事务管理器” -->
<bean id="dsTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- ==== 基于注解的方式配置事务 ==== -->
<tx:annotation-driven transaction-manager="dsTransactionManager"/>

<!-- ==== 基于XML的方式配置事务 ==== -->
<!-- 声明“通知”:
对于以save或update或delete开头的方法,使用正常的事务,其他的方法都使用只读的事务
read-only:表示事务是否是只读的,默认为false。
<tx:advice id="transactionManager" transaction-manager="dsTransactionManager">
<tx:attributes>
<tx:method name="save*" read-only="false"/>
<tx:method name="update*"/>
<tx:method name="delete*"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
-->
<!-- 声明“切面”
<aop:config>
<aop:advisor advice-ref="transactionManager" pointcut="execution(* cn.itcast.spring.p_jdbc.*Dao.*(..))"/>
</aop:config>
-->

</beans>

Hibernate事务管理

<?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-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">

<!-- 自动扫描与装配bean -->
<context:component-scan base-package="cn.itcast.spring.q_tx"></context:component-scan>

<!-- 加载外部的配置文件 -->
<context:property-placeholder
location="classpath:cn/itcast/spring/p_jdbc/jdbc.properties" />

<!-- 一、配置数据库连接池 ComboPooledDataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 基本的连接信息 -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${username}"></property>
<property name="password" value="${password}"></property>
<!-- 一些管理的配置 -->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--连接池中保留的最小连接数。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="5"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!--最大空闲时间,1800秒内未使用则连接被丢弃,若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>

<!-- 二、配置JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 三、配置声明式事务管理 -->

<!-- 声明“事务管理器” -->
<bean id="dsTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- ==== 基于注解的方式配置事务 ==== -->
<tx:annotation-driven transaction-manager="dsTransactionManager" />

</beans>

时间: 2024-08-30 00:39:55

事物管理的相关文章

spring事物管理--声明式(AspectJ)(推荐使用)

1.表结构及数据 2.需引入的jar包: 3.业务层(Service).持久层(Dao)接口与实现类 Service接口: //转账案例业务层接口 public interface AccountService { /** * @param out :转出账号 * @param in :转入账号 * @param money :转账金额 */ public void transfer(String out,String in,Double money); } Service实现类: //转账案例

Spring学习——声明式事物管理

1.什么是事物? 事务是一组操作的执行单元,相对于数据库操作来讲,事务管理的是一组SQL指令,比如增加,修改,删除等,事务的一致性,要求,这个事务内的操作必须全部执行成功,如果在此过程种出现了差错,比如有一条SQL语句没有执行成功,那么这一组操作都将全部回滚! 2.事物的四大特性: Atomic(原子性):要么都成功,要么都失败; Consistent(一致性):数据应该不被破坏; Isolate(隔离性):用户间操作不相混淆 ; Durable(持久性):永久保存 3.实际开发中,需要事物控制

声明事物管理器

<?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&q

[原创]java WEB学习笔记109:Spring学习---spring中事物管理

博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱好者,互联网技术发烧友 微博:伊直都在0221 QQ:951226918 ------------------------------------------------------------------------------------------------------------------

Spring中的事物管理,用 @Transactional 注解声明式地管理事务

事物: 事务管理是企业级应用程序开发中必不可少的技术,  用来确保数据的 完整性和 一致性. 事务就是一系列的动作, 它们被当做一个单独的工作单元. 这些动作要么全部完成, 要么全部不起作用 事务的四个关键属性: 原子性:事务是一个原子操作, 由一系列动作组成. 事务的原子性确保动作要么全部完成要么完全不起作用. 一致性:一旦所有事务动作完成, 事务就被提交. 数据和资源就处于一种满足业务规则的一致性状态中. 隔离性:可能有许多事务会同时处理相同的数据, 因此每个事物都应该与其他事务隔离开来,

集成Spring事物管理

单独使用MyBatis对事物进行管理 前面MyBatis的文章有写过相关内容,这里继续写一个最简单的Demo,算是复习一下之前MyBatis的内容吧,先是建表,建立一个简单的Student表: create table student ( student_id int auto_increment, student_name varchar(20) not null, primary key(student_id) ) 建立实体类Student.java: public class Studen

spring 事物管理

1 在spring中支持编程式事物和声明式事务管理,通常使用声明式事物管理,声明式的事物管理是基于aop机制实现的使用很方便. 2 spring支持单一数据库资源的事物管理和跨越多个数据库资源的事物管理既JTA全局事物. 3 在spring中提供了多个事物管理类,常用的是;DataSourceTransactionManager,HibernateTransactionManager和JtaTransactionManager. DataSourceTransactionManager :数据源

spring事物管理--声明式(AspectJ)注解实现 (推荐使用)

1.表结构及数据 2.使用的jar包 3.service.Dao层接口与实现类: Dao接口: //转账案例持久层接口 public interface AccountDao { /** * @param out :转出账号 * @param money :转账金额 */ public void outMoney(String out,Double money); /** * @param in :转入账号 * @param money :转账金额 */ public void inMoney(

Spring中的事物管理,基于spring的bean的配置

很多东西与上边的相同,这儿只简介: 导包... 数据库中建立三个表... 建立存放连接数据库的file文件:jdbc.properties: ----------------------------------------------------------------- com.atguigu.spring.tx.xml包下建立, 接口:BookShopDao 类:BookShopDaoImpl 继承于接口,BookShopDao 异常处理类:BookStockException 测试类:JU

MyBatis6:MyBatis集成Spring事物管理(下篇)

前言 前一篇文章<MyBatis5:MyBatis集成Spring事物管理(上篇)>复习了MyBatis的基本使用以及使用Spring管理MyBatis的事物的做法,本文的目的是在这个的基础上稍微做一点点的进阶:多数据的事物处理.文章内容主要包含两方面: 1.单表多数据的事物处理 2.多库/多表多数据的事物处理 这两种都是企业级开发中常见的需求,有一定的类似,在处理的方法与技巧上又各有不同,在进入文章前,先做一些准备工作,因为后面会用到多表的插入事物管理,前面的文章建立了一个Student相关