Spring Boot集成Infinispan启动失败:无法获取文件系统锁求助
错误现象
应用首次启动无异常,二次启动时抛出锁获取失败错误:
Caused by: org.infinispan.manager.EmbeddedCacheManagerStartupException: ISPN029025: Failed acquiring lock 'FileSystemLock{directory=.\cache1\0\RT12-CACHE, file=RT12-CACHE.lck}' for SIFS
at org.infinispan.manager.DefaultCacheManager.internalStart(DefaultCacheManager.java:781)
at org.infinispan.manager.DefaultCacheManager.start(DefaultCacheManager.java:746)
at org.infinispan.manager.DefaultCacheManager.(DefaultCacheManager.java:412)
at org.infinispan.manager.DefaultCacheManager.(DefaultCacheManager.java:361)
at org.infinispan.manager.DefaultCacheManager.(DefaultCacheManager.java:336)
at org.infinispan.manager.DefaultCacheManager.(DefaultCacheManager.java:324)
现有缓存配置
使用的local-cache-configuration片段:
<local-cache-configuration name="default-template-1" statistics="true"> <expiration lifespan="129600000"/> <persistence> <file-store path="./cache1/1"/> </persistence> </local-cache-configuration> <local-cache-configuration name="template-no-cache" statistics="true"> <expiration lifespan="0"/> <persistence> <file-store path="./cache1/0"/> </persistence> </local-cache-configuration> <local-cache-configuration name="template-60-min-cache" statistics="true"> <expiration lifespan="3600000"/> <persistence> <file-store path="./cache1/0"/> </persistence> </local-cache-configuration> <local-cache-configuration name="template-1-min-cache" statistics="true"> <expiration lifespan="60000"/> <persistence> <file-store path="./cache1/0"/> </persistence> </local-cache-configuration> <local-cache-configuration name="template-10-min-cache" statistics="true"> <expiration lifespan="600000"/> <persistence> <file-store path="./cache1/0"/> </persistence> </local-cache-configuration> <local-cache-configuration name="template-20-sec-cache" statistics="true"> <expiration lifespan="20000"/> <persistence> <file-store path="./cache1/1"/> </persistence> </local-cache-configuration> <local-cache-configuration name="template-cm-3" statistics="true"> <!-- for 60 minutes--> <expiration lifespan="3600000"/> <persistence> <file-store path="./cache1/1"/> </persistence> </local-cache-configuration>
版本信息
- Spring Boot 3.2.2
- infinispan-spring-boot-starter-embedded 14.0.28.Final
- infinispan-core 15.0.3.Final
解决方案
1. 修复缓存目录冲突(核心解决办法)
多个缓存模板共享了同一个file-store路径(如./cache1/0被4个模板复用),Infinispan会为每个缓存的存储目录加锁,共享目录会导致锁竞争,启动时无法获取锁。
给每个缓存模板分配独立的存储路径,示例:
<local-cache-configuration name="template-no-cache" statistics="true"> <expiration lifespan="0"/> <persistence> <file-store path="./cache1/template-no-cache"/> </persistence> </local-cache-configuration> <local-cache-configuration name="template-60-min-cache" statistics="true"> <expiration lifespan="3600000"/> <persistence> <file-store path="./cache1/template-60-min-cache"/> </persistence> </local-cache-configuration>
2. 开发环境临时优化
如果是开发调试场景,可通过配置避免锁残留:
- 添加锁超时时间,超时后强制尝试获取锁:
<file-store path="./cache1/0" lock-timeout="30000"/>
- 开启启动时清理存储目录(生产环境禁用,会丢失缓存数据):
<file-store path="./cache1/0" purge-on-startup="true"/>
3. 确保应用优雅关闭
锁残留可能源于上次关闭时未正常释放锁:
- 检查Spring配置中
EmbeddedCacheManager的销毁回调,Spring Boot Infinispan Starter默认会处理,也可手动指定:
@Bean(destroyMethod = "stop") public EmbeddedCacheManager infinispanCacheManager() { return new DefaultCacheManager("infinispan.xml"); }
- 在IDEA中停止应用时,使用Stop按钮而非强制终止,避免进程残留。
4. 统一版本兼容性
当前infinispan-spring-boot-starter-embedded(14.x)和infinispan-core(15.x)跨大版本,可能存在兼容性问题,建议将infinispan-core版本降级到14.0.28.Final,与starter版本保持一致。
内容的提问来源于stack exchange,提问作者Emindu

