Hibernate4+Spring3+SpringMVC事务管理
2014-04-21 13:51
267 查看
一,Hibernate4配置
1,首先将hibernate配置信息抽到hibernate.properties里面dataSource.password=111111
dataSource.username=root dataSource.databaseName=goodsmanage dataSource.driverClassName=com.mysql.jdbc.Driver dataSource.dialect=org.hibernate.dialect.MySQL5Dialect dataSource.serverName=localhost:3306 dataSource.url=jdbc:mysql://localhost:3306/goodsmanage dataSource.properties=user=${dataSource.username};databaseName=${dataSource.databaseName};serverName=${dataSource.serverName};password=${dataSource.password} dataSource.hbm2ddl.auto=update dataSource.show_sql=true hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect hibernate.hbm2ddl.auto=none hibernate.show_sql=false hibernate.query.substitutions=true 1, false 0 hibernate.default_batch_fetch_size=16 hibernate.max_fetch_depth=2 hibernate.bytecode.use_reflection_optimizer=true hibernate.cache.use_second_level_cache=true hibernate.cache.use_query_cache=true hibernate.cache.region.factory_class=org.hibernate.cache.EhCacheRegionFactory net.sf.ehcache.configurationResourceName=/ehcache_hibernate.xml hibernate.cache.use_structured_entries=true hibernate.generate_statistics=true proxool.maximum.connection.count=40 proxool.minimum.connection.count=5 proxool.statistics=1m,15m,1h,1d proxool.simultaneous.build.throttle=30,
2,hibernate4已经不在支持使用HibernateTemplate模板类,这里需要将hibernate的sessionFactory和dataSource注入到Spring的事务管理器
SpringContext.xml的配置为
<context:property-placeholder location="classpath:/hibernate.properties" /> <!-- Hibernate 参数配置 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描指定Package中的@Entity Class --> <property name="packagesToScan" value="com.geng.goodsmanage" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${dataSource.dialect}</prop> <prop key="hibernate.generate_statistics">true</prop> <prop key="hibernate.connection.release_mode">auto</prop> <prop key="hibernate.hbm2ddl.auto">${dataSource.hbm2ddl.auto}</prop> <prop key="hibernate.show_sql">${dataSource.show_sql}</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.query.substitutions">${hibernate.query.substitutions}</prop> <prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop> <prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop> <prop key="hibernate.bytecode.use_reflection_optimizer">${hibernate.bytecode.use_reflection_optimizer}</prop> <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop> <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop> <prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop> <prop key="net.sf.ehcache.configurationResourceName">${net.sf.ehcache.configurationResourceName}</prop> <prop key="hibernate.cache.use_structured_entries">${hibernate.cache.use_structured_entries}</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> <property name="dataSource" ref="dataSource" /> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${dataSource.driverClassName}" /> <property name="url" value="${dataSource.url}" /> <property name="username" value="${dataSource.username}" /> <property name="password" value="${dataSource.password}" /> </bean>
3,Hibernate4默认必须为开启事务,否则 getCurrentSession()获取不到
2,Spring3声明事务管理
声明事务配置变化不大<!-- 定义AOP拦截器 --> <aop:config proxy-target-class="true"> <aop:advisor pointcut="execution(* com.geng..*Service.*(..))" advice-ref="txAdvice" /> <!-- <aop:advisor pointcut="execution(* org.geng..*DAO.*(..))" advice-ref="txAdvice" /> --> </aop:config> <!-- 基本事务定义,使用transactionManager作事务管理,默认get*方法的事务为readonly,其余方法按默认设置 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="create*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="merge*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="del*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="remove*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="put*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="use*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到 --> <tx:method name="get*" propagation="REQUIRED" read-only="true" /> <tx:method name="count*" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="list*" propagation="REQUIRED" read-only="true" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice>
总结:编程时需注意业务上的事务范围,如生成订单,他的事务需在一个service方法里面需要先将货物数量减去,做update,然后再在订单表插入订单,如果这两步操作放在两个service方法里面恐怕异常就无法做到同时回滚。
3,SpringMVC主要配置
<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" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!-- 会自动注册了validator ConversionService --> <mvc:annotation-driven /> <!-- 防止图片js csss被拦截 --> <!-- <mvc:default-servlet-handler /> --> <!-- 对静态资源文件的访问 方案二 (二选一)--> <mvc:resources mapping="/**" location="/" /> <!-- <mvc:resources mapping="/images/**" 4000 location="/images/" cache-period="31556926"/> <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/> <mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/> --> <context:component-scan base-package="com.geng.goodsmanage"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan> <!-- 定义首页 --> <mvc:view-controller path="/" view-name="forward:/login/toLogin/" /> <!-- 视图的前缀和后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
4,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/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>goodsmanage</display-name> <!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔 此参数用于后面的Spring Context Loader --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc.xml, classpath*:/applicationContext*.xml </param-value> </context-param> <servlet> <servlet-name>goodsmanage</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>goodsmanage</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Filter --> <filter> <filter-name>hibernateFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>singleSession</param-name> <param-value>true</param-value> </init-param> </filter> <!-- Character Encoding filter --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- opensession --> <filter> <filter-name>hibernateFilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>sessionFactoryBeanName</param-name> <param-value>sessionFactory</param-value> </init-param> </filter> <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping> <!-- 容器啟動時 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
相关文章推荐
- Spring3整合Hibernate3.6之二:用AOP管理事务处理
- Hibernate —— Hibernate 的事务管理
- Spring 使用Hibernate事务管理:update等操作无效果问题
- Java事务管理之Hibernate
- Spring与Hibernate的整合,不配置事务管理器,事务会自动提交(Hibernate默认手动提交)
- 85-002-3 spring与hibernate的整合(采用AOP来管理事务实现声明式事务)
- SpringMVC + Spring + MyBatis 学习笔记:SpringMVC和Spring一同工作的时候,AOP事务管理不起作用的解决方法
- Hibernate第五章知识点总结——第五章--事务管理
- spring与hibernate整合事务管理的理解
- Spring管理Hibernate事务
- springmvc事务管理详解
- spring hibernate 事务管理
- SpringMVC学习系列(12) 完结篇 之 基于Hibernate+Spring+Spring MVC+Bootstrap的管理系统实现
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之五:Hibernate的事务管理、手动回滚
- Spring和Hibernate整合时对session及事务管理的理解
- Spring对Hibernate事务管理
- spring hibernate整合事务管理器bean初始化失败
- Hibernate的事务管理
- pring3、hibernate4、JPA整合(包括很久未解决的事务管理问题)
- hibernate-事务管理