一、前言
关于informa源码包informa-0.6.0.jar中的de.nava.informa.utils.manager.hibernate.HibernateUtil工具类,对hibernate会话Session打开openSession/关闭closeSession操作、数据持久化对象类CURD操作(saveObject/updateObject/deleteObject),详情参见源码说明部分。
二、源码说明
1. HibernateUtil工具类
package de.nava.informa.utils.manager.hibernate;@b@@b@import de.nava.informa.impl.hibernate.SessionHandler;@b@import de.nava.informa.utils.manager.PersistenceManagerException;@b@import java.sql.Connection;@b@import java.util.logging.Logger;@b@import net.sf.hibernate.HibernateException;@b@import net.sf.hibernate.LockMode;@b@import net.sf.hibernate.Session;@b@@b@final class HibernateUtil@b@{@b@ private static final Logger LOG = Logger.getLogger(HibernateUtil.class.getName());@b@ private static SessionHandler sessionHandler;@b@ private static final ThreadLocal SESSION = new ThreadLocal();@b@ private static boolean inited = false;@b@ private static Object lock = new Object();@b@ private static boolean sessionOpened = false;@b@@b@ private static void init()@b@ throws HibernateException@b@ {@b@ sessionHandler = SessionHandler.getInstance(System.getProperties());@b@ inited = true;@b@ }@b@@b@ public static synchronized Session openSession()@b@ throws HibernateException@b@ {@b@ if (!(inited))@b@ init();@b@ synchronized (lock)@b@ {@b@ if (sessionOpened)@b@ try@b@ {@b@ lock.wait();@b@ }@b@ catch (InterruptedException localInterruptedException)@b@ {@b@ }@b@ }@b@ ??? = (Session)SESSION.get();@b@ if (??? == null)@b@ {@b@ ??? = sessionHandler.getSession();@b@ SESSION.set(???);@b@ }@b@ sessionOpened = true;@b@ return ((Session)???);@b@ }@b@@b@ public static void closeSession()@b@ {@b@ Session localSession = (Session)SESSION.get();@b@ SESSION.set(null);@b@ if (localSession != null)@b@ try@b@ {@b@ localSession.close();@b@ }@b@ catch (HibernateException localHibernateException)@b@ {@b@ }@b@ synchronized (lock)@b@ {@b@ sessionOpened = false;@b@ lock.notify();@b@ }@b@ }@b@@b@ public static void lock(Object paramObject, Session paramSession)@b@ {@b@ try@b@ {@b@ paramSession.lock(paramObject, LockMode.NONE);@b@ }@b@ catch (HibernateException localHibernateException)@b@ {@b@ }@b@ }@b@@b@ public static void saveObject(Object paramObject)@b@ throws PersistenceManagerException@b@ {@b@ saveObject(paramObject, null);@b@ }@b@@b@ public static void saveObject(Object paramObject, Session paramSession)@b@ throws PersistenceManagerException@b@ {@b@ int i = (paramSession != null) ? 1 : 0;@b@ try@b@ {@b@ if (i == 0)@b@ paramSession = openSession();@b@ paramSession.save(paramObject);@b@ if (i == 0)@b@ {@b@ paramSession.flush();@b@ paramSession.connection().commit();@b@ }@b@ }@b@ catch (Exception localException1)@b@ {@b@ if (i == 0);@b@ throw new PersistenceManagerException("Couldn't save object.", localException1);@b@ }@b@ finally@b@ {@b@ if (i == 0)@b@ closeSession();@b@ }@b@ }@b@@b@ public static void updateObject(Object paramObject)@b@ throws PersistenceManagerException@b@ {@b@ updateObject(paramObject, null);@b@ }@b@@b@ public static void updateObject(Object paramObject, Session paramSession)@b@ throws PersistenceManagerException@b@ {@b@ int i = (paramSession != null) ? 1 : 0;@b@ try@b@ {@b@ if (i == 0)@b@ paramSession = openSession();@b@ paramSession.update(paramObject);@b@ if (i == 0)@b@ {@b@ paramSession.flush();@b@ paramSession.connection().commit();@b@ }@b@ }@b@ catch (Exception localException1)@b@ {@b@ if (i == 0);@b@ throw new PersistenceManagerException("Couldn't update object.", localException1);@b@ }@b@ finally@b@ {@b@ if (i == 0)@b@ closeSession();@b@ }@b@ }@b@@b@ public static void deleteObject(Object paramObject, Session paramSession)@b@ throws PersistenceManagerException@b@ {@b@ int i = (paramSession != null) ? 1 : 0;@b@ try@b@ {@b@ if (i == 0)@b@ paramSession = openSession();@b@ paramSession.delete(paramObject);@b@ if (i == 0)@b@ {@b@ paramSession.flush();@b@ paramSession.connection().commit();@b@ }@b@ }@b@ catch (Exception localException1)@b@ {@b@ if (i == 0);@b@ throw new PersistenceManagerException("Couldn't delete object.", localException1);@b@ }@b@ finally@b@ {@b@ if (i == 0)@b@ closeSession();@b@ }@b@ }@b@}
2.SessionHandler类
package de.nava.informa.impl.hibernate;@b@@b@import java.sql.Connection;@b@import java.util.Properties;@b@import net.sf.hibernate.HibernateException;@b@import net.sf.hibernate.Session;@b@import net.sf.hibernate.SessionFactory;@b@import net.sf.hibernate.cfg.Configuration;@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@@b@public class SessionHandler@b@{@b@ private static Log logger = LogFactory.getLog(SessionHandler.class);@b@ private static SessionHandler myInstance;@b@ private Configuration cfg = new Configuration();@b@ private SessionFactory sessFactory;@b@ private Connection conn;@b@ private Session curSession;@b@@b@ private SessionHandler(Properties paramProperties)@b@ throws HibernateException@b@ {@b@ try@b@ {@b@ this.cfg.configure();@b@ }@b@ catch (HibernateException localHibernateException)@b@ {@b@ logger.info("Can't find \"hibernate.cfg.xml\" in classpath.");@b@ }@b@ this.cfg.addClass(Channel.class).addClass(Item.class).addClass(ItemGuid.class).addClass(ItemEnclosure.class).addClass(ItemSource.class).addClass(Cloud.class).addClass(Category.class).addClass(ChannelGroup.class).addClass(ChannelSubscription.class).addClass(Image.class).addClass(ItemMetadata.class).addClass(TextInput.class);@b@ if (paramProperties != null)@b@ this.cfg.addProperties(paramProperties);@b@ this.sessFactory = this.cfg.buildSessionFactory();@b@ }@b@@b@ public static SessionHandler getInstance(Properties paramProperties)@b@ throws HibernateException@b@ {@b@ if (myInstance == null)@b@ myInstance = new SessionHandler(paramProperties);@b@ return myInstance;@b@ }@b@@b@ public static SessionHandler getInstance()@b@ throws HibernateException@b@ {@b@ return getInstance(null);@b@ }@b@@b@ public Session getSession()@b@ throws HibernateException@b@ {@b@ if (this.conn != null)@b@ this.curSession = this.sessFactory.openSession(this.conn);@b@ else@b@ this.curSession = this.sessFactory.openSession();@b@ return this.curSession;@b@ }@b@@b@ public Session getSession(Connection paramConnection)@b@ {@b@ this.conn = paramConnection;@b@ this.curSession = this.sessFactory.openSession(paramConnection);@b@ return this.curSession;@b@ }@b@@b@ public Connection getConnection()@b@ {@b@ return this.conn;@b@ }@b@@b@ public void setConnection(Connection paramConnection)@b@ {@b@ this.conn = paramConnection;@b@ }@b@@b@ public boolean isSessionOpen()@b@ {@b@ return this.curSession.isOpen();@b@ }@b@}