关于hibernate session

来源:互联网 发布:mac图片放大快捷键 编辑:程序博客网 时间:2024/06/06 07:49

一般每个请求都有各自独立的session,可以查看ID,都是不一样的

每个session都会起一个缓存,当某个action请求结束后,就可以清楚叼改session的缓存

所有每一个接口的执行过程及缓存为

请求->hibernate从数据库读取相应的数据后会缓存下了, ->  处理过程  -> 处理完成后session.clear()退出该接口请求并清楚缓存



对于事物

事物即为 Transaction ,使用事物时都会把整个数据库给锁住,只有commit()后才会解锁,使用完后得吧session close()掉

Session session = HibernateSessionFactory.getSession();Transaction tran = session.beginTransaction();tran.begin();MaintainAutostartBo bo = new MaintainAutostartBo();bo.setSession(session);MTPlanAutostart currentAutostartPlan =  (MTPlanAutostart) bo.getById(MTPlanAutostart.class, autostartPlan.getId());MTPlan plan = (MTPlan) bo.getById(MTPlan.class, autostartPlan.getPlan().getId());Timestamp nowTime = new Timestamp(System.currentTimeMillis());//当计划为启用状态,定时开始时间 < 系统当前时间 < 定时结束时间,才分配任务if(nowTime.compareTo(currentAutostartPlan.getBeginDate()) == 1 && plan.getState() == 1 && plan.getType()== MTPlan.IS_AUTOSTART &&(currentAutostartPlan.getEndDate() == null || nowTime.compareTo(currentAutostartPlan.getEndDate()) == -1)){log.info("==========<检修维护>定时任务分配开始");bo.AddTaskByAutostart(currentAutostartPlan);}//检测到该任务已过期,则取消定时任务if(plan.getType()== MTPlan.IS_NOI_AUTOSTART ||(currentAutostartPlan.getEndDate() != null && nowTime.compareTo(currentAutostartPlan.getEndDate()) == 1)){MtJobController.getInstance().resetJob(plan.getId());}tran.commit();



0 0