有色人生 发表于 2009-1-11 10:32:47

用动态代理实现AOP对数据库进行操作

要实现对数据库的操作,离不开数据源(DataSource)或者连接(Connection),但是通常来说对数据库的操作都应该放在DAO中,而DAO又不应该与应用服务器相关联,所以一般都使用连接(Connection)。现在我们这里就有一个问题了,怎么在拦截器中获得连接。我想可以通过两种方式获得:
在分别讨论这两种方法之前,我们需要先讨论一下在处理数据库的时候的异常的处理。我这里做了一个TransactionException继承至RuntimeException然后在拦截器里面抛出,再又应用框架处理这个异常。下面试这个类的代码:




public class TransactionException extends RuntimeException {
    private Throwable superException;
    private String myMessage;
   
    public TransactionException(Throwable throwable){
      super(throwable);
      this.superException = throwable;
    }
   
    public TransactionException(Throwable throwable,String message){
      super(message,throwable);
      this.superException = throwable;
      this.myMessage = message;
    }

    /**
   * @return Returns the myMessage.
   */
    public String getMessage() {
      return myMessage;
    }

    /**
   * @return Returns the superException.
   */
    public Throwable getSuperException() {
      return superException;
    }

    /**
   * @param myMessage The myMessage to set.
   */
    public void setMyMessage(String message) {
      this.myMessage = message;
    }

    /**
   * @param superException The superException to set.
   */
    public void setSuperException(Throwable superException) {
      this.superException = superException;
    }
   
   
}
1)    通过方法的第一个参数传进去
l    DAO
import java.sql.Connection;

public class TestDao {
    public void insertA(Connection con,String a,String b,……){
      …………………………………………
一系列操作
…………………………………………
    }
   
    public String queryA(Connection con,…….){
    …………………………………………
一系列操作
…………………………………………
}

    public void updateA(Connection con,…….){
      …………………………………………
一系列操作
…………………………………………
}
}

l    拦截器
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TransactionInterceptor implements Interceptor {

    public void before(InvokeJniInfo invInfo) {
      if(isNeedTransactions(invInfo)){
            Connection conn = (Connection) invInfo.getArgs();
            try {
                conn.setAutoCommit(false);
            } catch (SQLException e) {
                throw new TransactionException(e);
            }
      }
    }

    public void after(InvokeJniInfo invInfo) {
      if(isNeedTransactions(invInfo)){
            Connection conn = (Connection) invInfo.getArgs();
            try {
                conn.commit();
            } catch (SQLException e) {
                throw new TransactionException(e);
            }finally{
                if(conn != null){
                  try {
                        conn.close();
                  } catch (SQLException e) {
                        throw new TransactionException(e,"Close Connection is failure!");
                  }
                }
            }
      }
    }

    public void exceptionThrow(InvokeJniInfo invInfo) {
      if(isNeedTransactions(invInfo)){
            Connection conn = (Connection) invInfo.getArgs();
            try {
                conn.rollback();
            } catch (SQLException e) {
                throw new TransactionException(e);
            }finally{
                if(conn != null){
                  try {
                        conn.close();
                  } catch (SQLException e) {
                        throw new TransactionException(e,"Close Connection is failure!");
                  }
                }
            }
      }
    }
   
    private List getNeedTransaction(){
      List needTransactions = new ArrayList();
      needTransactions.add("insert");
      needTransactions.add("update");
      return needTransactions;
    }
   
    private boolean isNeedTransactions(InvokeJniInfo invInfo){
      String needTransaction = "";
      List needTransactions = getNeedTransaction();
      for(int i = 0;i
页: [1]
查看完整版本: 用动态代理实现AOP对数据库进行操作