I now tried everything and got a little bit further, but still the rollback does not happen. The log4j Trace tells me spring is doing something:
Code:
18:22:57 DEBUG [main] NameMatchTransactionAttributeSource:94 Adding transactional method [_mergeContacts] with attribute [PROPAGATION_REQUIRED,ISOLATION_DEFAULT,-MergeException]
18:22:57 DEBUG [main] DefaultListableBeanFactory:244 Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0'
18:22:57 DEBUG [main] DefaultListableBeanFactory:244 Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0'
18:22:57 DEBUG [main] DefaultListableBeanFactory:458 Finished creating instance of bean '(inner bean)'
18:22:57 DEBUG [main] DefaultListableBeanFactory:1461 Invoking afterPropertiesSet() on bean with name 'txAdvice'
18:22:57 DEBUG [main] DefaultListableBeanFactory:458 Finished creating instance of bean 'txAdvice'
18:22:57 DEBUG [main] DefaultListableBeanFactory:244 Returning cached instance of singleton bean 'org.springframework.aop.config.internalAutoProxyCreator'
18:22:57 DEBUG [main] DefaultListableBeanFactory:244 Returning cached instance of singleton bean 'org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0'
18:22:57 DEBUG [main] DefaultListableBeanFactory:244 Returning cached instance of singleton bean 'dataSource_mysql'
18:22:57 DEBUG [main] DefaultListableBeanFactory:214 Creating shared instance of singleton bean 'jdbcTemplate_mysql'
18:22:57 DEBUG [main] DefaultListableBeanFactory:430 Creating instance of bean 'jdbcTemplate_mysql'
but nothing about a rollback and in the db all has been committed.
My last context.xml goes as follows:
Code:
<context:property-placeholder location="file:server.properties" />
<context:component-scan base-package="server" />
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="_mergeContacts" read-only="false" rollback-for="MergeException"/>
</tx:attributes>
</tx:advice>
<aop:aspectj-autoproxy/>
<aop:config>
<aop:pointcut id="serverObjectOperation" expression="execution(* server.ServerObject._merge*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serverObjectOperation"/>
</aop:config>
<bean id="myService" class="server.MyService"/>
<bean id="mergeDao" class="server.dao.MergeDao">
<property name="jdbcTemplate" ref="jdbcTemplate_mysql" />
</bean>
<bean id="dataSource_mysql" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${jdbc_mysql.url}" />
<property name="username" value="${jdbc_mysql.username}" />
<property name="password" value="${jdbc_mysql.password}" />
<property name="initialSize" value="5" />
<property name="maxActive" value="50" />
</bean>
<bean id="jdbcTemplate_mysql" class="org.springframework.jdbc.core.JdbcTemplate" autowire="byName">
<property name="dataSource" ref="dataSource_mysql" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource_mysql"/>
</bean>
and my classes are:
Code:
/-----------------------------------------------------------------------------------------------------------------
package server;
public class MyService{
XMLAppContext appContext = new XMLAppContext("context.xml");
@Transactional(readOnly = false, rollbackFor=MergeException.class)
public String _mergeContacts(String id1, String id2) throws MergeException {
MergeDao m = (MergeDao) appContext.getBean("mergeDao");
return m._mergeContacts(id1,id2);
}
}
/-----------------------------------------------------------------------------------------------------------------
package server.dao;
public class MergeDao{
@Autowired
JdbcTemplate jdbcTemplate_mysql;
public String _mergeContacts(String id1, String id2) throws MergeException{
String newID = generateNewID();
jdbcTemplate_mysql.update("INSERT INTO XXX", newID,id1,id2);
replaceReferences(id1,newID);
replaceReferences(id2,newID);
if(true)
throw new MergeException();
}
private void replaceReferences(String id1, String id2) throws MergeException{
jdbcTemplate_mysql.update("Update table_XXX", id2,id1,);
}
}
I know that it is made double: tx:advice with this method and @Transactional on the method itself
As i (think i) know i can either use tx:annotation-driven with @Transactional above the method or tx:advice with pointcut and aop:aspectj-autoproxy.
As a third option, maybe, i found context:component-scan with scans the package for annotations.
Lost in code and options, trying hundred of different mixes, i finally have to wave the flag and cry for help ;(