<分区>
引用 T1 和 T2,哪一个是创建 hibernate 事务的正确做法?我对选择其中之一有疑问。 我的问题是在什么情况下我应该像 T1 或 T2 这样编码。
T1
try{
currentSession.beginTransaction();
// assume assume this method is a hql select query
Person person=personService.findPerson(personId);
// complex rules that will affect performance
Health health=healthService.calculateComplexBizRules(personId);
person.setHealth(health);
currentSession.commitTransaction();
}catch (Exception e) {
rollBackTransaction();
e.printStackTrace();
throw e;
}
T2
try{
currentSession.beginTransaction();
// assume this method is a hql select query
Person person=personService.findPerson(personId);
currentSession.commitTransaction();
// complex rules that will affect performance
Health health=healthService.calculateComplexBizRules(personId);
currentSession.beginTransaction();
currentSession.persist(health);
Person person=currentSession.merge(person);
person.setHealth(health);
currentSession.commitTransaction();
}catch (Exception e) {
rollBackTransaction();
e.printStackTrace();
throw e;
}