要了解事務配置的全部方法,請看一下《Spring事務配置的5種方法》html
本文介紹兩種配置方法:java
如下所使用環境爲Spring4.0.三、Hibernate4.3.5mysql
Entity類User.java,持久化類,對應數據庫表userweb
package com.lei.demo.entity; import javax.persistence.*; @Entity(name="users") public class Users { public Users(){ super(); } @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id") private Integer id; @Column(name="user_name",length=32) private String user_name; @Column(name="age") private Integer age; @Column(name="nice_name",length=32) private String nice_name; //屬性實現...... }
UserDAO.javar,表user的一些操做,其中屬性sessionFactory應該由Spring注入,以下:spring
package com.lei.demo.dao; import java.util.List; import javax.annotation.Resource; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.stereotype.Repository; import com.lei.demo.entity.Users; public class UsersDAO { private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public SessionFactory getSessionFactory() { return sessionFactory; } public List<Users> getAllUser(){ String hsql="from users"; Session session = sessionFactory.getCurrentSession(); Query query = session.createQuery(hsql); return query.list(); } }
UserService.java,業務實現類,以下sql
package com.lei.demo.service; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.lei.demo.dao.*; public class UserService { private UsersDAO userDao; public int userCount(){ return userDao.getAllUser().size(); } public UsersDAO getUserDao() { return userDao; } public void setUserDao(UsersDAO userDao) { this.userDao = userDao; } }
首先看一下xml配置,spring-hibernate.xml以下:數據庫
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd "> <!-- Hibernate4 --> <!-- 加載資源文件 其中包含變量信息,必須在Spring配置文件的最前面加載,即第一個加載--> <context:property-placeholder location="classpath:persistence-mysql.properties" /> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <!-- 能夠加多個包 --> <value>com.lei.demo.entity</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <!-- <prop key="hibernate.current_session_context_class">thread</prop> --> </props> </property> </bean> <!-- 數據庫映射 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.user}" /> <property name="password" value="${jdbc.pass}" /> </bean> <!-- 配置Hibernate事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 配置事務異常封裝 --> <bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> <!-- 聲明式容器事務管理 ,transaction-manager指定事務管理器爲transactionManager --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="get*" propagation="REQUIRED" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <aop:config expose-proxy="true"> <!-- 只對業務邏輯層實施事務 --> <aop:pointcut id="txPointcut" expression="execution(* com.lei.demo.service..*.*(..))" /> <!-- Advisor定義,切入點和通知分別爲txPointcut、txAdvice --> <aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/> </aop:config> </beans>
其中主要配置中是tx:advice和aop:config兩個配置節,以Spring AOP的方式實現事務管理。express
tx:advice配置了事務的管理者是transactionManager,同時tx:method也規定了若是方法名匹配「add*」和「get*」方法時使用事務,propagation是設定事務的傳播級別。除了「add*」和「get*」方法,其餘的方法的事務是隻讀的(典型地,對於只執行查詢的事務你會將該屬性設爲true,若是出現了更新、插入或是刪除語句時只讀事務就會失敗)apache
aop:config指定了一個aop:pointcut去引用上邊的advice。spring-mvc
這樣就經過AOP的攔截機制實現了事務,固然你還要用Spring的方式本身配置UserDAO和UserService。
第一步,首先看一下web.xml,以下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>lei-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/lei-dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>lei-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
第二步,spring-hibernate配置,見如下spring-hibernate.xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd "> <!-- Hibernate4 --> <!-- 加載資源文件 其中包含變量信息,必須在Spring配置文件的最前面加載,即第一個加載--> <context:property-placeholder location="classpath:persistence-mysql.properties" /> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <!-- 能夠加多個包 --> <value>com.lei.demo.entity</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <!-- <prop key="hibernate.current_session_context_class">thread</prop> --> </props> </property> </bean> <!-- 數據庫映射 --> <!-- class="org.apache.tomcat.dbcp.dbcp.BasicDataSource" --> <!-- class="org.springframework.jdbc.datasource.DriverManagerDataSource" --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.user}" /> <property name="password" value="${jdbc.pass}" /> </bean> <!-- 配置Hibernate事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 配置事務異常封裝 --> <bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> </beans>
第一節中xml配置事務中須要經過配置tx:advice和aop:config來增長事務的功能。此處採用全註釋方法,這兩個配置節就不須要了。
相應的須要在視圖解析配置中啓用註釋,以下lei-dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd "> <!-- 啓動自動掃描 該包下全部的Bean(@Controller) --> <context:component-scan base-package="com.lei.demo" /> <!-- 基於註釋的事務,當註釋中發現@Transactional時,使用id爲「transactionManager」的事務管理器 --> <!-- 若是沒有設置transaction-manager的值,則spring以缺省默認的事務管理器來處理事務,默認事務管理器爲第一個加載的事務管理器 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 定義視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/user/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>
UserDAO以下
package com.lei.demo.dao; import java.util.List; import javax.annotation.Resource; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.stereotype.Repository; import com.lei.demo.entity.Users; @Repository public class UsersDAO { @Resource(name="sessionFactory") private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public SessionFactory getSessionFactory() { return sessionFactory; } public List<Users> getAllUser(){ String hsql="from users"; Session session = sessionFactory.getCurrentSession(); Query query = session.createQuery(hsql); return query.list(); } }
UserService.java以下
package com.lei.demo.service; import javax.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.lei.demo.dao.*; @Service("userService") public class UserService { @Resource private UsersDAO userDao; @Transactional public int userCount(){ return userDao.getAllUser().size(); } public UsersDAO getUserDao() { return userDao; } public void setUserDao(UsersDAO userDao) { this.userDao = userDao; } }
這裏,方法名userCount上加入@Transactional,說明這個方法要啓用事務。若是類名UserService上加入@Transactional,則代表這個類中的全部方法都會啓用事務。
若是配有多個transactionManager,例如配置有transactionManager1,和transactionManager2,則能夠經過@Transactional(「transactionManager1」),的方式指定使用哪一個數據源的事務。
源代碼下載:
http://www.oschina.net/code/snippet_1764868_35775