看視頻中的代碼有一點(diǎn)疑問(wèn),Transaction tx=null;為空也可以tx.commit();嗎?
//事務(wù)對(duì)象
? ? ? ?Transaction tx=null;
? ? ? ?String hql="";
? ? ? ?try{
? ? ? ? ? ?Session session=MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
? ? ? ? ? ? hql="select * from USER where username=? and password=?";
? ? ? ? ? ? Query query=session.createQuery(hql);
? ? ? ? ? ? query.setParameter(9, u.getUsername());
? ? ? ? ? ? query.setParameter(1, u.getPassword());
? ? ? ? ? ? List list=query.list();
? ? ? ? ? ? tx.commit();
? ? ? ? ? ? if(list.size()>0){
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ?}catch(Exception e){
? ? ? ? ? ?e.printStackTrace();
? ? ? ? ? ?return false;
? ? ? ?}
? ? ? ?finally{
? ? ? ? ? ?if(tx!=null){
? ? ? ? ? ? ? ?tx=null;
? ? ? ? ? ?}
? ? ? ?}
2016-12-10
是不是少了一行代碼: ?//開(kāi)始事物
????????????????????????????????????tx = session.beginTransaction();