yyg1107

这家伙很懒,什么都没有留下
posts(15) comments(20) trackbacks(0)
  • BlogJava
  • 联系
  • RSS 2.0 Feed 聚合
  • 管理

公告

联系方式:     



常用链接

  • 我的随笔
  • 我的评论
  • 我的参与
  • 最新评论

留言簿

  • 给我留言
  • 查看公开留言
  • 查看私人留言

随笔分类(11)

  •  axis(1)
  •  hibernate(1)
  •  java(2)
  •  js(2)
  •  other(5)

随笔档案(8)

  • 2008年6月 (1)
  • 2007年8月 (2)
  • 2007年4月 (3)
  • 2006年11月 (2)

文章分类(37)

  •  C++Builder(3)
  •  eclipse(2)
  •  html(4)
  •  java(8)
  •  jbpm(6)
  •  JS(4)
  •  other(4)
  •  question handly!(4)
  •  web容器(1)
  •  workflow(1)

文章档案(36)

  • 2007年4月 (2)
  • 2007年3月 (1)
  • 2007年1月 (2)
  • 2006年12月 (2)
  • 2006年11月 (3)
  • 2006年10月 (11)
  • 2006年9月 (2)
  • 2006年7月 (2)
  • 2006年6月 (1)
  • 2006年5月 (1)
  • 2006年4月 (6)
  • 2006年3月 (3)

新闻档案(1)

  • 2007年1月 (1)

收藏夹(16)

  •  好的BLOG(9)
  •  收藏----blog(7)

JavaBlog

  • amigoxie

matrix

搜索

  •  

积分与排名

  • 积分 - 32782
  • 排名 - 1316

最新评论

  • 1. re: JS实现文本框输入提供选择框的提示功能-1
  • werewrewrwer
  • --wrewrwe
  • 2. re: struts启动的一个错误!
  • 更名struts-config.xml, remove && add struts capability,重新生成一个config.xml。
  • --barryken
  • 3. p
  • 42
  • --2
  • 4. re: hql语句----随机查询取头10条记录[未登录]
  • 没意思,这不就是分页的那一部分代码嘛!
  • --啊啊
  • 5. re: 第一次做成功的AXIS例子[未登录]
  • 楼主:能不能留下你的联系方式!探讨一下相关问题...
  • --hehe

阅读排行榜

  • 1. JS实现文本框输入提供选择框的提示功能-1(7293)
  • 2. 第一次做成功的AXIS例子(2431)
  • 3. 命令行创建ODBC数据源(1545)
  • 4. struts启动的一个错误!(1265)
  • 5. hibernate调用存储过程例子(1007)

评论排行榜

  • 1. JS实现文本框输入提供选择框的提示功能-1(7)
  • 2. 第一次做成功的AXIS例子(5)
  • 3. struts启动的一个错误!(4)
  • 4. 转:如何在JAVA中使用日期 (0)
  • 5. 命令行创建ODBC数据源(0)

View Post

JDBC数据库连接池

ConnectionHandler.java
package com.ofis.DBConnection;

import java.lang.reflect.*;
import java.sql.*;
import org.apache.xalan.lib.sql.ConnectionPool;

/** *//**
 * 
 * 数据库连接的代理类
 *
 
*/

class ConnectionHandler implements InvocationHandler {
    Connection dbconn;
    ConnectionPool pool;
    
/** *//**
     * 构造函数
     * 
@param connPool
     
*/

    
public ConnectionHandler(ConnectionPool connPool) {
        
this.pool = connPool;
    }


    
/** *//**
     * 
     * 动态绑定Connection
     * 
@param conn Connection
     * 
@return Connection
     * 
     
*/

    
public Connection bind(Connection conn) {
        Class[] interfaces 
= conn.getClass().getInterfaces();
        
if (interfaces == null || interfaces.length == 0) {
            interfaces 
= new Class[1];
            interfaces[
0] = Connection.class;
        }

        Connection proxyConn 
= (Connection) Proxy.newProxyInstance(conn.
                getClass().getClassLoader(), interfaces, 
this);
        
return conn;

    }

    
/** *//**
     * 调用拦截函数
     
*/

    
public Object invoke(Object object, Method method, Object[] objectArray) throws
            Throwable 
{
        Object obj 
= null;
        
if ("close".equals(method.getName())) {
            pool.releaseConnection(dbconn);
        }
 else {
            obj 
= method.invoke(dbconn, objectArray);
        }

        
return obj;
    }


}

DBConnectionPool.java
package com.ofis.DBConnection;
import org.apache.xalan.lib.sql.ConnectionPool;
import java.sql.*;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Vector;
/** *//**
 * 数据库连接池控制
 *
 
*/

