JDK动态代理机制(二)
essionFactory().getCurrentSession();
session.getTransaction().begin();//开始前开启事务
Object result;
try {
result = method.invoke(instance, args);//执行业务逻辑
session.getTransaction().commit();//业务逻辑执行完成后提交事务
return result;
} catch (Exception e) {
session.getTransaction().rollback(); //如果业务逻辑出现例外则事务回滚
throw new RuntimeException(e);
} finally {
if(null!=session && session.isOpen()) {
session.close();//这里是不会执行的,因为Session会在事务提交或回滚时自动提交,如果调用的是openSession获取Session这里就会执行
}
}
}
});
}
public void save(Admin entity) {
String pass = MD5.MD5Encode(entity.getPass());
entity.setPass(pass);
super.save(entity);
}
}