J 2 EE動的データソースの使用方法

12577 ワード

  :

     ,         ,        ,              ,      。               。

 

          ,              ,                 ,                ,         。

 

    :

 

   datasource,    AbstractRoutingDataSource , spring   。
Java        

    /** 
     * <p> 
     * Title:         
     * </p> 
     *  
     * <p> 
     * Description:  
     * </p> 
     *  
     * <p> 
     * Copyright:          2010 
     * </p> 
     *  
     * @author    HO174959 
     * @version 1.0 
     * @date Jul 23, 2010 
     * 
     */  
    public class CustomerRoutingDataSource extends AbstractRoutingDataSource {  
      
     /** 
      * <Description>         Key  
      * 
      * @since Jul 23, 2010 
      * @return <Description> 
      * 
      */  
     @Override  
     protected Object determineCurrentLookupKey() {  
      return CustomerContextHolder.getDataBase();     
     }  
    }  

 

AbstractRoutingDataSource       ,      ,     targetDataSources    map  ,key     ID,value    datasource,    spring    :
Java        

    package com.rb.util;  
      
      
      
    import java.sql.Connection;  
    import java.sql.SQLException;  
    import java.util.*;  
    import javax.sql.DataSource;  
    import org.springframework.beans.factory.InitializingBean;  
    import org.springframework.jdbc.datasource.AbstractDataSource;  
    import org.springframework.jdbc.datasource.lookup.DataSourceLookup;  
    import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;  
    import org.springframework.util.Assert;  
      
      
    public abstract class AbstractRoutingDataSource extends AbstractDataSource  
            implements InitializingBean {  
      
        public AbstractRoutingDataSource() {  
            dataSourceLookup = new JndiDataSourceLookup();  
        }  
      
        public void setTargetDataSources(Map targetDataSources) {  
            this.targetDataSources = targetDataSources;  
        }  
        public Map getTargetDataSources() {  
            return this.targetDataSources;  
        }  
        public void setDefaultTargetDataSource(Object defaultTargetDataSource) {  
            this.defaultTargetDataSource = defaultTargetDataSource;  
        }  
      
        public void setDataSourceLookup(DataSourceLookup dataSourceLookup) {  
            this.dataSourceLookup = ((DataSourceLookup) (dataSourceLookup == null ? ((DataSourceLookup) (new JndiDataSourceLookup()))  
                    : dataSourceLookup));  
        }  
      
        public void afterPropertiesSet() {  
            if (targetDataSources == null)  
                throw new IllegalArgumentException("targetDataSources is required");  
            resolvedDataSources = new HashMap(targetDataSources.size());  
            Object lookupKey;  
            DataSource dataSource;  
            for (Iterator it = targetDataSources.entrySet().iterator(); it  
                    .hasNext(); resolvedDataSources.put(lookupKey, dataSource)) {  
                java.util.Map.Entry entry = (java.util.Map.Entry) it.next();  
                lookupKey = resolveSpecifiedLookupKey(entry.getKey());  
                dataSource = resolveSpecifiedDataSource(entry.getValue());  
            }  
      
            if (defaultTargetDataSource != null)  
                resolvedDefaultDataSource = resolveSpecifiedDataSource(defaultTargetDataSource);  
        }  
      
        protected DataSource resolveSpecifiedDataSource(Object dataSource)  
                throws IllegalArgumentException {  
            if (dataSource instanceof DataSource)  
                return (DataSource) dataSource;  
            if (dataSource instanceof String)  
                return dataSourceLookup.getDataSource((String) dataSource);  
            else  
                throw new IllegalArgumentException(  
                        "Illegal data source value - only [javax.sql.DataSource] and String supported: "  
                                + dataSource);  
        }  
      
        public Connection getConnection() throws SQLException {  
            return determineTargetDataSource().getConnection();  
        }  
      
        public Connection getConnection(String username, String password)  
                throws SQLException {  
            return determineTargetDataSource().getConnection(username, password);  
        }  
      
        protected DataSource determineTargetDataSource() {  
            Assert  
                    .notNull(resolvedDataSources,  
                            "DataSource router not initialized");  
            Object lookupKey = determineCurrentLookupKey();  
            DataSource dataSource = (DataSource) resolvedDataSources.get(lookupKey);  
            if (dataSource == null)  
                dataSource = resolvedDefaultDataSource;  
            if (dataSource == null)  
                throw new IllegalStateException(  
                        "Cannot determine target DataSource for lookup key ["  
                                + lookupKey + "]");  
            else  
                return dataSource;  
        }  
      
        protected Object resolveSpecifiedLookupKey(Object lookupKey) {  
            return lookupKey;  
        }  
      
        protected abstract Object determineCurrentLookupKey();  
      
        private Map targetDataSources;  
      
        private Object defaultTargetDataSource;  
      
        private DataSourceLookup dataSourceLookup;  
      
        private Map resolvedDataSources;  
      
        private DataSource resolvedDefaultDataSource;  
    }  
      
      
    /* 
        DECOMPILATION REPORT 
     
        Decompiled from: E:\myeclipse6.0_project\ToolServerFlex\WebRoot\WEB-INF\lib\spring.jar 
        Total time: 218 ms 
        Jad reported messages/errors: 
    The class file version is 48.0 (only 45.3, 46.0 and 47.0 are supported) 
        Exit status: 0 
        Caught exceptions: 
    */  

 

 

