我有一个抽象类和两个扩展它的子类。我在spring配置文件中有以下内容
<bean id="importConfigFile" class="xxx.ImportConfigFiles" parent="parentImportFile"></bean>
<bean id="importFile" class="xxx.ImportUMTSKPIFiles" parent="parentImportFile"></bean>
<bean id="parentImportFile" name="parentImportFile" class="xxx.ImportUMTSFiles" abstract="true"></bean>
<tx:annotation-driven transaction-manager="transactionManager" />在我的抽象类中,我有以下方法
public void importDataToDB(){
//all the good stuff goes in here
}
@Transactional
public void executeInsertUpdateQuery(){
//all the good stuff goes in here
}我的java代码
ImportConfigFiles importConfigFiles = (ImportConfigFiles)context.getBean("importConfigFile");
importConfigFiles.setFileLocation(destPath);
importConfigFiles.importDataToDB();这不起作用。executeInsertUpdateQuery()只执行一个原生sql查询。如果我将@ transaction放在imortDataToDB()上,它可以工作,但它会使我的事务变得巨大,因为在该方法中,我循环遍历文件中的所有行,并将记录插入到db中。
发布于 2012-01-26 23:13:11
不太确定问题是什么,但@Transactional将整个方法包装在一个事务中,所以如果你在一个方法中导入所有内容,显然它将是巨大的。这样做的好处是,如果在某些地方导入失败,整个事务将不会执行,数据库中也不会有错误数据。
如果你不想这样做,你将不得不自己管理事务,或者为你的数据子集调用@Transactional注释方法,就像你现在对一个导入所做的那样,但是你可能会对10个文件或其他例如逻辑限制这样做。
https://stackoverflow.com/questions/9020135
复制相似问题