java之Spring事务回滚和Ehcache配置
来源:互联网 发布:协同设计软件 编辑:程序博客网 时间:2024/06/05 19:49
弄了一大早,终于配好了事务,事务的扫描包配好,Ehcache就是切面的问题,一切问题也迎刃而解。。。
一、Spring事务回滚
1、applicationContext.xml中配置
<!--spring 扫包 @Service ..... --> <context:component-scan base-package="com.yanhui"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan>
2、spring-mvc.xml配置
<!-- 扫描器--> <!-- <context:component-scan base-package="com.yanhui.*" /> --> <context:component-scan base-package="com.yanhui" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
3、applicationContext.xml开启事务
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- <tx:annotation-driven transaction-manager="transactionManager"/> --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="publish*" /> <tx:method name="save*" /> <tx:method name="add*" /> <tx:method name="update*" /> <tx:method name="insert*" /> <tx:method name="create*" /> <tx:method name="del*" /> <tx:method name="load*" /> <tx:method name="init*" /> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- AOP配置 --> <aop:config> <aop:pointcut id="myPointcut" expression="execution(public * com.yanhui.service..*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" /> </aop:config>
4、测试
mapper.insert(record);int a = 1/0;
二、Ehcache配置
1、引入jar包
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.6.11</version> </dependency>
2、新建一个cache-config.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:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd "> <cache:annotation-driven cache-manager="cacheManager"/> <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml"/> </bean> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="ehcacheManager"/> <property name="transactionAware" value="true"/> </bean></beans>
3、新建一个ehcache.xml
<?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <diskStore path="java.io.tmpdir"/> <defaultCache eternal="false" maxEntriesLocalHeap="1000" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="3600" timeToLiveSeconds="3600"/> <cache name="baseCache" eternal="false" maxEntriesLocalHeap="200" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="600" statistics="true" timeToLiveSeconds="600"/> <!-- eternal="false" // 元素是否永恒,如果是就永不过期(必须设置) maxElementsInMemory="1000" // 缓存容量的内存最大值(必须设置) overflowToDisk="false" // 当缓存达到maxElementsInMemory值是,是否允许溢出到磁盘(必须设置) diskPersistent="false" // 磁盘缓存在VM重新启动时是否保持(默认为false) timeToIdleSeconds="0" // 导致元素过期的访问间隔(秒为单位). 0表示可以永远空闲,默认为0 timeToLiveSeconds="600" // 元素在缓存里存在的时间(秒为单位). 0 表示永远存在不过期 memoryStoreEvictionPolicy="LFU" // 当达到maxElementsInMemory时,如何强制进行驱逐默认使用"最近使用(LRU)"策略,其它还有先入先出FIFO,最少使用LFU,较少使用LRU --></ehcache>
4、applicationContext.xml中加入
<import resource="cache-config.xml"/>
5、service.impl方法中
@Cacheable(value = "baseCache") public User findById(int id) { return userMapper.selectByPrimaryKey(id); }
6、测试
三、遇到的坑
spring、springMVC重复扫描导致事务失效的问题
意思是applicationContext.xml中扫描包和spring-mvc中的扫描包重复了,前者作为父容器先启动并配置了事务,然后spring-mvc.xml再启动,作为子容器也来管理包的内容,但是会覆盖前面applicationContext.xml对包的处理,而新的管理中并没有配置事务,所以事务就失败了。
经结论得出:
由于服务器启动时的加载配置文件的顺序为web.xml—root-context.xml(Spring的配置文件)—servlet-context.xml(SpringMVC的配置文件)。
解决办法:
Spring容器中不扫描Controller上的注解(即SpringMVC注解),SpringMVC层只负责扫描@Controller
阅读全文
0 0
- java之Spring事务回滚和Ehcache配置
- SPRING配置事务回滚
- java spring事务回滚
- spring aop事务配置,事务回滚
- spring mybatis事务回滚配置
- Spring事务和异常回滚
- Spring事务回滚和异常类
- Spring事务回滚
- spring事务回滚
- spring事务回滚
- spring事务回滚
- spring回滚事务
- spring 事务回滚
- spring 事务回滚
- spring事务回滚
- Spring事务回滚
- [Spring]事务回滚
- Spring事务回滚
- 完全二叉树的节点个数
- Building MFC application with /MD[d] (CRT dll version)requires MFC shared dll version
- 微信红包扫雷尾数软件挂
- codeforces #412
- 谈谈程序员解决问题的能力
- java之Spring事务回滚和Ehcache配置
- spring相关的jar包说明
- CentOS 7下编译安装Open Babel2.4.1和python绑定
- JVM内存泄漏工具
- 事务复习
- Spring AOP 实现原理
- HdU 1201-18岁生日
- HDU 4607 Park Visit (树的直径)
- 算法竞赛常见一类一题(大部分)