友系统中的配置:
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
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/aop
http://www.springframework.org/schema/aop/spring-aop-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/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-lazy-init="false">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:conf/db/jdbc.properties</value>
</list>
</property>
</bean>
<!-- 第一个数据库 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
<!-- Connection Pooling DBCP -->
<!-- 初始化时创建的连接数 -->
<property name="initialSize" value="5" />
<!-- 最大连接数据库连接数,设置为0时,表示没有限制; -->
<property name="maxActive" value="100" />
<!-- 最大等待连接中的数量,设置为0时,表示没有限制; -->
<property name="maxIdle" value="30" />
<!-- 最大等待秒数,单位为毫秒, 超过时间会报出错误信息; -->
<property name="maxWait" value="100000" />
<!-- 是否可用预执行 -->
<property name="poolPreparedStatements" value="true" />
<!-- 设置从数据源中返回的连接是否采用自动提交机制,默认值为 true;false支持事务 -->
<property name="defaultAutoCommit" value="false" />
</bean>
<!--根据dataSource和sql-map-config.xml创建一个SqlMapClient-->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="configLocation">
<value>classpath:/conf/sqlmap/sqlmap-config.xml</value>
</property>
</bean>
<context:annotation-config />
<context:component-scan base-package="com.woyo.business" />
<context:component-scan base-package="com.woyo.biz.lab.tools" />
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false"/>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- Transaction manager for a single JDBC DataSource -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="txInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
</bean>
<bean id="txBeanNameProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames">
<list>
<value>txInterceptor</value>
</list>
</property>
<property name="beanNames">
<value>*Service</value>
</property>
</bean>
</beans>
代码中,需要写:
@Transactional(rollbackFor=Exception.class)
public MessItemSaveVO saveMessItem(MessItemSaveVO messItemSaveVO) throws Exception {
旅游系统中的配置:
<!-- 事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="writeDataSource"/>
</bean>
<aop:config>
<aop:pointcut id="servicesPointcut" expression="execution(* com.woyoframework.travel.*.service.impl..*ServiceImpl.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicesPointcut" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="recommend*" propagation="REQUIRED" />
<tx:method name="cancel*" propagation="REQUIRED" />
<tx:method name="init*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="query*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
分享到:
相关推荐
在整个源代码分析中,我们可以看到 Spring 实现声明式事务管理有三个部分: 1. 对在上下文中配置的属性的处理,这里涉及的类是 TransactionAttributeSourceAdvisor,这是一个通知器,用它来对属性值进行处理,属性...
Spring 中的事务管理可以分为两种:编程式事务管理和声明式事务管理。编程式事务管理是指通过编程的方式来管理事务,而声明式事务管理是指通过配置的方式来管理事务。 事务管理的隔离级别 Spring 中的事务管理提供...
编程式事务管理允许开发者直接在代码中控制事务的开始、提交、回滚等操作。虽然这种方式灵活,但会导致大量重复的事务管理代码,增加维护难度。 示例: ```java Connection conn = null; try { conn = ...
Spring支持两种类型的事务管理方式:编程式事务管理和声明式事务管理。 #### 二、编程式事务管理 编程式事务管理允许开发人员通过编程的方式直接控制事务的开始、提交或回滚。这种方式相对灵活,但也存在一些缺点...
在Spring框架中,声明式事务管理是实现事务处理的一种高效且灵活的方式,它允许开发者通过XML配置或注解来定义事务边界,而无需在业务逻辑代码中显式地调用开始、提交或回滚事务的方法。这篇博文"Spring使用XML配置...
Spring事务管理主要包括两种类型:编程式事务管理和声明式事务管理。 - **编程式事务管理**:通过编写代码来控制事务的开始、提交或回滚等操作。这种方式灵活度高,但会使得代码变得冗余且难以维护。 - **声明式...
Spring事务管理主要分为两种方式:编程式事务管理和声明式事务管理。编程式事务管理是通过编写代码来控制事务的开始、提交、回滚等操作,而声明式事务管理则是通过配置或注解来定义事务边界,更加直观和易于使用。 ...
首先,开启Spring的声明式事务管理需要在配置类上使用`@EnableTransactionManagement`注解。这表示Spring将启用事务管理功能,并自动寻找并应用`@Transactional`注解。同时,我们需要配置一个`...
本主题将深入探讨Hibernate的编程式事务管理和Spring AOP的声明式事务管理,以及两者如何在实际项目中集成使用。 **Hibernate编程式事务管理** Hibernate作为流行的ORM(对象关系映射)框架,提供了对JDBC事务的...
接下来,我们将深入讨论Spring中的声明式事务管理。在Spring配置文件中,我们需要开启事务管理器,并在需要事务控制的Service层方法上添加`@Transactional`注解。这个注解允许我们指定事务的传播行为(如REQUIRED、...
Spring的声明式事务管理基于其面向切面编程(AOP)框架实现,事务管理器会拦截事务方法的调用,根据配置的事务属性自动进行事务的开启、提交、回滚等操作。 7. **Spring 2.x及更高版本的改进** 随着Spring的发展,...
声明式事务管理依赖于Spring的AOP机制,它可以在不修改业务代码的情况下,通过代理对象拦截方法调用,实现事务的自动控制。AOP允许我们定义“切面”(Aspect),即关注点的模块化,如事务管理就是一个典型的横切...
首先,Spring事务管理有两种主要模式:编程式事务管理和声明式事务管理。编程式事务管理通过调用`PlatformTransactionManager`接口提供的方法进行显式控制,如`beginTransaction()`, `commit()`, 和`rollback()`。...
Spring 声明式事务是指使用 Spring 框架提供的声明式事务管理机制,以便简化事务处理的复杂度。声明式事务可以通过配置文件或注解的方式来实现事务管理。例如,使用 @Transactional 注解来标注事务方法。 @Aspect ...
声明式事务管理是建立在 AOP 之上的,它的本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加入一个事务,在执行完目标方法之后根据执行情况提交或者回滚事务。声明式事务管理的最大优点是它可以将事务...
在本“spring AOP(声明式事务管理)小程序”中,我们将深入探讨Spring AOP如何实现声明式事务管理,以及相关的通知类型。 1. **什么是声明式事务管理**: 声明式事务管理是相对于编程式事务管理而言的,后者需要在...
Spring 声明式事务处理与多数据源支持 在大部分涉及到数据库操作的项目里面,事务控制、事务处理都是一个无法回避的问题。Spring 框架提供了声明式事务处理机制,使得业务代码中进行事务控制操作起来非常简单。只需...
根据提供的文件信息,本文将详细解析Spring 1.2中声明式事务管理的相关知识点,包括其配置方式、工作原理以及如何在实际应用中实施。 ### Spring 1.2声明式事务简介 Spring框架中的声明式事务管理是通过AOP(面向...
本教程将深入探讨如何在Spring框架中利用`TransactionInterceptor`进行声明式事务管理,与Hibernate集成实现高效的数据库事务控制。 首先,了解事务管理是至关重要的。事务是一组数据库操作,这些操作要么全部成功...
Spring提供了多种事务管理方式,包括编程式事务管理和声明式事务管理。在这篇DEMO中,我们将重点探讨Spring的声明式事务处理以及AOP(面向切面编程)的相关应用。 首先,让我们了解一下AOP。AOP是Spring框架的一个...