ibatis下加入c3p0连接池的方法

开发 后端
c3p0连接池是我们经常用到的,但是在ibatis下如何加入c3p0连接池呢?本文就针对ibatis下加入c3p0连接池提供了一个有效的解决方法。

我看论坛里有两个人写了在iBatis中加入c3p0连接池,我也就不跟着掺合了,做这个ibatis下加入c3p0连接池的方法的原因是没法用spring,又需要连接池,且对DBCP一直观感不加,没法,只好自己动手加入c3p0连接池了。

在iBatis中加入c3p0连接池没啥难技术含量,写个文章备份下。

首先打开iBatis的源码包,找到com.ibatis.sqlmap.engine.datasource包,在里面新建个类,C3p0DataSourceFactory,并实现DataSourceFactory接口,里面的代码是Java代码
private DataSource dataSource;  
 
public DataSource getDataSource() {  
    return dataSource;  
}  
 
public void initialize(Map map) {  
    C3p0Configuration c3p0 = new C3p0Configuration(map);  
    dataSource = c3p0.getDataSource();  

 private DataSource dataSource;
 
 public DataSource getDataSource() {
  return dataSource;
 }

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


在ibatis下加入c3p0连接池的下一步是到com.ibatis.common.jdbc包里,新建类C3p0Configuration,里面的代码是。。。太长了,原来在dbcp那部分里,还有单独加载配置文件的方法,我觉得如果连这个xml都读不了,那别的一样没法读。
Java代码
    private static final Probe PROBE = ProbeFactory.getProbe();  
    private DataSource dataSource;  
 
    /** 
     * Constructor to supply a map of properties 
     *  
     * @param properties 
     *            - the map of configuration properties 
     */ 
    public C3p0Configuration(Map properties) {  
        try {  
            dataSource = legacyC3p0Configuration(properties);  
        } catch (Exception e) {  
            throw new RuntimeException(  
                    "Error initializing C3p0DataSourceFactory.  Cause: " + e, e);  
        }  
    }  
 
    /** 
     * Getter for DataSource 
     *  
     * @return The DataSource 
     */ 
    public DataSource getDataSource() {  
        return dataSource;  
    }  
 
    private boolean notEmpty(String s) {  
        return s != null && s.length() > 0;  
    }  
 
    private boolean notSelfString(String s, String compareStr) {  
        return !s.equalsIgnoreCase(compareStr);  
    }  
 
    private DataSource legacyC3p0Configuration(Map map) {  
 
        ComboPooledDataSource combo = null;  
 
        try {  
            if (map.containsKey("JDBC.Driver")) {  
 
                combo = new ComboPooledDataSource();  
 
                String driverClass = (String) map.get("JDBC.Driver");  
                String jdbcUrl = (String) map.get("JDBC.ConnectionURL");  
                String user = (String) map.get("JDBC.Username");  
                String password = (String) map.get("JDBC.Password");  
 
                Class.forName(driverClass);  
                combo.setDriverClass(driverClass);  
                combo.setJdbcUrl(jdbcUrl);  
                combo.setUser(user);  
                combo.setPassword(password);  
 
                String acquireIncrement = (String) map  
                        .get("Pool.acquireIncrement");  
                if (notEmpty(acquireIncrement)  
                        && notSelfString(acquireIncrement, "acquireIncrement"))  
                    combo.setAcquireIncrement(Integer  
                            .parseInt(acquireIncrement));  
 
...................... 

 private static final Probe PROBE = ProbeFactory.getProbe();
 private DataSource dataSource;

 /**
  * Constructor to supply a map of properties
  *
  * @param properties
  *            - the map of configuration properties
  */
 public C3p0Configuration(Map properties) {
  try {
   dataSource = legacyC3p0Configuration(properties);
  } catch (Exception e) {
   throw new RuntimeException(
     "Error initializing C3p0DataSourceFactory.  Cause: " + e, e);
  }
 }

 /**
  * Getter for DataSource
  *
  * @return The DataSource
  */
 public DataSource getDataSource() {
  return dataSource;
 }

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

 private boolean notSelfString(String s, String compareStr) {
  return !s.equalsIgnoreCase(compareStr);
 }

 private DataSource legacyC3p0Configuration(Map map) {

  ComboPooledDataSource combo = null;

  try {
   if (map.containsKey("JDBC.Driver")) {

    combo = new ComboPooledDataSource();

    String driverClass = (String) map.get("JDBC.Driver");
    String jdbcUrl = (String) map.get("JDBC.ConnectionURL");
    String user = (String) map.get("JDBC.Username");
    String password = (String) map.get("JDBC.Password");

    Class.forName(driverClass);
    combo.setDriverClass(driverClass);
    combo.setJdbcUrl(jdbcUrl);
    combo.setUser(user);
    combo.setPassword(password);

    String acquireIncrement = (String) map
      .get("Pool.acquireIncrement");
    if (notEmpty(acquireIncrement)
      && notSelfString(acquireIncrement, "acquireIncrement"))
     combo.setAcquireIncrement(Integer
       .parseInt(acquireIncrement));

......................


***在com.ibatis.sqlmap.engine.config包里,找到SqlMapConfiguration,在里面找到,加上C3P0就可以实现在ibatis下加入c3p0连接池了
Java代码
// DATA SOURCE ALIASES  
typeHandlerFactory.putTypeAlias("SIMPLE", SimpleDataSourceFactory.class.getName());  
typeHandlerFactory.putTypeAlias("DBCP", DbcpDataSourceFactory.class.getName());  
typeHandlerFactory.putTypeAlias("C3P0", C3p0DataSourceFactory.class.getName());  
typeHandlerFactory.putTypeAlias("JNDI", JndiDataSourceFactory.class.getName());

通过这种手段,可以在ibatis下加入c3p0连接池,但是由于代码太长,在这里就不一一列出了,本文只是提出了一个在ibatis下加入c3p0连接池的思路,具体关于ibatis下加入c3p0连接池代码还需要朋友们自己斟酌。

【编辑推荐】

  1. ibatis也能用proxool连接池
  2. 实例说明ibatis动态查询
  3. 避免ibatisN+1查询的方法
  4. ibatis级联解决登录系统问题
  5. ibatis标签详解
责任编辑:桑丘 来源: 奈何桥收费站。。。blog
相关推荐

2009-09-22 17:53:09

Hibernate C

2009-09-22 14:44:18

Hibernate.c

2015-10-29 16:59:47

数据库

2018-07-20 14:50:43

Java数据库连接池

2020-03-04 13:55:28

c3p0数据库连接池

2009-06-17 09:34:31

Hibernate3Hibernate2连接池

2021-05-12 07:32:58

数据库C3P0项目

2009-07-15 11:00:48

proxool连接池

2015-04-27 09:50:45

Java Hibern连接池详解

2010-07-08 10:06:43

Tomcat+SQL

2010-07-20 14:50:42

Tomcat+SQL

2009-09-22 14:52:55

Hibernate p

2010-11-08 16:46:57

2013-06-17 10:25:16

连接池Java

2011-05-13 09:34:51

TomcatMysql连接池

2023-09-01 07:31:24

2019-12-30 15:30:13

连接池请求PHP

2022-07-19 13:51:47

数据库Hikari连接池

2010-10-22 09:32:01

SQL Server2

2009-09-23 14:00:07

Hibernate连接
点赞
收藏

51CTO技术栈公众号