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"/>删除,就没问题了。
这个异常是在对产品价格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"/>删除,就没问题了。