Spring Transaction Manager: Rollback doesnt work -
i wish execute few insert queries within transaction block if there error inserts rolled back.
i using mysql
database , spring transactionmanager this. table type innodb
i have done configuration following steps mentioned here.
following code (for 1 query)
transactiondefinition def = new defaulttransactiondefinition(); transactionstatus status = null; status = transactionmanager.gettransaction(def); jdbctemplate.execute(sqlinsertquery); transactionmanager.rollback(status);
spring config xml:
<bean id="jdbctemplate" class="org.springframework.jdbc.core.jdbctemplate"> <property name="datasource"> <ref bean="datasource" /> </property> </bean> <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> <property name="datasource" ref="datasource" /> </bean>
datasource config:
<bean id="datasource" class="org.apache.commons.dbcp.basicdatasource"> <property name="driverclassname" value="${jdbc.driverclassname}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="initialsize" value="${jdbc.initialsize}" /> <property name="maxactive" value="${jdbc.maxactive}" /> <property name="minidle" value="${jdbc.minidle}" /> <property name="maxidle" value="${jdbc.maxidle}" /> <property name="testonborrow" value="${jdbc.testonborrow}" /> <property name="testwhileidle" value="${jdbc.testwhileidle}" /> <property name="testonreturn" value="${jdbc.testonreturn}" /> <property name="validationquery" value="${jdbc.validationquery}" /> <property name="timebetweenevictionrunsmillis" value="${jdbc.timebetweenevictionrunsmillis}" /> <!--<property name="removeabandoned" value="true"/> <property name="removeabandonedtimeout" value="10"/> <property name="logabandoned" value="false"/> --> <property name="numtestsperevictionrun" value="${jdbc.numtestsperevictionrun}" /> </bean>
this code works fine , record gets inserted. rollback doesnt work! executes rollback statement without error no effect.
can guide me going wrong?
it appears issues datasource not set have autocommit off.
<property name="defaultautocommit" value="false"/>
give try. have never used transactionmanager outside of proxy not sure if there other gotchas using directly this, recommend @ either aop transactions or convience aop proxy annotation @transactional because more common.