This is my first attempt at Spring3 @Scheduled, but I found that I could not commit the 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 see that it starts hourly and loads xxx from the database, but the data is not tied to the database.
In spring xml: tx:annotation-driven
<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 someone 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 that makes the code more difficult to read. I wonder why the TransactionManager is not entered (and does not open) in the previous code snippets?
Thank you so much!
spring dependency-injection scheduling spring-3 transactional
smallufo
source share