This is my 1st time trying Spring3's @Scheduled , but found I cannot commit to DB. This is my code :
@Service
public class ServiceImpl implements Service , Serializable
{
@Inject
private Dao dao;
@Override
@Scheduled(cron="0 0 * * * ?")
@Transactional(rollbackFor=Exception.class)
public void hourly()
{
// get xxx from dao , modify it
dao.update(xxx);
}
}
I think it should work , I can see it starts-up hourly and load xxx from DB , but data is not committed to DB.
There's been tx:annotation-driven
in spring's xml :
<bean id="entityManagerFactoryApp" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="myapp"/>
</bean>
<bean id="transactionManagerApp" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryApp" />
</bean>
<tx:annotation-driven transaction-manager="transactionManagerApp" />
Can somebody tell me what I missed here ?
I have one 'dirty' solution :
@Service
public class ServiceImpl implements Service , Serializable
{
@Inject
private Dao dao;
@Inject
@Qualifier("transactionManagerApp")
private PlatformTransactionManager txMgrApp;
@Override
@Scheduled(cron="0 0 * * * ?")
@Transactional(rollbackFor=Exception.class)
public void hourly()
{
final TransactionTemplate txTemplateApp = new TransactionTemplate(txMgrApp);
txTemplateApp.execute(new TransactionCallbackWithoutResult()
{
@Override
protected void doInTransactionWithoutResult(TransactionStatus status)
{
//get xxx from dao
dao.update(xxx);
}
});
}
}
It works fine here , but it is so redundant , making the code harder to read. I wonder why TransactionManager is not injected (and opened) in the previous code snippets?
Thanks a lot !