从Spring MVC-step-by-step发现的问题(Spring 2.5.2)

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

这个异常是在对产品价格update时出现的:[code]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)...[/code]

在网上查了一下,说是事务配置的问题,找到教程里面关于事务配置的地方:[code] <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>[/code]
对这一部分的解释是: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"/>删除,就没问题了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值