📄 daoexecutor.java
字号:
package test.hibernate;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;
import test.hibernate.HibernateSessionFactory;
import vteam.frame.system.exception.*;
import vteam.frame.system.log.VteamLogFactory;
import vteam.frame.system.log.VteamLogger;
public class DAOExecutor {
private static final VteamLogger log = VteamLogFactory.getFrameLog();
private DAOExecutor() {
}
public static void executorDAO(ArrayList list) throws AppException, RollbackException,HibernateException{
if (list == null || list.size() <= 0){
return;
}
Object bean = null;
Class cls = null;
Method method = null;
String action = null;
Object dao = null;
Class daoclass = null;
// 实例化一个默认的事务定义对象
// DefaultTransactionDefinition def = new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
// 设置当前的事务隔离等级
// def.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
// 开启事务
/// TransactionStatus status = this.transactionManager.getTransaction(def);
Transaction tx = null ;
// BaseHibernateDAO baseHibernateDao = null;
try{
//baseHibernateDao = new BaseHibernateDAO();
tx =HibernateSessionFactory.getSession().beginTransaction(); //getSession().beginTransaction();
for (int i = 0; i < list.size(); i++) {
bean = list.get(i);
cls = bean.getClass(); //获得“bean”类名
method = cls.getMethod("getActionType", null);
action = (String) method.invoke(bean, null); //"add"
method = cls.getMethod("getIDao", null);
dao = method.invoke(bean, null); //"PositionmDAOImpl"
daoclass = dao.getClass();
Class beanclass[] = {cls};
Object beanparm[] = {bean};
method = daoclass.getMethod(action, beanclass);
method.invoke(dao, beanparm);
}
// 提交事务
tx.commit();
}catch (InvocationTargetException ite){ //捕捉反射类可能出现的异常
if (tx != null) {
try {
tx.rollback(); // 事务回滚
} catch (HibernateException hbex) {
log.error("vteam.frame.core.dao.DAOExecutor(executorDAO()),tx rollback InvocationTargetException:",hbex);
}
}
if(ite.getTargetException() instanceof RollbackException) {
throw (RollbackException) ite.getTargetException();//add / mod /del (table) exception
}else{
throw new StException("System Execute Exception : " + ite.getMessage());
}
}
catch(HibernateException hh)
{
// tx.rollback();
// log.error(cls +" "+action.toString()+ " failed", hh);
// throw new RollbackException(FrameConst.MSG_ADD_FAILURE,cls.toString());
throw new HibernateException( cls + " Execute hb Error:" + hh.getMessage());
}
catch (Exception ee){//其他异常
if (tx != null) {
// try {
tx.rollback();
// } catch (HibernateException eex) {
log.error("vteam.frame.core.dao.DAOExecutor(executorDAO()),tx rollback Exception:",ee);
// }
}
throw new StException("System Execute Error:" + ee.getMessage());
}
finally{
list.clear();
list = null;
bean = null;
cls = null;
method = null;
action = null;
dao = null;
daoclass = null;
try {
HibernateSessionFactory.getSession().close();
}catch (HibernateException ex){
log.error("vteam.frame.core.dao.DAOExecutor(executorDAO()),Session close Exception:",ex);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -