ITeye博客

iBatis 配置自定义C3P0连接池

LiYunpeng · 2010-04-12 10:36 · iBatis · 企业架构 · Apache · SQL · .net · XML · iBATIS · Markdown

[转][url]http://www.blogjava.net/usherlight/archive/2010/02/01/311493.html[/url] 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类来进行数据源属性的设置,而是使用反射机制直接调用数据源的方法来设置参数。 这就要求配置文件中的参数名称必须与数据源类中的方法名匹配. [code="java"] public class C3p0DataSourceFactory implements DataSourceFactory { private DataSource dataSource; public C3p0DataSourceFactory() { dataSource = new C3p0DataSource(); } public DataSource getDataSource() { return dataSource; } public void setProperties(Properties properties) { Properties driverProperties = new Properties(); MetaObject metaDataSource = MetaObject.forObject(dataSource); for (Object key : properties.keySet()) { String propertyName = (String) key; if (propertyName.startsWith(DRIVER_PROPERTY_PREFIX)) { String value = properties.getProperty(propertyName); driverProperties.setProperty(propertyName .substring(DRIVER_PROPERTY_PREFIX_LENGTH), value); } else if (metaDataSource.hasSetter(propertyName)) { String value = (String) properties.get(propertyName); Object convertedValue = convertValue(metaDataSource, propertyName, value); metaDataSource.setValue(propertyName, convertedValue); } else { throw new DataSourceException("Unkown DataSource property: " + propertyName); } } if (driverProperties.size() > 0) { metaDataSource.setValue("driverProperties", driverProperties); } } @SuppressWarnings("unchecked") private Object convertValue(MetaObject metaDataSource, String propertyName, String value) { Object convertedValue = value; Class targetType = metaDataSource.getSetterType(propertyName); if (targetType == Integer.class || targetType == int.class) { convertedValue = Integer.valueOf(value); } else if (targetType == Long.class || targetType == long.class) { convertedValue = Long.valueOf(value); } else if (targetType == Boolean.class || targetType == boolean.class) { convertedValue = Boolean.valueOf(value); } return convertedValue; } private static final String DRIVER_PROPERTY_PREFIX = "driver."; private static final int DRIVER_PROPERTY_PREFIX_LENGTH = DRIVER_PROPERTY_PREFIX.length(); } [/code] 2. 数据源类,其中的一堆setter就是用于设置属性的 [code="java"] public class C3p0DataSource implements DataSource { private ComboPooledDataSource dataSource; public C3p0DataSource() { this.dataSource = new ComboPooledDataSource(); } public Connection getConnection() throws SQLException { return dataSource.getConnection(); } public Connection getConnection(String username, String password) throws SQLException { return dataSource.getConnection(username, password); } public PrintWriter getLogWriter() throws SQLException { return dataSource.getLogWriter(); } public int getLoginTimeout() throws SQLException { return dataSource.getLoginTimeout(); } public void setLogWriter(PrintWriter out) throws SQLException { dataSource.setLogWriter(out); } public void setLoginTimeout(int seconds) throws SQLException { dataSource.setLoginTimeout(seconds); } public synchronized void setDriver(String driver) { try { dataSource.setDriverClass(driver); } catch (Exception e) { } } public void setUrl(String url) { dataSource.setJdbcUrl(url); } public void setUsername(String username) { dataSource.setUser(username); } public void setPassword(String password) { dataSource.setPassword(password); } public void setInitialPoolSize(int initialPoolSize) { dataSource.setInitialPoolSize(initialPoolSize); } public void setMaxPoolSize(int maxPoolSize) { dataSource.setMaxPoolSize(maxPoolSize); } public void setMinPoolSize(int minPoolSize) { dataSource.setMinPoolSize(minPoolSize); } public void setPreferredTestQuery(String preferredTestQuery) { dataSource.setPreferredTestQuery(preferredTestQuery); } public void setPoolPingQuery(String poolPingQuery) { dataSource.setPreferredTestQuery(poolPingQuery); } } [/code] 3. 在配置文件Configuration.xml中,可以先定义数据源的别称,然后就象POOLED和UNPOOLED一样使用别称来引用数据源。 [code="xml"] ... ... [/code]

评论(1)

  1. #1 楼 · Purking · 2010-11-07 01:18

    还可以直接继承 UnpooledDataSourceFactory

    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>
    
    
    1. 将 c3p0.properties 文件放入 classpath 中

    这样省去很多代码,还可以完全设置 c3p0 的各个配置属性,简单很多,试一下? :)

评论功能维护中,当前仅展示历史评论。