报错问题
在使用IDEA进行springmvc+spring+hibernate框架单元测试的时候,只能单个测试方法正确运行,一旦多个测试方法便报错。
报错报文
Caused by: net.sf.ehcache.CacheException: Another CacheManager with same name already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
- Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
- Shutdown the earlier cacheManager before creating new one with same name.
The source of the existing CacheManager is: URLConfigurationSource net.sf.ehcache.CacheManager.assertNoCacheManagerExistsWithSameName(CacheManager.java:626)
at net.sf.ehcache.CacheManager.init(CacheManager.java:391)
at net.sf.ehcache.CacheManager.(CacheManager.java:269)
at org.hibernate.cache.ehcache.EhCacheRegionFactory.start(EhCacheRegionFactory.java:74)
… 81 more
解决办法
找到数据库的配置文件,添加或修改配置
hibernate.cache.region.factory_class = org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
hibernate.cache.provider_class = net.sf.ehcache.hibernate.SingletonEhCacheProvider
将EhCacheRegionFactory和EhCacheProvider设置为单例模式,也就是保证每个方法共享同一个缓存管理器。