所以我有一个问题,我对春天有点陌生。因此,基本上我希望在“ retrieveAndSaveInformationFromBac()”结尾之前什么都不要提交。目前,如果引发异常,则会提交一些更改。这是代码:
public void retrieveAndSaveInformationFromBac() throws ClientWSBacException {
logger.info("Start BATCH BAC");
BatchContextManager.useBatchSchemaForCurrentThread();
Map<ClasseId, Classe> mapClasses = new HashMap<>();
try {
logger.info("Deleting existant table...");
deleteAllTables();
ClientWSBac client = new ClientWSBac(productService);
List<ModeleBac> listModeleBacToSave = new ArrayList<>();
for(VehiculeType type : VehiculeType.values()){
logger.info("Saving the type : "+ type);
Map<BacFileKey, List<ModeleBac>> mapModeleBac = client.retrieveAndSaveAllFromBac(type, mapClasses);
listModeleBacToSave = new ArrayList<>();
for(List<ModeleBac> listModeleBac : mapModeleBac.values()) {
for(ModeleBac modele : listModeleBac) {
if(modele.getNivsBac().size() > 0){
checkEnDescriprions(modele);
listModeleBacToSave.add(modele);
}
}
if(listModeleBacToSave.size()> 500){
saveListModeleBac(listModeleBacToSave);
}
}
saveListModeleBac(listModeleBacToSave);
}
saveClasseAndTypeCarrosserie(mapClasses);
logger.info("END OF THE BATCH");
switchDataSourcesAfterBatch();
} catch (Exception e) {
logger.error("Error during importation", e);
throw new ClientWSBacException("Error during importation.Stopping the process....");
}finally{
BatchContextManager.clear();
}}
这是我“保存”我的信息的地方:
@Transactional
private void saveListModeleBac(List<ModeleBac> listModeleBac){
long startTime = System.nanoTime();
try{
modeleBacDAO.save(listModeleBac);
} catch (Exception e){
e.printStackTrace();
logger.error("Error during the save of the informations..");
throw e;
}
long endTime = System.nanoTime();
long duration = ((endTime - startTime)/1000000000);
logger.debug("Number of insert : " + String.valueOf(listModeleBac.size())+ " in " + String.valueOf(duration) + " seconds" );}
这是我删除表格的地方:
@Transactional
private void deleteAllTables(){
if(databaseMode.equals(DatabaseMode.H2)){
modeleBacDAO.deleteAllModeleBac();
classeDAO.deleteAllInBatch();
typeCarrosserieDAO.deleteAllInBatch();
}else{
dataSourceUtils.callStoredProcVideTable(currentDBSchemaResolver.resolveCurrentTenantIdentifier());
}}
答案 0 :(得分:2)
@Transactional
can't work on private method,因为它是使用方面(使用动态代理)
基本上,您希望retrieveAndSaveInformationFromBac()
是一个工作单元,即交易。
因此用@Transactional
对其进行注释。
答案 1 :(得分:0)
由于您使用的是Hibernate
,因此可以通过属性来处理:
<property name="hibernate.connection.autocommit">false</property>
然后您可以像
一样进行事务处理Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
//do stuff and then
tx.commit();