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