package com.gzccit.aop;import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;import org.apache.log4j.Logger;import com.gzccit.dao.ConnManager;
import com.gzccit.test.model.ThreadUtil;
import com.gzccit.test.util.CommonUtil;
import com.gzccit.test.util.CortexLogUtils;
import com.gzccit.test.util.DataBaseUtil;
/**
 * 该类实现了JDK动态代理功能   
 * 数据操作前开启连接,完成后关闭连接,并且做日志记录
 * 
 * @author 
 *  
 */
public class JDKProxyFactory implements InvocationHandler{
private Object targetObject;

/**
 * 获取指定对象的动态代理类,
 * @param targetObject 需要加入动态代理的对象
 * @return
 */
public Object createProxyIntance(Object targetObject){
this.targetObject = targetObject;
return Proxy.newProxyInstance(this.targetObject.getClass().getClassLoader(), 
this.targetObject.getClass().getInterfaces(), this);
} public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("\n方法 "+method.getName()+" 开始----->");
// System.out.println("类名:"+this.targetObject.getClass().getName());
// if(args!=null){
// System.out.println("参数列表:");
// for (int i = 0; i < args.length; i++) {
// System.out.println(args[i]);
// }
// }

Object result = null; 
Connection conn = null;
try {
conn = ConnManager.getInsance().getConn();
conn.setAutoCommit(false);
ThreadUtil.setConnection(conn);
result = method.invoke(targetObject, args);
conn.commit();
// afteradvice() -->后置通知
} catch (RuntimeException e) {
//e.printStackTrace();
conn.rollback();
Logger logger=CortexLogUtils.getLogger();
for (int i = 0; i < args.length; i++) {

CommonUtil.logValue(args[i]);
}
}finally{
ConnManager.releaseResource(conn, null, null);
ThreadUtil.setConnection(null);
System.out.println("<------方法 "+method.getName()+" 结束\n");

}
return result;
}}package com.gzccit.test.action;import com.gzccit.aop.JDKProxyFactory;
import com.gzccit.test.ITest;
import com.gzccit.testImpl.testImpl;
/**
 * Action类 未继承Action 
 * @author Administrator
 *
 */
public class testAction {
/*
 * Action 供Flex调用的
 */
public String test(String mainDB){
JDKProxyFactory factory = new JDKProxyFactory();
ITest service = (ITest)factory.createProxyIntance(new testImpl());
Object obj = service.myTest(mainDB);
return service.myTest(mainDB);
}
}
package com.gzccit.test;/**
 * 接口类
 * @author Administrator
 *
 */
public interface ITest {
/**
 * 测试方法
 */
public String myTest(String mainDB);}java接口实现类
package com.gzccit.testImpl;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import com.gzccit.dao.ConnManager;
import com.gzccit.test.ITest;
import com.gzccit.test.model.*;
import com.gzccit.test.util.CCITException;
/**
 * 接口实现类
 * @author Administrator
 *
 */
public class testImpl implements ITest { public String myTest(String mainDB) {
// TODO Auto-generated method stub
Connection conn;
conn=ThreadUtil.getConnection();
String ret = null;
Statement statement=null;
try {
statement = conn.createStatement();
statement.execute("use "+mainDB);
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
ret = "#ER_"+e2.getMessage();
throw new CCITException(ret,e2);
}

ResultSet rs1=null;
String sql="";
System.out.println("mainDB:====="+mainDB);
try {
sql = "SELECT TOP 5 * FROM RYZD";
statement = conn.createStatement();
rs1 = ThreadUtil.getConnection().createStatement().executeQuery(sql);
while(rs1.next()){
String sm1 = rs1.getString(1);
System.out.println("人员字典Sm1_F:"+sm1);
}
conn.commit();
} catch (SQLException e) {
// TODO Auto-generated catch block ConnManager.releaseResource(conn, statement, rs1);
try {
conn.rollback();
e.printStackTrace();
ret = "#ER_"+e.getMessage();
throw new CCITException(ret,e);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
ret = "#ER_"+e1.getMessage();
throw new CCITException(ret,e1);
}
}finally{
ConnManager.releaseResource(conn, null, null);//关闭
}
return ret;
}}
现在小弟很郁闷的是 如果我把接口实现类中的 
         finally{
ConnManager.releaseResource(conn, null, null);//关闭
}给删除后Flex那边就不会收到错误!如果加上这句话就会有提示 java.lang.reflect.UndeclaredThrowableException : null
请高手赐教...小弟在线等...

