hibernateproxy.java

来自「SSH 的分页...是新手的一个不错例子.」· Java 代码 · 共 45 行

JAVA
45
字号
package com.placard.core.common.hibernate;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.placard.core.common.exception.BusinessException;


public class HibernateProxy{
	private static Log logger = LogFactory.getLog(HibernateProxy.class);
	public Object run(HibernateCallBack callBack)throws BusinessException{
		Session session = null;
		Transaction tx = null;
		try {
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			Object result = callBack.execute();
			tx.commit();
			session.flush();
			return result;
		} catch (HibernateException e) {
			try {
				tx.rollback();
			} catch (HibernateException e1) {
				logger.error(this.getClass().toString()+":事务回滚时出错");
				throw new BusinessException("事务回滚时出错!");
			}
			logger.error(this.getClass().toString()+":执行操作出错!");
			throw new BusinessException(e.getMessage());
		}finally{
			try {
				HibernateUtil.closeSession();
			} catch (HibernateException e) {
				logger.error(this.getClass().toString()+":关闭session时出错!");
				throw new BusinessException("关闭session时出错!");
			}
		}
	}
	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?