Spring的事務管理

Spring的事務管理的兩種方式:一是編程式事務管理;二是配置式事務管理

配置式事務管理又包括兩種方式:①基於xml配置文件的方式②基於註解的方式

如下的代碼是spring整合了Hibernate後的代碼。

一、基於xml配置文件的方式

配置文件詳情:applicationContex.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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		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-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	
	<!-- 配置自動掃描的包:掃描指定路徑下帶有這些註解的類:@controller @service @repository @Component,自動註冊到spring容器-->
	<context:component-scan base-package="com.atguigu.spring.hibernate"></context:component-scan>
	
	<!-- 配置數據源 -->
	<!-- 導入資源文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>

		<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
		<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
	</bean>
	
	<!-- 配置 Hibernate 的 SessionFactory 實例: 通過 Spring 提供的 LocalSessionFactoryBean 進行配置 -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<!-- 配置數據源屬性 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 配置 hibernate 配置文件的位置及名稱 -->
		<!--  
		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
		-->
		<!-- 使用 hibernateProperties 屬性來配置 Hibernate 原生的屬性,不用hibernate.cfg.xml配置文件 -->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
		<!-- 配置 hibernate 映射文件的位置及名稱, 可以使用通配符 -->
		<property name="mappingLocations" 
			value="classpath:com/spring/hibernate/entities/*.hbm.xml"></property>
	</bean>

	<!-- 配置 Spring 的聲明式事務 -->
	<!-- 1. 配置事務管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<!-- 2. 配置事務屬性, 需要事務管理器 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true"/>
			<tx:method name="purchase" propagation="REQUIRED"/>
			<tx:method name="*"/>
		</tx:attributes>
	</tx:advice>

	<!-- 3. 配置事務切點, 並把切點和事務屬性關聯起來 -->
	<aop:config>
		<aop:pointcut expression="execution(* com.atguigu.spring.hibernate.service.*.*(..))" 
			id="txPointcut"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
	</aop:config>

</beans>

二、基於註解的方式

配置文件詳情:applicationContex.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"
	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-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<!-- 配置自動掃描的包 -->
	<context:component-scan base-package="com.atguigu.spring"></context:component-scan>	
	<!-- 導入資源文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	
	<!-- 配置 C3P0 數據源 -->
	<bean id="dataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>

		<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
		<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
	</bean>
	<pre name="code" class="html">        <!-- 配置 Hibernate 的 SessionFactory 實例: 通過 Spring 提供的 LocalSessionFactoryBean 進行配置 -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<!-- 配置數據源屬性 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 配置 hibernate 配置文件的位置及名稱 -->
		<!-- 注掉也可,用如下的配置 
		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
		-->
		<!-- 使用 hibernateProperties 屬性來配置 Hibernate 原生的屬性,不用hibernate.cfg.xml配置文件 -->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
		<!-- 配置 hibernate 映射文件的位置及名稱, 可以使用通配符 -->
		<property name="mappingLocations" 
			value="classpath:com/spring/hibernate/entities/*.hbm.xml"></property>
	</bean>
</pre><pre name="code" class="html">        <!-- 配置 Spring 的聲明式事務 -->
<!--1.配置事務管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!--2.啓用事務註解 --><tx:annotation-driven transaction-manager="transactionManager"/></beans>


在需要使用事務的方法上加註解:@Transactional   註解的屬性在後面的括號裏配置

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章