CustomerContextHolder  :
Java        

    /** 
     * <p> 
     * Title:         
     * </p> 
     *  
     * <p> 
     * Description:  
     * </p> 
     *  
     * <p> 
     * Copyright:          2010 
     * </p> 
     *  
     * @author    HO174959 
     * @version 1.0 
     * @date Jul 23, 2010 
     * 
     */  
    public class CustomerContextHolder {  
        private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();  
      
        public   static void setDataBase(String dataBase) {  
            contextHolder.set(dataBase);  
        }  
      
        public static String getDataBase() {  
            return (String) contextHolder.get();  
        }  
      
        public static void clearDataBase() {  
            contextHolder.remove();  
        }  
      
    }  

            ,    setDataBase,      key       。

 

              :
Java        

    package com.rb.util;  
      
    import java.sql.Connection;  
    import java.sql.SQLException;  
      
    import javax.sql.DataSource;  
      
    public class TransactionManager {  
        //private Connection conn;  
        private DataSource dataSource;  
        public static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();  
          
        public DataSource getDataSource() {  
            return dataSource;  
        }  
        public void setDataSource(DataSource dataSource) {  
            this.dataSource = dataSource;  
        }  
        public TransactionManager() {  
        }  
          
    //  public synchronized Connection  getConnection(){  
    //      Connection conn = tl.get();  
    //      if(conn==null){  
    //          try {  
    //              conn = dataSource.getConnection();  
    //          } catch (SQLException e) {  
    //              e.printStackTrace();  
    //          }  
    //          tl.set(conn);  
    //      }  
    //      return conn;  
    //  }  
        /**      */  
        public void beginTransaction() throws SQLException{  
            try {  
                Connection conn = tl.get();  
                if(conn==null){  
                    conn = dataSource.getConnection();  
                    tl.set(conn);  
                }  
                conn.setAutoCommit(false); //               
            } catch (SQLException e) {  
                throw new SQLException("         ");  
            }  
        }  
        /**           */  
        public void commitAndClose() throws SQLException{  
            Connection conn  = null;  
            try {  
                conn = tl.get();  
                conn.commit(); //      
            } catch (SQLException e) {  
                throw new SQLException("         ");  
            }finally{  
                if(conn!=null){  
                    conn.close();  
                }  
                tl.remove(); //        
            }  
        }  
        /**         */  
        public void rollbackAndClose()throws SQLException{  
            Connection conn  = null;  
            try {  
                conn = tl.get();  
                conn.rollback();  
            } catch (SQLException e) {  
                throw new SQLException("         ");  
            } finally{  
                if(conn!=null){  
                    conn.close();  
                }  
                tl.remove(); //        
            }  
        }  
    }  

    setDataBase    ,  beginTransaction  ,       ,connection null,   datasource   ,     datasource           ,     map  。   conn = dataSource.getConnection();   ,   AbstractRoutingDataSource  

  :
Java        

    public Connection getConnection() throws SQLException {  
      return determineTargetDataSource().getConnection();  
     }  

 

   :
Java        

    protected DataSource determineTargetDataSource() {  
            Assert  
                    .notNull(resolvedDataSources,  
                            "DataSource router not initialized");  
            Object lookupKey = determineCurrentLookupKey();  
            DataSource dataSource = (DataSource) resolvedDataSources.get(lookupKey);  
            if (dataSource == null)  
                dataSource = resolvedDefaultDataSource;  
            if (dataSource == null)  
                throw new IllegalStateException(  
                        "Cannot determine target DataSource for lookup key ["  
                                + lookupKey + "]");  
            else  
                return dataSource;  
        }  

        :
Java        

    Object lookupKey = determineCurrentLookupKey();  

            ,             setDataBase   ,        datasource。

                 。