I am trying to understand the use of declarative transaction management, but my demo is not rolling back after a DataAccessException.
From applicationContext.xml
This method should demonstrate a rollback of the changes to recOne when recTwo contains values that produce a DataAccessException.Code:<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/> <property name="url" value="jdbc:sqlserver://localhost;databaseName=emscribedx;"/> <property name="username" value="..."/> <property name="password" value="..."/> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
The log shows that Spring located this method and registered it as TransactionalCode:@Transactional public void updateTwoRecords ( HistoricalDate recOne, HistoricalDate recTwo ){ historicalDatesService.update(recOne); historicalDatesService.update(recTwo); }
2605 [main] DEBUG org.springframework.transaction.annotation.Annotat ionTransactionAttributeSource - Adding transactional method 'updateTwoRecords' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
However, what I see is that the changes from first update will still be committed when the second fails.
From my log. This is the current status of the two records in the DB before testing begins.
============= EventOne Read Back Before method call that should trigger a rollback ===============id=19 date=2008-09-10 event=Original Event One Replaced by this
============= EventTwo Read Back Before method call that should trigger a rollback ===============id=20 date=1846-09-10 event=Original Event Two Replaced by this
These are the values of the two objects in memory before passing them to the updateTwoRecords method.
============= EventOne Before method call that should trigger a rollback ===============id=23 date=2008-09-10 event=Large Hadron Collider powered up
============= EventTwo Before method call that should trigger a rollback ===============id=24 date=1846-09-10 event=First patent issued for sewing machine XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
When the updateTwoRecords() method is called this is logged by the second update.
org.springframework.dao.DataIntegrityViolationExce ption: PreparedStatementCallback; SQL [update historical_dates set date = ? ,event = ? where id=?]; String or binary data would be truncated.; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: String or binary data would be truncated.
values read from the DB after the call to updateTwoRecords. The changes to EventOne have not been rolled back.
============= EventOneReadBack After Error ===============id=13 date=2008-09-10 event=Large Hadron Collider powered up
============= EventTwoReadBack After Error ===============id=14 date=1846-09-10 event=Original Event Two Deleted
I'm guessing that I'm missing some configuration information. I see in ch 10 of the Spring docs that the config file contains a <tx:advice> element and an <aop:config> element. But I don't see this in numerous examples from the web.
What is missing or what am I doing wrong?
Thanks,
bils


Reply With Quote