通过 Spring 事务管理标记为只读的所有服务方法更新数据库



我正在学习春季事务管理。
我使用过Spring3.0和oracle 10g进行练习。
以下是我的弹簧交易配置

 <aop:config>
      <aop:pointcut expression="execution(* com.spring.*.service.impl.*.*(..))" 
                    id="serivcePointcut"/>
      <aop:advisor advice-ref="txAdvice" pointcut-ref="serivcePointcut"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
     <tx:attributes>
         <tx:method name="get*" read-only="true"/>
         <tx:method name="find*" read-only="true"/>
         <tx:method name="*"/>
    </tx:attributes>
</tx:advice.

我只是想检查通过 get* 方法插入任何记录是否会引发异常,因为只读已被标记为 true。所以我通过getCustomer()方法插入了一条记录。但是猜猜看,插入的记录不是抛出异常。

在日志中,我能够看到正在为 com.spring.customer.service.impl.CustomerServiceImpl 创建的事务,甚至日志显示 Set jdbc 连接只读。

谁能解释我哪里出错了?

TransactionDefinition 的 javadoc 说:

这只是作为实际事务子系统的提示;它 不一定会导致写入访问尝试失败。一个 无法解释只读提示的事务管理器不会 在要求输入只读事务时引发异常。

Connection的javadoc说:

将此连接置于只读模式,作为对驱动程序的提示 启用数据库优化。

简而言之,如果您的代码确实是只读的,则使用 readOnly 可能会提高性能。但这不会阻止您在此类事务中执行写入操作。

最新更新