【发布时间】:2017-07-31 16:36:45
【问题描述】:
当涉及不同的数据库操作并且一个操作依赖于其他操作时,有哪些不同的方式和最佳方式来确保执行顺序。 像这样的代码:
String countryName = "name";
Country countryObj = new Country();
countryObj.setName(countryName);
countryObj.setStates(new ArrayList<State>());
// update database
PersistenceManager manager = new PersistenceManager();
List<Country> countries = manager.getAllCountries();
if (countries != null && !countries.isEmpty()) {
for (Country country2 : countries) {
if (country2.getName().equalsIgnoreCase(countryName)) {
return;
}
}
}
manager.saveCountry(countryObj, country);
这里 manager.getAllCountries() 从数据库中检索所有国家,而 manager.saveCountry 取决于之前的操作
【问题讨论】:
-
你的数据库/ORM 支持事务吗?
-
我的数据库是MongoDB,我认为它不支持事务
标签: java multithreading mongodb database