Spring2.5.2的MVC-step-by-step教程没有一点问题,按照它的步骤,能够创建一个完整的Spring的Demo.只是,教程里用的数据库是HSQL,当把HSQL换成我本地的MySQL5的时候,出现了问题。

这个异常是在对产品价格update时出现的:
root cause
java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1552)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1519)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1504)
org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:795)
org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:588)
org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:789)
org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:812)...


在网上查了一下,说是事务配置的问题,找到教程里面关于事务配置的地方:
    <bean id="transactionManager" 
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <aop:config>
        <aop:advisor pointcut="execution(* *..ProductManager.*(..))" advice-ref="txAdvice"/>
    </aop:config>

    <tx:advice id="txAdvice">
        <tx:attributes>
            <tx:method name="save*"/>
            <tx:method name="*" read-only="true"/>
        </tx:attributes>
    </tx:advice>

对这一部分的解释是:The pointcut applies to any method called on the ProductManager interface. The advice is a transaction advice that applies to methods with a name starting with 'save'. The default transaction attributes of REQUIRED applies since no other attribute was specified. The advice also applies "read-only" transactions on any other methods that are adviced via the pointcut.
大概翻译一下:切面是针对实现ProductManager接口的类的。对以'save'开头的方法的建议是事务建议,因为没有指定属性,所以使用默认的'REQUIRED'属性;对其他方法使用'read-only'事务。
看来,这个配置也没什么问题了;我猜,还是MySql的connector的问题(mysql-connector-java-5.0.8-bin.jar),MySql5在事务方面还是不太稳定。最后,把配置文件中这行<tx:method name="*" read-only="true"/>删除,就没问题了。
评论
sherman4223 2008-07-21   回复
仅删除<tx:method name="*" read-only="true"/>是不够的,save方法没有将increase后的数据保存在数据库里。
        /*
        int count = getSimpleJdbcTemplate().update(
            "update products set price = ? where id = ?",
            new MapSqlParameterSource().addValue("price", prod.getPrice())
                .addValue("id", prod.getId()));
        */
        int count = getSimpleJdbcTemplate().update( "update products set price = ? where id = ?", 
        		prod.getPrice(),prod.getId() );

这样改了就好了
metaphy 2008-05-26   回复
引用
汗一个 教授派的吧
啥意思...
jianfeng008cn 2008-05-24   回复
汗一个 教授派的吧
发表评论

您还没有登录,请登录后发表评论

metaphy
搜索本博客
我的相册
0be92b64-8320-3c0c-9365-4b33d8931699-thumb
2008-08-05_213302
共 15 张
最近加入圈子
存档
最新评论