国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

使用動態代理實現用AOP對數據庫進行操作

2019-11-17 04:59:29
字體:
來源:轉載
供稿:網友
要實現對數據庫的操作,離不開數據源(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()[0];
            try {
                conn.setAutoCommit(false);
            } catch (SQLException e) {
                throw new TransactionException(e);
            }
        }
    }

    public void after(InvokeJniInfo invInfo) {
        if(isNeedTransactions(invInfo)){
            Connection conn = (Connection) invInfo.getArgs()[0];
            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()[0];
            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            needTransaction = (String)needTransactions.get(i);
            if(invInfo.getMethod().getName().startsWith(needTransaction)){
                return true;
            }
        }
        return false;
    }
}

需要注意的是:getNeedTransaction就是需要進行事務處理的方法的開頭,這個方法可以寫成一個從配置文件里面去讀,這里我就寫死在里面了。只是對insert和update開頭的方法進行事務控制。
2)    將Connection對象放在ThreadLocal中
l    ConnectionUtil類:
import java.sql.Connection;

public final class ConnectionUtil {
    private static ThreadLocal connections = new ThreadLocal();
    public static Connection getConnection(){
        Connection conn = null;
        conn = (Connection) connections.get();
        if(conn == null){
            conn = getRealConnection();
            connections.set(conn);
        }
        return conn;
    }
    public static void realseConnection(Connection conn){
        connections.set(null);
    }
    private static Connection getRealConnection() {
        實現自己獲取連接的代碼
        return null;
    }
}
l    DAO類
public class TestDao {
    public void insertA(String a,String b){
        Connection conn = getConnection();
        …………………………………………
一系列操作
…………………………………………
    }
        public String queryA(Connection con,…….){
        Connection conn = getConnection();
    …………………………………………
一系列操作
…………………………………………
}

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

    private Connection getConnection(){
        return ConnectionUtil.getConnection();
    }
    
}
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 = getConnection();
            try {
                conn.setAutoCommit(false);
            } catch (SQLException e) {
                throw new TransactionException(e);
            }
        }
    }

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

    public void exceptionThrow(InvokeJniInfo invInfo) {
        if(isNeedTransactions(invInfo)){
            Connection conn = getConnection();
            try {
                conn.rollback();
            } catch (SQLException e) {
                throw new TransactionException(e);
            }finally{
                if(conn != null){
                    try {
                        conn.close();
                        releaseConnection(conn);
                    } catch (SQLException e) {
                        throw new TransactionException(e,"Close Connection is failure!");
                    }
                }
            }
        }
    }
    
    private Connection getConnection(){
        return ConnectionUtil.getConnection();
    }
    
    private void releaseConnection(Connection conn){
        ConnectionUtil.releaseConnection(conn);
    }
    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            needTransaction = (String)needTransactions.get(i);
            if(invInfo.getMethod().getName().startsWith(needTransaction)){
                return true;
            }
        }
        return false;
    }
}
    最后將這個攔截器添加到AOP攔截框架中去,InterceptorHandler類中的getIntercetors方法中添加一個:

    private synchronized List getIntercetors(){
        if(null == interceptors){
            interceptors = new ArrayList();
            ……………………………………
interceptors.add(new TransactionInterceptor ());
            ……………………………………
        }
        return interceptors;
}

到此全部ok!
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 襄汾县| 松原市| 左云县| 邹城市| 苗栗市| 万源市| 开远市| 桂阳县| 明光市| 三亚市| 特克斯县| 安塞县| 兴化市| 玉环县| 牙克石市| 海安县| 昭苏县| 鲁甸县| 鹤庆县| 霞浦县| 镇宁| 赣州市| 海原县| 安徽省| 黄梅县| 新津县| 阳城县| 南安市| 沈丘县| 桂阳县| 长泰县| 普洱| 永登县| 黎川县| 北辰区| 乐平市| 沂源县| 齐河县| 吉安县| 西乡县| 乐陵市|