public class DBConnectionPool implements ConnectionPool{

    
private static Vector pool;
    
private final int POOL_MAX_SIZE=40;
    
static String strDriver = "net.sourceforge.jtds.jdbc.Driver";
    
static String strURL ="jdbc:jtds:sqlserver://localhost:1433/template";
    
public DBConnectionPool() {
    }


    
public boolean isEnabled() {
        
return false;
    }


    
public void setDriver(String string) {
        
this.strDriver = string;
    }


    
public void setURL(String string) {
        
this.strURL=string;
    }


    
public void freeUnused() {
    }


    
public boolean hasActiveConnections() {
        
return false;
    }


    
public void setPassword(String string) {
    }


    
public void setUser(String string) {
    }


    
public void setMinConnections(int _int) {
    }


    
public boolean testConnection() {
        
return false;
    }

    
/** *//**
     * 得到数据库连接操作
     
*/

    
public synchronized Connection getConnection() throws SQLException {
        
if(pool==null){
            pool
=new Vector();
        }

        Connection conn;
        
if(pool.isEmpty()){
            conn
=createConnection();
        }
else{
            
int last_idx=pool.size()-1;
            conn
=(Connection)pool.get(last_idx);
            pool.remove(pool.get(last_idx));
        }

        ConnectionHandler connHandler
=new ConnectionHandler(this);
        
return connHandler.bind(conn);
    }

    
/** *//**
     * 释放数据库连接操作
     
*/

    
public synchronized void releaseConnection(Connection connection) throws SQLException {
        
if(pool.size()>POOL_MAX_SIZE){
            
try {
                connection.close();
            }
 catch (SQLException ex) {
                ex.printStackTrace();
            }

        }
else{
            pool.add(connection);
        }

    }

    
/** *//**
     * 创建数据库连接操作
     * 
@return
     
*/

    
private static Connection createConnection(){
        Connection conn;
        
try {
            Class.forName(strDriver);
            conn 
= DriverManager.getConnection(strURL, "sa", "");
            
return conn;
        }
 catch (SQLException ex) {
            
return null;
        }
 catch (ClassNotFoundException ex) {
            
return null;
        }


    }

    
public void releaseConnectionOnError(Connection connection) throws
            SQLException 
{
    }


    
public void setPoolEnabled(boolean _boolean) {
    }


    
public void setProtocol(Properties properties) {
    }


}


DBHelper.java
package com.ofis.DBConnection;
import java.sql.*;

/** *//**
 * 数据库连接操作
 *
 
*/

public class DBHelper {
    
/** *//**
     * 得到数据库连接
     * 
@return
     
*/

    
public static Connection getConnection() {
        DBConnectionPool cp
=new DBConnectionPool();
        cp.setDriver(
"net.sourceforge.jtds.jdbc.Driver");
        cp.setURL(
"jdbc:jtds:sqlserver://localhost:1433/template");
        Connection conn 
= null;
        
try {
            conn 
= cp.getConnection();
        }
 catch (SQLException ex) {
        }

        
return conn;
  }

    
/** *//**
     * 释放数据库连接
     * 
@param conn 
     
*/

    
public static void releaseConnection(Connection conn){
        DBConnectionPool cp
=new DBConnectionPool();
        
try {
            cp.releaseConnection(conn);
        }
 catch (SQLException ex) {
        }

    }


}


DBHelperTest.java

package com.ofis.DBConnection;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.ofis.DBConnection.DBHelper;
import junit.framework.TestCase;

public class DBHelperTest extends TestCase {

    
/**//*
     * Test method for 'com.ofis.DBConnection.DBHelper.getConnection()'
     
*/

    
public void testGetConnection() {
        Connection h1 
= DBHelper.getConnection() ;
        Connection h2 
= DBHelper.getConnection() ;
        Connection h3 
= DBHelper.getConnection() ;
        Connection h4 
= DBHelper.getConnection() ;
        Connection h5 
= DBHelper.getConnection() ;
        
try{
            Statement stmt 
= h1.createStatement();
            ResultSet rs 
= stmt.executeQuery("select * from tbusers");
            
if(rs.next())
            
{
                System.out.println(rs.getString(
"user_truename"));
            }

        }

        
catch(SQLException ex){
            System.err.println(ex.getMessage());
        }
        
        
finally {
            
if (h1 != null) {
                DBHelper.releaseConnection(h1);
            }

            
if (h2 != null) {
                DBHelper.releaseConnection(h2);
            }

            
if (h3 != null) {
                DBHelper.releaseConnection(h3);
            }

            
if (h4 != null) {
                DBHelper.releaseConnection(h4);
            }

            
if (h5 != null) {
                DBHelper.releaseConnection(h5);
            }

            testCon();
            
        }


    }


    
public void testCon(){
        Connection h1 
= DBHelper.getConnection() ;
        Connection h2 
= DBHelper.getConnection() ;
        
try{
            Statement stmt 
= h1.createStatement();
            ResultSet rs 
= stmt.executeQuery("select * from tbusers");
            
if(rs.next())
            
{}
        }

        
catch(SQLException ex){
            System.err.println(ex.getMessage());
        }
        
        
finally {
            
if (h1 != null) {
                DBHelper.releaseConnection(h1);
            }

            
if (h2 != null) {
                DBHelper.releaseConnection(h2);
            }

//            if (h3 != null) {
//                DBHelper.releaseConnection(h3);
//            }
//            if (h4 != null) {
//                DBHelper.releaseConnection(h4);
//            }
//            if (h5 != null) {
//                DBHelper.releaseConnection(h5);
//            }
            
        }

    }


}

 

 

posted on 2006-09-30 13:42 young 阅读(307) 评论(0)  编辑  收藏 所属分类: java

新用户注册  刷新评论列表  

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问   管理
相关文章:
  • Hibernate Gossip: 樂觀鎖定(Optimistic locking)
  • 转:如何在JAVA中使用日期
  • Jakarta Commons:巧用类和组件1 (3)
  • Jakarta Commons:巧用类和组件1 (2)
  • Jakarta Commons:巧用类和组件1 (1)
  • Struts VS Turbine [转]
  • JDBC数据库连接池
 
 
Powered by:
BlogJava
Copyright © young