spring + webwork + hibernate 的项目开发习惯了,突然接触到只用webwork + hibernate 的项目,没有了spring对整个框架的管理,很是不习惯,于是就从借鉴了一下Webwork与Hibernate结合开发的通用模块了,感觉整个结构也比较清晰,就随手整理了过来。
通用类:
HibernateSessionFactory.java
HibernateSessionFactoryImp.java
HibernateSession.java
HibernateSessionImp.java
AbstractAction.java
拦截器:
HibernateInterceptor.java
一.通用类
1.HibernateSessionFactory.java中主要封装了Hibernate的SessionFactory实例.
private static final Log LOG = LogFactory.getLog(SessionFactoryService.class);
private SessionFactory sf;
//返回SessionFactory实例
public HibernateSessionFactory getSessionFactory() ...{
return sf;
}
//初始化生成SessionFactory
public void init() ...{
try ...{
sf = new Configuration().configure().buildSessionFactory();
}
catch (Exception e) ...{
LOG.error("error generate SessionFactory", e);
throw new RuntimeException( e.getMessage() );
}
}
//关闭SessionFactory
public void dispose() ...{
try ...{
sf.close();
}
catch (Exception e) ...{
LOG.error("error closing HibernateSession", e);
}
}
public String getDialect() ...{
return Environment.getProperties().getProperty(Environment.DIALECT);
}
}
2.HibernateSessionFactoryImp.java是一个Interface,供HibernateSession实现,从而让webwork拦截器自动调用其中的方法.
public void setSessionFactoryService(SessionFactoryService sfs);
}
3. HibernateSession.java封装了一个Session实例,并且实现了HibernateSessionFactoryImp接口从而在加入拦截器申明以后(后文会介绍)会通过webwork的拦截器自动调用其中的setHibernateSessionFactory()方法,从而自动将HibernateSessionFactory实例注入到了HibernateSession中,供HibernateSession调用
private static final Log LOG = LogFactory.getLog(SessionService.class);
private Session session;
private Transaction transaction;
private HibernateSessionFactory hsf;
private boolean rollBackOnly;
//返回一个Session实例,并开始一个事务
public Session getSession() throws HibernateException ...{
if (session==null) ...{
session = hsf.getSessionFactory().openSession();
transaction = session.beginTransaction();
}
return session;
}
//注入HibernateSessionFactory实例
public void setHibernateSessionFactory(HibernateSessionFactory hsf) ...{
this.hsf = hsf;
}
//关闭一个Session
public void disposeSession() throws HibernateException ...{
LOG.debug("disposing");
if (session==null) return;
if (rollBackOnly) ...{
try ...{
LOG.debug("rolling back");
if (transaction!=null) transaction.rollback();
}
catch (HibernateException e) ...{
LOG.error("error during rollback", e);
throw e;
}
finally ...{
session.close();
session=null;
transaction=null;
}
}
else ...{
try ...{
LOG.debug("committing");
if (transaction!=null) transaction.commit();
}
catch (HibernateException e) ...{
LOG.error("error during commit", e);
transaction.rollback();
throw e;
}
finally ...{
session.close();
session=null;
transaction=null;
}
}
}
//事务回滚
public boolean isRollBackOnly() ...{
return rollBackOnly;
}
public void setRollBackOnly(boolean rollBackOnly) ...{
this.rollBackOnly = rollBackOnly;
}
}
4.同样HibernateSessionImp.java也是一个接口,供AbstractAction实现,从而使Webwork自动调用其中的
public void setHibernateSession(HibernateSession hs);
}
5.让我们来看看AbstractAction.java
private static final Log LOG = LogFactory.getLog(AbstractAction.class);
private HibernateSession hs;
//重写Webwork的execute()方法
public String execute() throws Exception ...{
// 如果有错返回到INPUT所指定的页面
if ( hasErrors() ) ...{
LOG.debug("action not executed, field or action errors");
LOG.debug( "Field errors: " + getFieldErrors() );
LOG.debug( "Action errors: " + getActionErrors() );
return INPUT;
}
LOG.debug("executing action");
return go();
}
protected abstract String go() throws HibernateException;
//注入HibernateSession实例
public void setSessionService(SessionService ss) ...{
this.ss = ss;
}
public SessionService getSessionService() ...{
return ss;
}
//返回Session实例
protected Session getSession() throws HibernateException ...{
return ss.getSession();
}
protected void setRollbackOnly() ...{
ss.setRollBackOnly(true);
}
/** *//**
* 将需要保存的对象暂存在Webwork的Session中(此Session非彼Session)
*/
protected Object get(String name) ...{
return ActionContext.getContext().getSession().get(name);
}
/** *//**
* 从Webwork的Session(此Session非彼Session)中取出暂存的对象
*/
protected void set(String name, Object value) ...{
ActionContext.getContext().getSession().put(name, value);
}
}
二.拦截器
HibernateInterceptor.java
private static final Log LOG = LogFactory.getLog(HibernateInterceptor.class);
public void destroy() ...{}
public void init() ...{}
public String intercept(ActionInvocation invocation) throws Exception ...{
Action action = invocation.getAction();
if ( !(action instanceof AbstractAction) ) return invocation.invoke();
HibernateSession hs = ( (AbstractAction) action ).getHibernateSession();
try ...{
return invocation.invoke();
}
catch (Exception e) ...{
ss.setRollBackOnly(true);
if (e instanceof HibernateException) ...{
LOG.error("HibernateException in execute()", e);
return Action.ERROR;
}
else ...{
LOG.error("Exception in execute()", e);
throw e;
}
}
finally ...{
try ...{
hs.disposeSession();
}
catch (HibernateException e) ...{
LOG.error("HibernateException in dispose()", e);
return Action.ERROR;
}
}
}
}
拦截器监视Action的一举一动,并随时截获异常信息,返回给页面.若无异常,则调用HibernateSession的disposeSession()方法,关闭一个Session.
原文:http://www.nihaoblog.com/933_12896.html