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

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

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

    云自無心水自閑

    天平山上白云泉,云自無心水自閑。何必奔沖山下去,更添波浪向人間!
    posts - 288, comments - 524, trackbacks - 0, articles - 6
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    IBatis3中使用自定義數據源C3P0

    Posted on 2010-02-01 12:57 云自無心水自閑 閱讀(3916) 評論(2)  編輯  收藏 所屬分類: Java心得體會iBatis
    IBatis2中提供了3種DataSource的配置:JNDI, Apache DBCP, IBatis自帶的SimpleDataSource。但在IBatis3中只提供了兩種DataSource: UNPOOLED, POOLED。
    如果要實現自定義的DataSource,就需要通過擴展DataSourceFactory。本文就演示一下這個過程。
    準備工作:Connection Pool的選擇,通過搜索發現目前比較流行的免費數據庫連接池主要有3種:Apache DBCP, C3P0, Proxool。
    看了一下,Proxool的最新版本是0.9.1(2008-08-23), C3P0的最新版本是0.9.1.2(2007-05-21), DBCP最新版本是1.2.2(2007-04-04)
    好像這3個項目都已經挺長時間沒有更新了。但是總體評價上C3P0無論從穩定上還是效率上都要好一點。
    (具體這3個項目誰更優秀,并不是本文的重點,本文主要是介紹一下如何在IBatis3中自定義數據源)
    大致步驟:
    1、實現org.apache.ibatis.datasource.DataSourceFactory接口,主要是2個方法
    a、public DataSource getDataSource() 如何具體地得到一個數據源
    b、public void setProperties(Properties properties) 如何設置數據源的參數屬性
    2、實現javax.sql.DataSource,這個就是提供給DataSourceFactory的實例
    3、在IBatis3中引用新加入的數據源

    1. 從代碼中可以看出,IBatis3與IBatis2不同,不再通過一個Configuration類來進行數據源屬性的設置,而是使用反射機制直接調用數據源的方法來設置參數。
    這就要求配置文件中的參數名稱必須與數據源類中的方法名匹配.
     1 public class C3p0DataSourceFactory implements DataSourceFactory {
     2 
     3     private DataSource dataSource;
     4 
     5     public C3p0DataSourceFactory() {
     6         dataSource = new C3p0DataSource();
     7     }
     8 
     9     public DataSource getDataSource() {
    10         return dataSource;
    11     }
    12 
    13     public void setProperties(Properties properties) {
    14         Properties driverProperties = new Properties();
    15         MetaObject metaDataSource = MetaObject.forObject(dataSource);
    16         for (Object key : properties.keySet()) {
    17             String propertyName = (String) key;
    18             if (propertyName.startsWith(DRIVER_PROPERTY_PREFIX)) {
    19                 String value = properties.getProperty(propertyName);
    20                 driverProperties.setProperty(propertyName
    21                         .substring(DRIVER_PROPERTY_PREFIX_LENGTH), value);
    22             } else if (metaDataSource.hasSetter(propertyName)) {
    23                 String value = (String) properties.get(propertyName);
    24                 Object convertedValue = convertValue(metaDataSource,
    25                         propertyName, value);
    26                 metaDataSource.setValue(propertyName, convertedValue);
    27             } else {
    28                 throw new DataSourceException("Unkown DataSource property: "
    29                         + propertyName);
    30             }
    31         }
    32         if (driverProperties.size() > 0) {
    33             metaDataSource.setValue("driverProperties", driverProperties);
    34         }
    35     }
    36 
    37     @SuppressWarnings("unchecked")
    38     private Object convertValue(MetaObject metaDataSource, String propertyName,
    39             String value) {
    40         Object convertedValue = value;
    41         Class targetType = metaDataSource.getSetterType(propertyName);
    42         if (targetType == Integer.class || targetType == int.class) {
    43             convertedValue = Integer.valueOf(value);
    44         } else if (targetType == Long.class || targetType == long.class) {
    45             convertedValue = Long.valueOf(value);
    46         } else if (targetType == Boolean.class || targetType == boolean.class) {
    47             convertedValue = Boolean.valueOf(value);
    48         }
    49         return convertedValue;
    50     }
    51 
    52     private static final String DRIVER_PROPERTY_PREFIX = "driver.";
    53     private static final int DRIVER_PROPERTY_PREFIX_LENGTH = DRIVER_PROPERTY_PREFIX
    54             .length();
    55 
    56 }
    57 

    2. 數據源類,其中的一堆setter就是用于設置屬性的。
     1 public class C3p0DataSource implements DataSource {
     2 
     3     private ComboPooledDataSource dataSource;
     4     public C3p0DataSource() {
     5         this.dataSource = new ComboPooledDataSource();
     6     }
     7     
     8     public Connection getConnection() throws SQLException {
     9         return dataSource.getConnection();
    10     }
    11 
    12     public Connection getConnection(String username, String password)
    13             throws SQLException {
    14         return dataSource.getConnection(username, password);
    15     }
    16 
    17     public PrintWriter getLogWriter() throws SQLException {
    18         return dataSource.getLogWriter();
    19     }
    20 
    21     public int getLoginTimeout() throws SQLException {
    22         return dataSource.getLoginTimeout();
    23     }
    24 
    25     public void setLogWriter(PrintWriter out) throws SQLException {
    26         dataSource.setLogWriter(out);
    27     }
    28 
    29     public void setLoginTimeout(int seconds) throws SQLException {
    30         dataSource.setLoginTimeout(seconds);
    31     }
    32     
    33     
    34     public synchronized void setDriver(String driver) {
    35         try {
    36             dataSource.setDriverClass(driver);
    37         } catch (Exception e) {
    38         }
    39     }
    40     
    41     public void setUrl(String url) {
    42         dataSource.setJdbcUrl(url);
    43     }
    44     
    45     public void setUsername(String username) {
    46           dataSource.setUser(username);
    47     }
    48 
    49     public void setPassword(String password) {
    50         dataSource.setPassword(password);
    51     }
    52     
    53     public void setInitialPoolSize(int initialPoolSize) {
    54         dataSource.setInitialPoolSize(initialPoolSize);
    55     }
    56     
    57     public void setMaxPoolSize(int maxPoolSize) {
    58         dataSource.setMaxPoolSize(maxPoolSize);
    59     }
    60       
    61     public void setMinPoolSize(int minPoolSize) {
    62         dataSource.setMinPoolSize(minPoolSize);
    63     }
    64     
    65     public void setPreferredTestQuery(String preferredTestQuery) {
    66         dataSource.setPreferredTestQuery(preferredTestQuery);
    67     }
    68     
    69     public void setPoolPingQuery(String poolPingQuery) {
    70         dataSource.setPreferredTestQuery(poolPingQuery);
    71     }
    72 }

    3. 在配置文件Configuration.xml中,可以先定義數據源的別稱,然后就象POOLED和UNPOOLED一樣使用別稱來引用數據源。
    <Configuration>
        ...
        <typeAlias>
            <typeAlias type="com.test.datasource.C3p0DataSourceFactory" alias="C3P0"/>
        </typeAlias>
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="C3P0">
                    <property name="driver" value="${jdbc.driver}"/>
                    <property name="url" value="${jdbc.url}"/>
                    <property name="username" value="${jdbc.username}"/>
                    <property name="password" value="${jdbc.password}"/>
                    <property name="poolPingQuery" value="${pingquery}"/>           
                </dataSource>
            </environment>
        </environments>
        ...
    <Configuration>






    評論

    # re: IBatis3中使用自定義數據源C3P0  回復  更多評論   

    2010-11-07 01:39 by wppurking
    1. public class C3p0 extends UnpooledDataSourceFactory;
    (com.package.c3p0_ibatis)

    2. 配置文件中設置
    <environments default="development">
    <environment id="development">
    <transactionManager type="JDBC"/>
    <dataSource type="com.package..C3p0"> <!-- 配置全名 -->
    <!-- 連接池設置由C3p0.properties自己決定 -->
    </dataSource>
    </environment>
    </environments>

    3. 將 c3p0.properties 文件放入 classpath 中

    這樣省去很多代碼,還可以完全設置 c3p0 的各個配置屬性,簡單很多,試一下?

    # re: IBatis3中使用自定義數據源C3P0  回復  更多評論   

    2010-11-07 01:41 by wppurking
    補充一下:繼承后需要與 PooledDataSourceFactory 做同樣的操作,編寫自己的無參構造方法: this.dataSource = new ComboPooledDataSource();
    主站蜘蛛池模板: 久久91亚洲人成电影网站| 一二三四在线观看免费高清中文在线观看| 扒开双腿猛进入爽爽免费视频| 99久久亚洲精品无码毛片| 99精品视频在线观看免费| 亚洲婷婷综合色高清在线| 亚洲熟女乱色一区二区三区| 亚洲久悠悠色悠在线播放| 在线精品一卡乱码免费| 亚洲四虎永久在线播放| 亚洲第一网站免费视频| 亚洲第一成人在线| 好爽…又高潮了毛片免费看 | 国产午夜无码片免费| 亚洲啪啪综合AV一区| 中文字幕日本人妻久久久免费| 亚洲成在人天堂一区二区| 国产2021精品视频免费播放| 国产亚洲中文日本不卡二区| 日韩免费视频在线观看| 日本激情猛烈在线看免费观看| 国产成人亚洲影院在线观看| 你懂的免费在线观看网站| 亚洲人成影院午夜网站| 精品久久免费视频| 9久久免费国产精品特黄| 亚洲国产成人精品不卡青青草原| 最近的中文字幕大全免费8| 亚洲色成人网站WWW永久四虎| 免费成人午夜视频| 久久久久久久久久国产精品免费| 亚洲AV无码国产精品色| 亚洲AV无码乱码在线观看牲色 | 亚洲成人福利在线| 国产精品色午夜免费视频| 9久热精品免费观看视频| 亚洲同性男gay网站在线观看| 国产成人免费网站在线观看| 在线观看黄片免费入口不卡| 亚洲一级片在线播放| 老司机亚洲精品影视www|