企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
工程中默认使用声明式事务,事务控制在service层,规则定义在transaction.xml文件中 * 以select、query、get等开头的方法为只读事务 * 以insert、create、save、do等开头的方法走事务 所以当控制台报Connection is read-only错误时就要注意下service中方法开头了 ``` Caused by: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ``` transaction.xml文件内容 ``` <!-- Spring 通知配置 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="select*" read-only="true"/> <tx:method name="query*" read-only="true"/> <tx:method name="get*" read-only="true"/> <tx:method name="load*" read-only="true"/> <tx:method name="list*" read-only="true"/> <tx:method name="insert*" read-only="false" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="create*" read-only="false" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="save*" read-only="false" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="submit*" read-only="false" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="update*" read-only="false" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="modify*" read-only="false" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="del*" read-only="false" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="remove" read-only="false" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="do*" read-only="false" propagation="REQUIRED" rollback-for="Throwable" /> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- Spring 事务切面配置 --> <aop:config> <aop:pointcut id="serviceOperation" expression="execution(* org.walkframework.base.mvc.service.*.*.*(..)) or execution(* org.walkframework.base.mvc.service.*.*.*.*(..)) or execution(* com.asiainfo.walk.*.mvc.service.*.*.*(..)) or execution(* com.asiainfo.walk.*.mvc.service.*.*.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" /> </aop:config> ``` **注意:** 当数据库是mysql时,如果发现方法开头也对,代码任何地方也没问题,但事务始终就是不生效时,这时就要注意下表的存储引擎类型了,MyISAM是不支持事务的,可考虑改成InnoDB类型