hiberante入门(十五):事宜相关-悲观乐观锁
hiberante入门(十五):事务相关-悲观乐观锁package com.asm.hibernate.utilspublic class HibernateUtilOS
hiberante入门(十五):事务相关-悲观乐观锁
package com.asm.hibernate.utils;public class HibernateUtilOSV {private static SessionFactory sf;private static ThreadLocal session = new ThreadLocal();private HibernateUtilOSV() {}public static Session getThreadLocalSession() {Session s = (Session) session.get();if (s == null) {s = getSession();session.set(s);}return s;}public static void closeSession() {Session s = (Session) session.get();if (s != null) {s.close();session.set(null);}}static {Configuration cf = new Configuration();cf.configure();sf = cf.buildSessionFactory();}public static SessionFactory getSessionFactory() {return sf;}public static Session getSession() {return sf.openSession();}}?
package com.asm.hibernate.osv;public class OpenSessionViewFilter implements Filter {public void destroy() {// TODO Auto-generated method stub}public void doFilter(ServletRequest arg0, ServletResponse arg1,FilterChain arg2) throws IOException, ServletException {Session s = null;Transaction tx = null;try {s = HibernateUtilOSV.getThreadLocalSession();tx = s.beginTransaction();arg2.doFilter(arg0, arg1);tx.commit();} catch (Exception e) {if (tx != null)tx.rollback();} finally {HibernateUtilOSV.closeSession();}}public void init(FilterConfig arg0) throws ServletException {// TODO Auto-generated method stub}}package com.asm.hibernate.osv;public class UserDao {static void addUseDao(User user) {HibernateUtilOSV.getThreadLocalSession().save(user);}}package com.asm.hibernate.utils;public class HibernateUtilSelf {private static SessionFactory sf;private HibernateUtilSelf() {}public static Session getThreadLocalSession() {Session s = sf.getCurrentSession();if (s == null) {s = getSession();}return s;}static {Configuration cf = new Configuration();cf.configure();sf = cf.buildSessionFactory();}public static SessionFactory getSessionFactory() {return sf;}public static Session getSession() {return sf.openSession();}}package com.asm.hibernate.osv;public class UserTest {public static void main(String[] args) {User user = new User();user.setName("richie");user.setDate(new Date());addUser(user);}static void addUser(User user) {Session s = null;Transaction ts = null;try {s = HibernateUtilSelf.getThreadLocalSession();ts = s.beginTransaction();s.save(user);ts.commit();} catch (HibernateException e) {if (ts != null)ts.rollback();throw e;} finally {if (s != null)// s.close(); //注意这里不能使用close,并注意下面的打印结果System.out.println("s=" + s);}}}package com.asm.hibernate.test;public class VersionUserTest { public static void main(String[] args) { addUser(); update(1); } static void update(int id) { Session s1 = null; Session s2 = null; Transaction tx1 = null; Transaction tx2 = null; try { s1 = HibernateUtil.getSession(); tx1 = s1.beginTransaction(); User user1 = (User) s1.get(User.class, id); System.out.println(user1.getName()); s2 = HibernateUtil.getSession(); tx2 = s2.beginTransaction(); User user2 = (User) s2.get(User.class, id); user1.setName("user1Name"); user2.setName("user2Name"); tx1.commit(); tx2.commit(); } catch (HibernateException e) { .... } finally { .... } } static void addUser() { ...主要作用是增加一条记录以实现操作。 }}基本和方式一相同,只是在User类中增加一个Date类型的属性,同时在实体配置文件配置如下内容: <timestamp name="实体类的中Date类型的属性"></timestamp>?