问题描述
我想在应用程序的开头阅读文本数据夹具(CSV文件),并将其放入数据库中.
I want to read text data fixtures (CSV files) at the start on my application and put it in my database.
为此,我用初始化方法( @PostConstruct 批注)创建了一个 PopulationService .
For that, I have created a PopulationService with an initialization method (@PostConstruct annotation).
我还希望它们在单个事务中执行,因此我在同一方法上添加了 @Transactional .
I also want them to be executed in a single transaction, and hence I added @Transactional on the same method.
但是, @Transactional 似乎被忽略了:在我的底层DAO方法中开始/停止了事务.
However, the @Transactional seems to be ignored :The transaction is started / stopped at my low level DAO methods.
然后我需要手动管理交易吗?
Do I need to manage the transaction manually then ?
推荐答案
这可能会有所帮助():
this might be helpful (http://forum.springsource.org/showthread.php?58337-No-transaction-in-transactional-service-called-from-PostConstruct):
因此,如果您希望在事务中执行@PostConstruct
中的某些内容,则必须执行以下操作:
So if you would like something in your @PostConstruct
to be executed within transaction you have to do something like this:
@Service("something")
public class Something {
@Autowired
@Qualifier("transactionManager")
protected PlatformTransactionManager txManager;
@PostConstruct
private void init(){
TransactionTemplate tmpl = new TransactionTemplate(txManager);
tmpl.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
//PUT YOUR CALL TO SERVICE HERE
}
});
}
}
这篇关于@PostConstruct方法上的@Transactional的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!