?? 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 + -