Kivanc
Kivanc

Reputation: 256

spring data source transaction manager not rolling back

<!-- Data source definition -->
<bean id="dataSourceWebsube"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url.websube}"
    p:username="${jdbc.username.websube}"   p:password="${jdbc.password.websube}" />

<bean id="jdbcTemplateWebsube" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSourceWebsube" />
</bean>         

<!-- Transaction manager, actually this one is useless since the Transaction Manager Bean is already called transactionManager -->      
<tx:annotation-driven transaction-manager="transactionManager"/>  

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSourceWebsube" />
</bean>

public class Test 
{
@Transactional
public static void testTranscational(JdbcTemplate jdbcTmpl)
{
    String sql = null;                              
    sql = "INSERT INTO NBSM.INT_RTLNOUTBOUND_PRODUCTS(BASEPRODUCTCODE)VALUES(1)";                
    jdbcTmpl.update(sql);           
    throw new RuntimeException();                       
}

public static void main(String[] args) {            
    ApplicationContext ctx = new ClassPathXmlApplicationContext("application-context.xml");
    JdbcTemplate jdbcTmpl = (JdbcTemplate) ctx.getBean("jdbcTemplateWebsube");      
    testTranscational(jdbcTmpl);
}
}

Hello,

I'm trying to test spring's transaction management capabilities but I cannot get it to work. I have been trying to solve it for hours but no luck.

Above you can see the related config xml definitions and the dao test class. Even when I use debug level for logging, there's no footprint of the rolling back mechanism.

Any help would be greatly appreciated

Upvotes: 1

Views: 3251

Answers (1)

Ralph
Ralph

Reputation: 120831

It is because of Spring AOP can only:

  • a) work on Spring managed Bean
  • b) work on none static methods
  • c) is only taken in account if the Spring AOP Proxy is invoked (you need to invoke the bean from an other bean, but not from itself)

a) and b) are your faults, you need to fix it. -- to overcome the problem of c) I strongly replace Spring AOP by AspectJ compile time waving.

If I am right, even the transaction handling (by @Transational must not be working.

Need to be checked by someone else:

I have never seen @Transactional in combination with JdbcTemplate, so I have doubt if this is working at all. -- But may I am wrong.

Upvotes: 2

Related Questions