<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    要實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的操作,離不開數(shù)據(jù)源(DataSource)或者連接(Connection),但是通常來(lái)說(shuō)對(duì)數(shù)據(jù)庫(kù)的操作都應(yīng)該放在DAO中,而DAO又不應(yīng)該與應(yīng)用服務(wù)器相關(guān)聯(lián),所以一般都使用連接(Connection)。現(xiàn)在我們這里就有一個(gè)問(wèn)題了,怎么在攔截器中獲得連接。我想可以通過(guò)兩種方式獲得:
    在分別討論這兩種方法之前,我們需要先討論一下在處理數(shù)據(jù)庫(kù)的時(shí)候的異常的處理。我這里做了一個(gè)TransactionException繼承至RuntimeException然后在攔截器里面拋出,再又應(yīng)用框架處理這個(gè)異常。下面試這個(gè)類的代碼:
    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)    通過(guò)方法的第一個(gè)參數(shù)傳進(jìn)去
    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就是需要進(jìn)行事務(wù)處理的方法的開頭,這個(gè)方法可以寫成一個(gè)從配置文件里面去讀,這里我就寫死在里面了。只是對(duì)insert和update開頭的方法進(jìn)行事務(wù)控制。
    2)    將Connection對(duì)象放在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() {
            實(shí)現(xiàn)自己獲取連接的代碼
            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;
        }
    }
        最后將這個(gè)攔截器添加到AOP攔截框架中去,InterceptorHandler類中的getIntercetors方法中添加一個(gè):

        private synchronized List getIntercetors(){
            if(null == interceptors){
                interceptors = new ArrayList();
                ……………………………………
    interceptors.add(new TransactionInterceptor ());
                ……………………………………
            }
            return interceptors;
    }
    posted on 2008-04-22 09:54 LifeNote 閱讀(1496) 評(píng)論(0)  編輯  收藏 所屬分類: JavaSpring
     
    主站蜘蛛池模板: 亚洲色大18成人网站WWW在线播放| 亚洲αv在线精品糸列| 成年人免费网站在线观看| 国产一卡2卡3卡4卡无卡免费视频| 免费观看的a级毛片的网站| 日本特黄特色免费大片| 亚洲国产日韩在线视频| 亚洲成人动漫在线观看| 黄色一级视频免费| 中文在线免费不卡视频| 精品国产无限资源免费观看| 免费人成在线观看视频播放| 国产成人A人亚洲精品无码| 学生妹亚洲一区二区| a级毛片在线免费| 青青草免费在线视频| 亚洲av午夜福利精品一区人妖| 皇色在线免费视频| 四虎www成人影院免费观看| 久久综合亚洲色一区二区三区| 另类小说亚洲色图| 波多野结衣中文字幕免费视频| 亚洲jjzzjjzz在线观看| a级在线免费观看| 中文字幕亚洲第一在线| 日韩免费a级毛片无码a∨| 国产精品亚洲lv粉色| 国产香蕉免费精品视频| 亚洲乱码一区二区三区国产精品| 爽爽日本在线视频免费| 国产V片在线播放免费无码| 成年人视频在线观看免费| 午夜亚洲乱码伦小说区69堂| 国产国拍精品亚洲AV片| 亚洲欧美国产国产一区二区三区| 久久成人国产精品免费软件| 国产av天堂亚洲国产av天堂| 成人浮力影院免费看| 男人免费视频一区二区在线观看| 日本免费福利视频| 搡女人免费免费视频观看|