`
Sev7en_jun
  • 浏览: 1225387 次
  • 性别: Icon_minigender_1
  • 来自: 广州
博客专栏
84184fc0-d0b6-3f7f-a3f0-4202acb3caf5
Apache CXF使用s...
浏览量:111303
社区版块
存档分类
最新评论

ibatis-2.3.0.677增加对c3p0连接池的支持

 
阅读更多

 

1,在com.ibatis.sqlmap.engine.datasource包新增类C3P0DataSourceFactory:

 

package com.ibatis.sqlmap.engine.datasource;

import com.ibatis.common.jdbc.C3P0Configuration;
import java.util.Map;
import javax.sql.DataSource;

public class C3P0DataSourceFactory
  implements DataSourceFactory
{
  private DataSource dataSource;

  public void initialize(Map map)
  {
    C3P0Configuration c3p0 = new C3P0Configuration(map);
    this.dataSource = c3p0.getDataSource();
  }

  public DataSource getDataSource() {
    return this.dataSource;
  }
}

 

 

2,在com.ibatis.common.jdbc包新增类C3P0Configuration ,主要作用是加载c3p0的配置文件信息: 

 

package com.ibatis.common.jdbc;

import com.ibatis.common.beans.Probe;
import com.ibatis.common.beans.ProbeFactory;
import com.ibatis.common.exception.NestedRuntimeException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;

public class C3P0Configuration
{
  private static final Probe PROBE = ProbeFactory.getProbe();
  private static final String ADD_DRIVER_PROPS_PREFIX = "Driver.";
  private static final int ADD_DRIVER_PROPS_PREFIX_LENGTH = "Driver.".length();
  private DataSource dataSource;

  public C3P0Configuration(Map properties)
  {
    try
    {
      this.dataSource = legacyC3P0Configuration(properties);
      if (this.dataSource == null)
        this.dataSource = newDbcpConfiguration(properties);
    } catch (Exception e) {
      throw new NestedRuntimeException("Error initializing C3P0DataSourceFactory.  Cause: " + e, e);
    }
  }

  public DataSource getDataSource() {
    return this.dataSource;
  }

  private BasicDataSource newDbcpConfiguration(Map map) {
    BasicDataSource basicDataSource = new BasicDataSource();
    Iterator props = map.keySet().iterator();
    while (props.hasNext()) {
      String propertyName = (String)props.next();
      if (propertyName.startsWith("Driver.")) {
        String value = (String)map.get(propertyName);
        basicDataSource.addConnectionProperty(propertyName.substring(ADD_DRIVER_PROPS_PREFIX_LENGTH), value);
      } else if (PROBE.hasWritableProperty(basicDataSource, propertyName)) {
        String value = (String)map.get(propertyName);
        Object convertedValue = convertValue(basicDataSource, propertyName, value);
        PROBE.setObject(basicDataSource, propertyName, convertedValue);
      }
    }
    return basicDataSource;
  }

  private Object convertValue(Object object, String propertyName, String value) {
    Object convertedValue = value;
    Class targetType = PROBE.getPropertyTypeForSetter(object, propertyName);
    if ((targetType == Integer.class) || (targetType == Integer.TYPE))
      convertedValue = Integer.valueOf(value);
    else if ((targetType == Long.class) || (targetType == Long.TYPE))
      convertedValue = Long.valueOf(value);
    else if ((targetType == Boolean.class) || (targetType == Boolean.TYPE)) {
      convertedValue = Boolean.valueOf(value);
    }
    return convertedValue;
  }

  private ComboPooledDataSource legacyC3P0Configuration(Map map) throws Exception {
    ComboPooledDataSource basicDataSource = null;
    if (map.containsKey("driver")) {
      basicDataSource = new ComboPooledDataSource();
      String driver = (String)map.get("driver");
      String url = (String)map.get("url");
      String username = (String)map.get("username");
      String password = (String)map.get("password");

      String initialPoolSize = (String)map.get("initialPoolSize");
      String maxPoolSize = (String)map.get("maxPoolSize");
      String minPoolSize = (String)map.get("minPoolSize");
      String acquireIncrement = (String)map.get("acquireIncrement");
      String maxIdleTime = (String)map.get("maxIdleTime");
      String maxIdleTimeExcessConnections = (String)map.get("maxIdleTimeExcessConnections");
      String maxConnectionAge = (String)map.get("maxConnectionAge");
      String maxStatements = (String)map.get("maxStatements");
      String maxStatementsPerConnection = (String)map.get("maxStatementsPerConnection");
      String numHelperThreads = (String)map.get("numHelperThreads");
      String automaticTestTable = (String)map.get("automaticTestTable");
      String preferredTestQuery = (String)map.get("preferredTestQuery");
      String checkoutTimeout = (String)map.get("checkoutTimeout");
      String idleConnectionTestPeriod = (String)map.get("idleConnectionTestPeriod");
      String acquireRetryDelay = (String)map.get("acquireRetryDelay");
      String acquireRetryAttempts = (String)map.get("acquireRetryAttempts");
      String testConnectionOnCheckin = (String)map.get("testConnectionOnCheckin");

      basicDataSource.setDriverClass(driver);
      basicDataSource.setJdbcUrl(url);
      basicDataSource.setUser(username);
      basicDataSource.setPassword(password);

      if (notEmpty(automaticTestTable)) {
        basicDataSource.setAutomaticTestTable(automaticTestTable);
      }
      if (notEmpty(acquireRetryDelay)) {
        basicDataSource.setAcquireRetryDelay(Integer.valueOf(acquireRetryDelay).intValue());
      }
      if (notEmpty(testConnectionOnCheckin)) {
        basicDataSource.setTestConnectionOnCheckin(Boolean.valueOf(testConnectionOnCheckin).booleanValue());
      }
      if (notEmpty(acquireRetryAttempts)) {
        basicDataSource.setAcquireRetryAttempts(Integer.valueOf(acquireRetryAttempts).intValue());
      }
      if (notEmpty(preferredTestQuery)) {
        basicDataSource.setPreferredTestQuery(preferredTestQuery);
      }
      if (notEmpty(checkoutTimeout)) {
        basicDataSource.setCheckoutTimeout(Integer.valueOf(checkoutTimeout).intValue());
      }
      if (notEmpty(checkoutTimeout)) {
        basicDataSource.setCheckoutTimeout(Integer.valueOf(checkoutTimeout).intValue());
      }
      if (notEmpty(idleConnectionTestPeriod)) {
        basicDataSource.setIdleConnectionTestPeriod(Integer.valueOf(idleConnectionTestPeriod).intValue());
      }
      if (notEmpty(initialPoolSize)) {
        basicDataSource.setInitialPoolSize(Integer.parseInt(initialPoolSize));
      }
      if (notEmpty(maxPoolSize)) {
        basicDataSource.setMaxPoolSize(Integer.parseInt(maxPoolSize));
      }
      if (notEmpty(minPoolSize)) {
        basicDataSource.setMinPoolSize(Integer.parseInt(minPoolSize));
      }
      if (notEmpty(acquireIncrement)) {
        basicDataSource.setAcquireIncrement(Integer.parseInt(acquireIncrement));
      }
      if (notEmpty(maxIdleTime)) {
        basicDataSource.setMaxIdleTime(Integer.parseInt(maxIdleTime));
      }
      if (notEmpty(maxIdleTimeExcessConnections)) {
        basicDataSource.setMaxIdleTimeExcessConnections(Integer.parseInt(maxIdleTimeExcessConnections));
      }
      if (notEmpty(maxConnectionAge)) {
        basicDataSource.setMaxConnectionAge(Integer.parseInt(maxConnectionAge));
      }
      if (notEmpty(maxStatements)) {
        basicDataSource.setMaxStatements(Integer.parseInt(maxStatements));
      }
      if (notEmpty(maxStatementsPerConnection)) {
        basicDataSource.setMaxStatementsPerConnection(Integer.parseInt(maxStatementsPerConnection));
      }
      if (notEmpty(numHelperThreads)) {
        basicDataSource.setNumHelperThreads(Integer.parseInt(numHelperThreads));
      }
    }
    return basicDataSource;
  }

  private boolean notEmpty(String s) {
    return (s != null) && (s.length() > 0);
  }
}

 

 

 3,在com.ibatis.sqlmap.engine.builder.xml下SqlMapConfigParser类的registerDefaultTypeAliases方法中添加如下代码:

this.vars.typeHandlerFactory.putTypeAlias("C3P0", C3P0DataSourceFactory.class.getName());

 

 

 4,在com.ibatis.common.exception下新增异常处理类,如果没有此包,可以新建,代码见附件Exception.rar(NestedException.class , NestedRuntimeException.class)

 

5, ibatis配置文件如下,注意property中的name必须和下面一致:

<transactionManager type="JDBC">
  <dataSource type="C3P0">
   <property name="driver" value="${driver}" />
   <property name="url" value="${url}" />
   <property name="username" value="${username}" />
   <property name="password" value="${password}" />
   <property name="initialPoolSize" value="10" />
   <property name="maxPoolSize" value="100" />
   <property name="minPoolSize" value="10" />
   <property name="acquireIncrement" value="5" />
   <property name="maxIdleTime" value="3600" />
   <property name="maxIdleTimeExcessConnections" value="1200" />
   <property name="maxConnectionAge" value="27000" />
   <property name="maxStatements" value="200" />
   <property name="maxStatementsPerConnection" value="50" />
   <property name="numHelperThreads" value="10" />
   <property name="idleConnectionTestPeriod"  value="5" />
   <property name="checkoutTimeout" value="30000"/>
   <property name="preferredTestQuery" value="select 1"/>
   <property name="acquireRetryDelay" value="1000"/>
   <property name="acquireRetryAttempts" value="1"/>
   <property name="testConnectionOnCheckin" value="true"/>
   <property name="automaticTestTable" value="c3p0Test"/>
  </dataSource>
 </transactionManager>

 配置中只支持如上属性配置,如果需要加额外属性请在第二步的C3P0Configuration类的legacyC3P0Configuration方法中增加

附件ibatis-2.3.0.677.jar包是已扩展,无需修改

增加c3p0支持包:c3p0-0.9.1.2.jar

 

 

 

PS: 以上只限ibatis-2.3.0.677版本,其他版本按照此方式未必行得通

以下亲测可用

 

PS: 以下只限ibatis-2.3.0.677版本

分享到:
评论
1 楼 zhh309 2014-11-12  

学习了。

相关推荐

    ibatis-2.3.4.726.jar,ibatis-2.3.0.677.jar,ibatis-2.3.3.720.jar下载

    这里提到的是iBATIS的三个不同版本的jar包:ibatis-2.3.4.726.jar、ibatis-2.3.0.677.jar以及ibatis-2.3.3.720.jar。 首先,让我们深入了解iBATIS的核心概念和功能: 1. **SQL Map配置**:iBATIS的核心是SQL Map...

    ibatis-2.3.3.720.jar

    ibatis-2.3.3.720.jar

    整合后的ibatis-2.3.0.677.jar

    使用Ibatis时,首先需要在项目中引入ibatis-2.3.0.677.jar,并配置相应的配置文件(如`sqlMapConfig.xml`),然后创建Mapper接口和对应的XML映射文件,最后在业务逻辑中通过SqlSession执行SQL语句。这个版本的Ibatis...

    ibatis-2.3.0.677-sources.jar

    ibatis-2.3.0.677-sources.jar 值得学习的源码资源,不容错过。

    ibatis-2.3.0.677.jar

    标题 "ibatis-2.3.0.677.jar" 指向的是一个特定版本的 iBATIS 库,即版本号为 2.3.0.677 的 JAR 文件。iBATIS 是一个开源的 Java 框架,主要用于简化数据库与应用程序之间的交互。它在早期广泛应用于企业级应用开发...

    ibatis2.3.4.8.jar 和 ibatis-2.3.4.726.jar两个版本的下载

    在本主题中,我们将深入探讨Ibatis的两个特定版本:ibatis2.3.4.8.jar和ibatis-2.3.4.726.jar。 Ibatis的核心概念包括映射器(Mapper)、SQL映射文件和SqlSession。映射器是Ibatis的主要组件,它定义了数据库操作与...

    ibatis-2.3.3.7.jar

    ibatis-2.3.3.7.jar

    ibatis-sqlmap-2.3.4.726-sources.jar.zip_birth84v_cutting1v2_ibat

    《深入解析iBatis-SQLMap 2.3.4.726源码》 在Java开发领域,iBatis作为一个优秀的持久层框架,深受广大开发者喜爱。它将SQL语句与Java代码分离,提高了代码的可读性和可维护性。本篇将围绕iBatis-SQLMap 2.3.4.726...

    ibatis-2.3.4.726最新API chm格式

    ibatis-2.3.4.726最新API chm格式 非常好用

    最稳定Ibatis包 ibatis-2.3.0.677.rar

    在实际使用Ibatis时,首先需要在项目中引入ibatis-2.3.0.677.jar包,然后配置SqlMapConfig.xml文件,这是整个Ibatis的全局配置文件,用于设置数据源、事务管理器以及其他核心组件。接下来,开发者可以创建具体的...

    ibatis-2.3.0.677

    ibatis-2.3.0.677 ibatis-2.3.0.677 ibatis-2.3.0.677 ibatis-2.3.0.677 ibatis-2.3.0.677 ibatis-2.3.0.677 ibatis-2.3.0.677

    com.springsource.com.ibatis-2.3.0.677.jar

    ibatis的jar包com.springsource.com.ibatis-2.3.0.677.jar

    ibatis-2.3.0.677.jar.zip

    该版本的iBatis jar文件,即`ibatis-2.3.0.677.jar`,包含了框架的所有核心类和库。其中,包含了SqlMapConfig.xml配置文件,这是iBatis系统的核心,用于定义数据源、事务管理器、SqlMapClient等重要组件。此外,还...

    iBATIS-DAO-2.3.4.726.rar_com.ibatis.dao_iBATIS dao 2_iBatis DAO_

    在这个2.3.4.726版本的源码中,我们可以深入理解iBATIS DAO的工作原理,并通过添加注释来帮助我们更好地掌握其实现细节。 首先,iBATIS DAO的核心概念是SQL Maps,它们定义了数据库操作的SQL语句,并将其映射到Java...

    ibatis-2.3.4.732.jar

    ibatis-2.3.4.732.jar

    ibatis-2.3.2.715

    《深入解析iBatis 2.3.2.715源代码》 iBatis,作为一款轻量级的Java持久层框架,以其灵活、高效的特点,在许多项目中得到了广泛应用。本文将针对iBatis的2.3.2.715版本的源代码进行深入剖析,旨在帮助开发者更好地...

    ibatis-2.3.2.715.jar

    3. 数据源配置:`ibatis-2.3.2.715.jar`包含对数据源的支持,允许开发者配置连接池,提高数据库连接的复用,降低系统资源消耗。例如,它可以与C3P0、DBCP等连接池配合使用。 4. 参数映射与结果映射:Ibatis提供了...

    ibatis-2.3.4.726官方最新开发包

    总的来说,"ibatis-2.3.4.726官方最新开发包"是Java开发者进行数据库操作的强大工具,它简化了数据库操作,提升了开发效率,并且提供了丰富的文档支持,使得学习和使用变得更加容易。如果你正准备使用或升级Ibatis,...

    iBATIS-2.3.0.677-Api

    iBATIS-2.3.0.677-Api文档

Global site tag (gtag.js) - Google Analytics