解决方案 »

  1.   

    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    import java.sql.Connection;import org.apache.log4j.Logger;import com.gzccit.dao.ConnManager;
    import com.gzccit.test.model.ThreadUtil;
    import com.gzccit.test.util.CommonUtil;
    import com.gzccit.test.util.CortexLogUtils;
    import com.gzccit.test.util.DataBaseUtil;
    /**
     * 该类实现了JDK动态代理功能   
     * 数据操作前开启连接,完成后关闭连接,并且做日志记录
     *  
     * @author  
     *   
     */
    public class JDKProxyFactory implements InvocationHandler{
    private Object targetObject;/**
    * 获取指定对象的动态代理类,
    * @param targetObject 需要加入动态代理的对象
    * @return
    */
    public Object createProxyIntance(Object targetObject){
    this.targetObject = targetObject;
    return Proxy.newProxyInstance(this.targetObject.getClass().getClassLoader(),  
    this.targetObject.getClass().getInterfaces(), this);
    }public Object invoke(Object proxy, Method method, Object[] args)
    throws Throwable {
    System.out.println("\n方法 "+method.getName()+" 开始----->");
    // System.out.println("类名:"+this.targetObject.getClass().getName());
    // if(args!=null){
    // System.out.println("参数列表:");
    // for (int i = 0; i < args.length; i++) {
    // System.out.println(args[i]);
    // }
    // }Object result = null;  
    Connection conn = null;
    try {
    conn = ConnManager.getInsance().getConn();
    conn.setAutoCommit(false);
    ThreadUtil.setConnection(conn);
    result = method.invoke(targetObject, args);
    conn.commit();
    // afteradvice() -->后置通知
    } catch (RuntimeException e) {
    //e.printStackTrace();
    conn.rollback();
    Logger logger=CortexLogUtils.getLogger();
    for (int i = 0; i < args.length; i++) {CommonUtil.logValue(args[i]);
    }
    }finally{
    ConnManager.releaseResource(conn, null, null);
    ThreadUtil.setConnection(null);
    System.out.println("<------方法 "+method.getName()+" 结束\n");}
    return result;
    }}package com.gzccit.test.action;import com.www.8892.orggzccit.aop.JDKProxyFactory;
    import com.gzccit.test.ITest;
    import com.gzccit.testImpl.testImpl;
    /**
     * Action类 未继承Action  
     * @author Administrator
     *
     */
    public class testAction {
    /*
    * Action 供Flex调用的
    */
    public www.163xyx.comString test(String mainDB){
    JDKProxyFactory factory = new JDKProxyFactory();
    ITest service = (ITest)factory.createProxyIntance(new testImpl());
    Object obj = service.myTest(mainDB);
    return service.myTest(mainDB);
    }
    }
    package com.gzccit.test;/**
     * 接口类
     * @author Administrator
     *
     */
    public interface ITest {
    /**
    * 测试方法
    */
    public www.cctv40.cnString myTest(String mainDB);}java接口实现类
    package com.gzccit.testImpl;import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;import com.gzccit.dao.ConnManager;
    import com.gzccit.test.ITest;
    import com.gzccit.test.model.*;
    import com.gzccit.test.util.CCITException;
    /**
     * 接口实现类
     * @author Administrator
     *
     */
    public www.cctv40.comclass testImpl implements ITest {public String myTest(String mainDB) {
    // TODO Auto-generated method stub
    Connection conn;
    conn=ThreadUtil.getConnection();
    String ret = null;
    Statement statement=null;
    try {
    statement = conn.createStatement();
    statement.execute("use "+mainDB);
    } catch (SQLException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
    ret = "#ER_"+e2.getMessage();
    throw new CCITException(ret,e2);
    }ResultSet rs1=null;
    String sql="";
    System.out.println("mainDB:====="+mainDB);
    try {
    sql = "SELECT TOP 5 * FROM RYZD";
    statement = conn.createStatement();
    rs1 = ThreadUtil.getConnection().createStatement().executeQuery(sql);
    while(rs1.next()){
    String sm1 = rs1.getString(1);
    System.out.println("人员字典Sm1_F:"+sm1);
    }
    conn.commit();
    } catch (SQLException e) {
    // TODO Auto-generated catch blockConnManager.releaseResource(conn, statement, rs1);
    try {
    conn.rollback();
    e.printStackTrace();
    ret = "#ER_"+e.getMessage();
    throw new CCITException(ret,e);
    } catch (SQLException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    ret = "#ER_"+e1.getMessage();
    throw new CCITException(ret,e1);
    }
    }finally{
    ConnManager.releaseResource(conn, null, null);//关闭
    }
    return ret;
    }}
      

  2.   

    代码太长了
    如果代理实例的调用处理程序的 invoke 方法抛出一个经过检查的异常(不可分配给 RuntimeException 或 Error 的 Throwable),且该异常不可分配给该方法(在代理实例上调用该方法,并将其指派到调用处理程序)的 throws 子句中声明的任何异常类,则由代理实例上的方法调用抛出此异常。
    UndeclaredThrowableException 实例包含由调用处理程序抛出的经过检查的未声明异常,而且可以使用 getUndeclaredThrowable() 方法检索。
      

  3.   

    ConnManager.releaseResource(conn, null, null);這句中有個參數是不是異常類型的,需要指定呢?