在网上找的教程都是针对web项目的,我现在需要在一个java项目中使用proxool连接池,求教。

解决方案 »

  1.   

    1. If you adapting an existing application then the most transparent way to switch to Proxool is to simply change the URL you use to connect to the database. For example:
     1.Connection connection = null;
     2.try {
     3.  Class.forName("org.hsqldb.jdbcDriver");
     4.  try {
     5.    connection = DriverManager.getConnection("jdbc:hsqldb:test");
     6.  } catch (SQLException e) {
     7.    LOG.error("Problem getting connection", e);
     8.  }
     9.  
    10.  if (connection != null) {
    11.    LOG.info("Got connection :)");
    12.  } else {
    13.    LOG.error("Didn't get connection, which probably means that no Driver accepted the URL");
    14.  }
    15.  
    16.} catch (ClassNotFoundException e) {
    17.  LOG.error("Couldn't find driver", e);
    18.} finally {
    19.  try {
    20.    // Check to see we actually got a connection before we
    21.    // attempt to close it.
    22.    if (connection != null) {
    23.      connection.close();
    24.    }
    25.  } catch (SQLException e) {
    26.    LOG.error("Problem closing connection", e);
    27.  }
    28.}To start using Proxool, simply edit lines 3 and 5 (ed in blue):
     1.Connection connection = null;
     2.try {
     3.  Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
     4.  try {
     5.    connection = DriverManager.getConnection("proxool.example:org.hsqldb.jdbcDriver:jdbc:hsqldb:test");
     6.  } catch (SQLException e) {
     7.    LOG.error("Problem getting connection", e);
     8.  }
     9.  
    10.  if (connection != null) {
    11.    LOG.info("Got connection :)");
    12.  } else {
    13.    LOG.error("Didn't get connection, which probably means that no Driver accepted the URL");
    14.  }
    15.  
    16.} catch (ClassNotFoundException e) {
    17.  LOG.error("Couldn't find driver", e);
    18.} finally {
    19.  try {
    20.    // Check to see we actually got a connection before we
    21.    // attempt to close it.
    22.    if (connection != null) {
    23.      connection.close();
    24.    }
    25.  } catch (SQLException e) {
    26.    LOG.error("Problem closing connection", e);
    27.  }
    28.} 
      

  2.   

    英文不好的话 再帮你翻译下
    我的新书刚上架,欢迎订购:
    《搜索引擎零距离—基于Ruby+Java搜索引擎原理与实现》 清华出版社。
    http://www.huachu.com.cn/itbook/itbookinfo.asp?lbbh=10105450
      

  3.   

    PS:在网上找的教程都是针对web项目的,我现在需要在一个java项目中使用proxool连接池,求教。web的SSH框架都有数据库连接配置java项目中使用连接池我昨天回答过相同问题帖出来吧 需要fscontext.jar和providerutil.jar,没有去Google下载package com.isoftstone.common;import java.sql.Connection;
    import java.sql.DriverManager;
    import java.util.Hashtable;import javax.naming.Context;
    import javax.naming.InitialContext;import oracle.jdbc.pool.OracleConnectionPoolDataSource;public class CreatePool {
    public static Connection getCon(){
    Connection con = null;
    try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "tiger");
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return con;
    } public static void main(String[] args) {
    Connection conn =getCon();
    if(null!=conn)
    {
    System.out.print("2--------");
    }
    createPool();

    Connection connection = getPool("jdbc/pool");
    if(null!=connection){
    System.out.print("2--------");
    }
    }

    public static void createPool(){
    Hashtable ht = new Hashtable();
    ht.put(Context.INITIAL_CONTEXT_FACTORY, 
    "com.sun.jndi.fscontext.RefFSContextFactory");
    try {
    OracleConnectionPoolDataSource ods =  new OracleConnectionPoolDataSource();
    Context context = new InitialContext(ht);

    ods.setPassword("tiger");
    ods.setURL("jdbc:oracle:thin:@127.0.0.1:1521:orcl");
    ods.setUser("scott");
    //ods.setLoginTimeout(arg0)
    ods.setDriverType("oracle.jdbc.driver.OracleDriver");
    context.unbind("jdbc/pool");
    context.bind("jdbc/pool", ods);

    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    public static Connection getPool(String name){
    Connection con = null;
    Hashtable ht = new Hashtable();
    ht.put(Context.INITIAL_CONTEXT_FACTORY, 
    "com.sun.jndi.fscontext.RefFSContextFactory");
    try {
    Context context = new InitialContext(ht);
    OracleConnectionPoolDataSource ods = (OracleConnectionPoolDataSource)context.lookup(name);
    con = ods.getConnection();

    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return con;
    }

    }
      

  4.   

    我这个是mysql,而且你的代码里没出现proxool啊