最近在项目配置事务时报如下异常,在此请大家帮忙看看,异常信息如下:
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
问题描述:
事务配置文件配置片段如下:
tx:attributes
/tx:attributes
/tx:advice
在程序中有一个服务层方法,调用了以find开头的方法时报上面的异常。不知为什么?
另外对查询类方法配事务时有必要指定read-only属性吗,它本身不就是只读的吗,还有指定吗?
查询类方法配事务时当然有必要指定read-only属性为true,
这样就可以防止你在写查询方法的时候又进得了数据库修改操作
你把报异常的那个方法代码粘出来,应该是你在那个查询方法里又进行了数据库修改操作
去掉rollback-for="Exception"看看
[quote]
Write operations are not allowed in read-only mode
[/quote]
这说明了你的read-only的事务执行了写的操作引发的异常。你有必要检查一下所有的
get*,find*,load* 方法,保证没有写的操作。