Question

In my application, I have 2 transactionManager, created like this:

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <qualifier value="exec"/>
    <property name="dataSource" ref="execDataSource"/>
</bean>
<bean id="txManagerAdmin" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <qualifier value="admin"/>
    <property name="dataSource" ref="adminDataSource"/>
</bean>

In the same file, I have the annotation-driven declaration:

<tx:annotation-driven transaction-manager="admin"/>

To make simple the usability of my admin transactionManager, I created a simple Annotation:

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Transactional(value="admin", rollbackFor=NullPointerException.class, propagation = Propagation.REQUIRES_NEW, isolation = Isolation.DEFAULT)
public @interface AdminTx {

}

And here my method with the transaction annotation:

@AdminTx
    @Override
    public UaCatalogDTO addUa(UaDTO uaDTO) throws TechnicalException {
        MapSqlParameterSource namedParameterSource = new MapSqlParameterSource();
        mapAllUaFields(uaDTO, namedParameterSource);
        try {
            jdbcTemplate.update(SqlQueries.ADD_UA, namedParameterSource);
        } catch (DuplicateKeyException e) {
            throw new TechnicalException(e, "ADM001");
        }
                if (1==1) //due to compiler
                throw new NullPointerException(); //to test the transaction is working
    }

This method is called from another class thanks to an interface. The bean was injected by Spring @Autowired annotation. The jdbcTemplateobject was created with this code:

@Autowired
@Qualifier("adminDataSource")
public void setDataSource(DataSource dataSource) {
    this.jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}

My problem is when I execute the jdbcTemplate.update() line, if I check my database, the data is already present. Plus, even if I throw the NullPointerException, the data stays in database.

After some search, I found that we can call TransactionSynchronizationManager.isActualTransactionActive(), and it returns me false value. So I understand that my annotation does nothing, but I can't understand why.

I'm running on a Websphere server, and database is DB2.

Was it helpful?

Solution

After some searching, I finally found the problem, so I post it if someone is interested: It was the bean generated by Spring using no XML specific declaration but Spring scanning.

When I tried to declare my bean in XML file and then injected it in the class with Autowired and Qualifier, the Transaction finally open and close.

I don't know the reason by the way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top