How to configure Hibernate transaction in Spring

 In order to ensure data consistency, it is often necessary to introduce the concept of transactions during programming. Transactions have 4 properties: atomicity, consistency, isolation, and durability.

         There are two types of transactions: programmatic transactions and declarative transactions. Programmatic transaction is to place transaction processing in the program, while declarative transaction is to operate through configuration files or annotations.

         In Spring, there is the concept of declarative transaction. Through the integration with Hibernate-like framework, declarative transaction can be well completed.

         In fact, no matter how many ways to configure Hibernate transactions in Spring, you can't escape the following:

         1. Configure SessionFactory

         2. Configure the transaction container

         3. Configure transaction rules

         4. Configure transaction entry

         A total of 4 ways to configure Hibernate transactions are provided later.

         First, let's talk about configuring the SessionFactory. There are two ways to configure the SessionFactory. One is to configure the SessionFactory by configuring the location of the hibernate.cfg.xml file, and the other is to manually configure the data source in the Spring configuration file.

         The following are two ways to configure SessionFactory (the second configuration requires the introduction of two additional packages: commons-dbcp, commons-pool)

	<!-- 1. The first way to configure SessionFactory-->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation" value="classpath:hibernate.cfg.xml" />
	</bean>
	
	<!-- 2、第二种配置SessionFactory的方式 -->
	<!-- 2.1配置数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/hibernate_cache"></property>
		<property name="username" value="root"></property>
		<property name="password" value="admin"></property>
	</bean>
	<!-- 2.2、配置SessionFactory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
		<property name="mappingLocations">
			<list>
				<value>classpath:实体对应xml的路径</value>
			</list>
		</property>
	</bean>

         至此Hibernate就成功的将SessionFactory交给了Spring来管理。现在再来看Spring是怎样管理Hibernate事务的吧。

         第一种方式,利用tx标签配置事务。

	<!-- 配置事务容器 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<!-- 定义事务规则 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="modify*" propagation="REQUIRED" />
			<tx:method name="del*" propagation="REQUIRED" />
			<tx:method name="*" propagation="REQUIRED" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<!-- 定义事务入口 -->
	<aop:config>
		<aop:pointcut id="allDaoMethod" expression="execution(* com.jianxin.dao.*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="allDaoMethod" />
	</aop:config>

         第二种,用代理进行配置

	<!-- 配置事务容器 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<!-- 定义事务规则 -->
	<bean id="transactionProxy"
		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
		abstract="true">
		<property name="transactionManager" ref="transactionManager" />
		<property name="transactionAttributes">
			<props>
				<!-- ,回滚为-,不回滚为+ -->
				<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="modify*">PROPAGATION_REQUIRED,+MyException</prop>
				<prop key="del*">PROPAGATION_REQUIRED</prop>
				<prop key="*">READONLY</prop>
			</props>
		</property>
	</bean>
	<!-- 定义事务入口 -->
	<bean id="userDaoProxy" parent="transactionProxy">
		<property name="target" ref="userDao"></property>
	</bean>

         第三种,利用拦截器

	<!-- 配置事务容器 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<!-- 定义事务规则 -->
	<bean id="transactionInterceptor"
		class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<property name="transactionManager" ref="transactionManager" />
		<property name="transactionAttributes">
			<props>
				<!-- 回滚为-,不回滚为+ -->
				<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="modify*">PROPAGATION_REQUIRED,+MyException</prop>
				<prop key="del*">PROPAGATION_REQUIRED</prop>
				<prop key="*">READONLY</prop>
			</props>
		</property>
	</bean>
	<!-- 定义事务入口 -->
	<bean id="proxyFactory"
		class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<property name="interceptorNames">
			<list>
				<value>transactionInterceptor</value>
			</list>
		</property>
		<property name="beanNames">
			<list>
				<value>*Dao</value>
			</list>
		</property>
	</bean>

         第四种,利用注解。

         首先,在配置文件中写入下面语句,打开注解功能

	<!-- 开户事务注解功能 -->
	<tx:annotation-driven transaction-manager="transactionManager" />

         然后用@Transactional对类或者方法进行标记,如果标记到类上,那么次类中所有方法都进行事务回滚处理,在类中定义Transactional的时候,它有propagation、rollbackFor、noRollbackFor等属性,此属性是用来定义事务规则,而定义到哪这个就是事务入口。

         纵观以上四种在Spring中配置Hibernate事务的方法,其核心都是一样的,不同的只是实现的方式而已。所以看到这,这篇博文中你只需要记住4句话,就可以轻松理解在Spring中配置Hibernate事务的核心:

         1.配置SessionFactory

         2.配置事务容器

         3.配置事务规则

         4.配置事务入口


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325385837&siteId=291194637