您的位置:首页 > 其它

关于Proxool使用介绍及在使用中发现的问题

2006-08-30 10:20 495 查看
Proxool.xml文件配置如下:
<?xml version="1.0" encoding="ISO-8859-1" ?> 

- <!-- 
 the proxool configuration can be embedded within your own application's.
Anything outside the "proxool" tag is ignored. 
  --> 
- <something-else-entirely>
- <proxool>
  <alias>victa</alias> 
  <driver-url>jdbc:mysql://localhost:3306/victa</driver-url> 
  <driver-class>com.mysql.jdbc.Driver</driver-class> 
- <driver-properties>
  <property name="user" value="user" /> 
  <property name="password" value="password" /> 
  </driver-properties>
  <maximum-connection-count>30</maximum-connection-count> 
  <house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql> 
  </proxool>
  </something-else-entirely>


这个是没有什么好说的, 在以前的使用中都是直接获取Connection,好像是没有什么问题,后来为了方便,单独提取出来,没有想到出现了问题。代码如下:

----------------SqlTool.java----------
package cn.victa;

import java.sql.*;
import java.util.ArrayList;
import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;
import org.logicalcobwebs.proxool.ProxoolFacade; 

public final class SqlTool{
 /*创建这个私有静态变量是为了调用它的私有构造函数,由于在运行时静态变量只初始化一次,
  *因而节约了时间,保证在调用getConnection()方法之前已经注册了proxool.victa这个驱动程序*/
 //private static SqlTool instance=new SqlTool();
 //私有化构造函数,防止生成实例
 private SqlTool(){
  //init();
 }
 
 private static void init(){
  try {
   JAXPConfigurator.configure("proxool.xml",false);
     }
     catch (Exception ex) {
      //ex.printStackTrace();
     }
 }
 /**
     * 获得Connection对象
     */
 public static Connection getConnection(){
  Connection conn=null;
  try{
   init();
   conn=DriverManager.getConnection("proxool.victa");
  }
  catch(Exception e){
   conn=null;
  }
  return conn;
 }
 
 /**
     * 关闭Connection对象
     */
 public static void closeConnection(Connection conn){
  try{
   if((conn!=null)&&(!conn.isClosed())){
    conn.close();
   }
  }
  catch(SQLException e){
   System.out.println("关闭数据库连接发生异常!");
  }
 }
 
 /**
     * 关闭PreparedStatement对象
     */
 public static void closePreparedStatement(PreparedStatement pstmt){
  try{
   if(pstmt!=null){
    pstmt.close();
   }
  }
  catch(SQLException e){
   System.out.println("关闭PreparedStatement发生异常!");
  }
 }
 
 /**
     * 关闭ResultSet对象
     */
 public static void closeResultSet(ResultSet rs){
  try {
   if(rs!=null){
    rs.close();
   }
     }
     catch (Exception ex) {
      System.out.println("关闭ResultSet发生异常!");
     }
 }
 
 
 /**
     * 测试函数
     */
 public static void main(String[] args)throws Exception{
  java.sql.Connection conn=SqlTool.getConnection();
  System.out.println (conn==null?"NULL":"NOT NULL");
 }
}


---------------------------------------UserTool.java--------------------------------------------

package cn.victa;

import java.sql.*;
import java.util.ArrayList;
import java.util.Random; 

public class UserTool{
 private static UserTool instance;
 
 //禁止构造新的实例
 private UserTool(){
 }
 
 public static UserTool getInstance(){
  if(instance==null){
   instance=new UserTool();
  }
  return instance;
 }
 
 public int addUser(String username,String password,String headImg,String sex,String email,String pwdQuestion,String pwdAnswer){
  PreparedStatement pstmt=null;
  Connection conn=null;
  ResultSet rs=null;
  conn=SqlTool.getConnection();
  int res=0;
  if(conn==null){
   return res;
  }
  else{
   try {
    pstmt=conn.prepareStatement("insert into userinfo(username,password,face,sex,email,pwdQuestion,pwdAnswer,regDate,lastLogin) values(?,?,?,?,?,?,?,?,?)");
    Timestamp current=new Timestamp(System.currentTimeMillis());
    pstmt.setString(1,username);
    pstmt.setString(2,MD5Encode.encode(password));
    pstmt.setString(3,headImg);
    pstmt.setString(4,sex);
    pstmt.setString(5,email);
    pstmt.setString(6,pwdQuestion);
    pstmt.setString(7,pwdAnswer);
    pstmt.setTimestamp(8,current);
    pstmt.setTimestamp(9,current);
    
    res=pstmt.executeUpdate();
      }
      catch (Exception ex) {
       System.out.println(ex.toString());
       res=0;
      }
      finally {
       SqlTool.closePreparedStatement(pstmt);
       SqlTool.closeConnection(conn);
       return res;
      }
  }
 }
 
 /**
  *删除一个用户(其实并没有真正删除,只是将isActive设置为0)。
  *任何能正常登录的用户的isActive为1。
  *这样用户就不能登录了,但有关该用户以前的数据都保存了。
  *@param uid 要删除的用户的编号
  *@return 是否成功删除
  */
 public boolean deleteUser(int uid){
  PreparedStatement pstmt=null;
  Connection conn=null;
  ResultSet rs=null;
  boolean delete=false;
  conn=SqlTool.getConnection();
  if(conn==null){
   return delete;
  }
  else{
   try {
    pstmt=conn.prepareStatement("update userinfo set isActive=0 where uid=?");
    pstmt.setInt(1,uid);
    pstmt.executeUpdate();
    delete=true;
      }
      catch (Exception ex) {
       delete=false;
      }
      finally {
       SqlTool.closePreparedStatement(pstmt);
       SqlTool.closeConnection(conn);
       return delete;
      }
  }
 }
 
 public UserInfo getUserInfo(String username){
  PreparedStatement pstmt=null;
  Connection conn=null;
  ResultSet rs=null;
  conn=SqlTool.getConnection();
  if(conn==null){
   return null;
  }
  else{
   UserInfo user=new UserInfo();
   try {
    pstmt=conn.prepareStatement("select * from userinfo where username=?");
    pstmt.setString(1,username);
    rs=pstmt.executeQuery();
    if(rs.next()){
     user.setUserId(rs.getInt("uid"));
     user.setUsername(rs.getString("username"));
     user.setPassword(rs.getString("password"));
     user.setFace(rs.getString("face"));
     user.setSex(rs.getString("sex"));
     user.setEmail(rs.getString("email"));
     user.setPwdQuestion(rs.getString("pwdQuestion"));
     user.setPwdAnswer(rs.getString("pwdAnswer"));
     user.setRegDate(rs.getTimestamp("regDate"));
     user.setLoginCount(rs.getInt("loginCount"));
     user.setLastLogin(rs.getTimestamp("lastLogin"));
     user.setIsActive(rs.getByte("isActive"));
     user.setPrimeCount(rs.getInt("primeCount"));
     user.setArticleCount(rs.getInt("articleCount"));
    }
    else{
     user=null;
    }
      }
      catch (Exception ex) {
       ex.printStackTrace();
       user=null;
      }
      finally {
       SqlTool.closeResultSet(rs);
       SqlTool.closePreparedStatement(pstmt);
       SqlTool.closeConnection(conn);
       return user;
      }
  }
 }
 
 /**
  *根据名字中的一部分来查找所有符合条件的用户
  *@param partName-用户名中的部分
  *@return 以ArrayList为容器的UserInfo类
  */
 public ArrayList getUserInfoList(String partName){
  PreparedStatement pstmt=null;
  Connection conn=null;
  ResultSet rs=null;
  ArrayList userList=null;
  conn=SqlTool.getConnection();
  if(conn==null){
   return null;
  }
  else{
   try {
    userList=new ArrayList();
    pstmt=conn.prepareStatement("select * from userinfo where username like '%"+partName+"%'");
    rs=pstmt.executeQuery(); 
    while(rs.next()){
     UserInfo user=new UserInfo();
     user.setUserId(rs.getInt("uid"));
     user.setUsername(rs.getString("username"));
     user.setPassword(rs.getString("password"));
     user.setFace(rs.getString("face"));
     user.setSex(rs.getString("sex"));
     user.setEmail(rs.getString("email"));
     user.setPwdQuestion(rs.getString("pwdQuestion"));
     user.setPwdAnswer(rs.getString("pwdAnswer"));
     user.setRegDate(rs.getTimestamp("regDate"));
     user.setLoginCount(rs.getInt("loginCount"));
     user.setLastLogin(rs.getTimestamp("lastLogin"));
     user.setIsActive(rs.getByte("isActive"));
     userList.add(user);
    }
      }
      catch (Exception ex) {
       userList=null;
      }
      finally {
       SqlTool.closeResultSet(rs);
       SqlTool.closePreparedStatement(pstmt);
       SqlTool.closeConnection(conn);
       return userList;
      }
  }
 }
 
 public UserInfo getUserInfo(int id){
  PreparedStatement pstmt=null;
  Connection conn=null;
  ResultSet rs=null;
  conn=SqlTool.getConnection();
  if(conn==null){
   return null;
  }
  else{
   UserInfo user=new UserInfo();
   try {
    pstmt=conn.prepareStatement("select * from userinfo where uid=?");
    pstmt.setInt(1,id);
    rs=pstmt.executeQuery();
    if(rs.next()){
     user.setUserId(rs.getInt("uid"));
     user.setUsername(rs.getString("username"));
     user.setPassword(rs.getString("password"));
     user.setFace(rs.getString("face"));
     user.setSex(rs.getString("sex"));
     user.setEmail(rs.getString("email"));
     user.setPwdQuestion(rs.getString("pwdQuestion"));
     user.setPwdAnswer(rs.getString("pwdAnswer"));
     user.setRegDate(rs.getTimestamp("regDate"));
     user.setLoginCount(rs.getInt("loginCount"));
     user.setLastLogin(rs.getTimestamp("lastLogin"));
     user.setIsActive(rs.getByte("isActive"));
     user.setPrimeCount(rs.getInt("primeCount"));
     user.setArticleCount(rs.getInt("articleCount"));
    }
    else{
     user=null;
    }
      }
      catch (Exception ex) {
       user=null;
      }
      finally {
       SqlTool.closeResultSet(rs);
       SqlTool.closePreparedStatement(pstmt);
       SqlTool.closeConnection(conn);
       return user;
      }
  }
 }
 
 public boolean login(String username,String password){
  PreparedStatement pstmt=null;
  Connection conn=null;
  ResultSet rs=null;
  boolean;
  conn=SqlTool.getConnection();
  if(conn==null){
   return checked;
  }
  else{
   try {
    pstmt=conn.prepareStatement("select username,password from userinfo where username=? and password=? and isActive=1");
    pstmt.setString(1,username);
    pstmt.setString(2,MD5Encode.encode(password));
    
    rs=pstmt.executeQuery();
    while(rs.next()){
     checked=true;
    }
    if(checked==true){
     pstmt=conn.prepareStatement("update userinfo set loginCount=loginCount+1,lastLogin=? where username=?");
     Timestamp current=new Timestamp(System.currentTimeMillis());
     pstmt.setTimestamp(1,current);
     pstmt.setString(2,username);
     pstmt.executeUpdate();
    }
      }
      catch (Exception ex) {;
      }
      finally {
       SqlTool.closeResultSet(rs);
       SqlTool.closePreparedStatement(pstmt);
       SqlTool.closeConnection(conn);
       return checked;
      }
  }
 }
 
 
 public boolean updateUserInfo(int uid,String password,String face,String sex,String email,String pwdQuestion,String pwdAnswer){
  PreparedStatement pstmt=null;
  Connection conn=null;
  ResultSet rs=null;
  boolean updated=false;
  conn=SqlTool.getConnection();
  if(conn==null){
   return false;
  }
  else{
   try {
    pstmt=conn.prepareStatement("update userinfo set password=?,face=?,sex=?,email=?,pwdQuestion=?,pwdAnswer=? where uid=?");
    pstmt.setString(1,MD5Encode.encode(password));
    pstmt.setString(2,face);
    pstmt.setString(3,sex);
    pstmt.setString(4,email);
    pstmt.setString(5,pwdQuestion);
    pstmt.setString(6,pwdAnswer);
    pstmt.setInt(7,uid);
    pstmt.executeUpdate();
    updated=true;
      }
      catch (Exception ex) {
       updated=false;
      }
      finally {
       SqlTool.closePreparedStatement(pstmt);
       SqlTool.closeConnection(conn);
       return updated;
      }
  }
 }
 
 public boolean updateUserInfo(String username,String password,String face,String sex,String email,String pwdQuestion,String pwdAnswer){
  PreparedStatement pstmt=null;
  Connection conn=null;
  ResultSet rs=null;
  boolean updated=false;
  conn=SqlTool.getConnection();
  if(conn==null){
   return false;
  }
  else{
   try {
    pstmt=conn.prepareStatement("update userinfo set password=?,face=?,sex=?,email=?,pwdQuestion=?,pwdAnswer=? where username=?");
    pstmt.setString(1,MD5Encode.encode(password));
    pstmt.setString(2,face);
    pstmt.setString(3,sex);
    pstmt.setString(4,email);
    pstmt.setString(5,pwdQuestion);
    pstmt.setString(6,pwdAnswer);
    pstmt.setString(7,username);
    pstmt.executeUpdate();
    updated=true;
      }
      catch (Exception ex) {
       updated=false;
      }
      finally {
       SqlTool.closePreparedStatement(pstmt);
       SqlTool.closeConnection(conn);
       return updated;
      }
  }
 }
 
 public boolean updateUserInfo(UserInfo userInfo,int uid){
  PreparedStatement pstmt=null;
  Connection conn=null;
  ResultSet rs=null;
  boolean updated=false;
  conn=SqlTool.getConnection();
  if(conn==null){
   return false;
  }
  else{
   try {
    pstmt=conn.prepareStatement("update userinfo set face=?,sex=?,email=?,pwdQuestion=?,pwdAnswer=? where uid=?");
    pstmt.setString(1,userInfo.getFace());
    pstmt.setString(2,userInfo.getSex());
    pstmt.setString(3,userInfo.getEmail());
    pstmt.setString(4,userInfo.getPwdQuestion());
    pstmt.setString(5,userInfo.getPwdAswer());
    pstmt.setInt(6,uid);
    pstmt.executeUpdate();
    updated=true;
      }
      catch (Exception ex) {
       updated=false;
      }
      finally {
       SqlTool.closePreparedStatement(pstmt);
       SqlTool.closeConnection(conn);
       return updated;
      }
  }
 }
 
 public boolean existsUsername(String username){
  PreparedStatement pstmt=null;
  Connection conn=null;
  ResultSet rs=null;
  boolean exists=false;
  conn=SqlTool.getConnection();
  if(conn==null){
   return true;
  }
  else{
   try {
    pstmt=conn.prepareStatement("select username from userinfo where username=?");
    pstmt.setString(1,username);
    rs=pstmt.executeQuery();
    while(rs.next()){
     exists=true;
    }
      }
      catch (Exception ex) {
       System.out.println(ex.getMessage());
       exists=true;
      }
      finally {
       SqlTool.closeResultSet(rs);
       SqlTool.closePreparedStatement(pstmt);
       SqlTool.closeConnection(conn);
       return exists;
      }
  }
 }
 
 public boolean checkRole(String username,String roleName){
  PreparedStatement pstmt=null;
  Connection conn=null;
  ResultSet rs=null;
  boolean hasRole=false;
  conn=SqlTool.getConnection();
  if(username.equals("admin")){
   return true;
  }
  else if(conn==null){
   return false;
  }
  else{
   try {
    pstmt=conn.prepareStatement("select role.uid,role.rolename from userinfo,role where username=? and rolename=? and userinfo.uid=role.uid");
    pstmt.setString(1,username);
    pstmt.setString(2,roleName);
    
    rs=pstmt.executeQuery();
    while(rs.next()){
     hasRole=true;
    }
    
    pstmt=conn.prepareStatement("select role.uid,role.rolename from userinfo,role where username=? and rolename=? and userinfo.uid=role.uid");
    pstmt.setString(1,username);
    pstmt.setString(2,"administrator");
    
    rs=pstmt.executeQuery();
    while(rs.next()){
     hasRole=true;
    }
      }
      catch (Exception ex) {
       hasRole=false;
      }
      finally {
       SqlTool.closeResultSet(rs);
       SqlTool.closePreparedStatement(pstmt);
       SqlTool.closeConnection(conn);
       return hasRole;
      }
  }
 }
 
 public ArrayList getUserList(int startIndex,int size){
  PreparedStatement pstmt=null;
  Connection conn=null;
  ResultSet rs=null;
  conn=SqlTool.getConnection();
  ArrayList userList=new ArrayList(size);
  if(conn==null){
   return null;
  }
  else{
   try {
    pstmt=conn.prepareStatement("select * from userinfo where isActive=1 order by uid");
    rs=pstmt.executeQuery();
    if(startIndex==0){
     rs.beforeFirst();
    }
    else{
     rs.absolute(startIndex);
    }
    int current=0;
    while(rs.next()&¤t<size){
     UserInfo user=new UserInfo();
     user.setUserId(rs.getInt("uid"));
     user.setUsername(rs.getString("username"));
     user.setFace(rs.getString("face"));
     user.setSex(rs.getString("sex"));
     user.setEmail(rs.getString("email"));
     user.setPwdQuestion(rs.getString("pwdQuestion"));
     user.setPwdAnswer(rs.getString("pwdAnswer"));
     user.setRegDate(rs.getTimestamp("regDate"));
     user.setLoginCount(rs.getInt("loginCount"));
     user.setLastLogin(rs.getTimestamp("lastLogin"));
     user.setIsActive(rs.getByte("isActive"));
     user.setPrimeCount(rs.getInt("primeCount"));
     
     userList.add(user);
     current++;
    }
      }
      catch (Exception ex) {
       System.out.println(ex.getMessage());
          userList=new ArrayList();
      }
      finally {
       SqlTool.closeResultSet(rs);
       SqlTool.closePreparedStatement(pstmt);
       SqlTool.closeConnection(conn);
       return userList;
      }
  }
 }
 
 /**
  *查询具有特权用户的列表
  *@param startIndex 起始位置
  *@param size 每次返回的个数
  */
 public ArrayList getPowerUserList(int startIndex,int size){
  PreparedStatement pstmt=null;
  Connection conn=null;
  ResultSet rs=null;
  conn=SqlTool.getConnection();
  ArrayList userList=new ArrayList(size);
  if(conn==null){
   return null;
  }
  else{
   try {
    pstmt=conn.prepareStatement("select * from userinfo where uid in(select uid from role) order by uid");
    rs=pstmt.executeQuery();
    if(startIndex==0){
     rs.beforeFirst();
    }
    else{
     rs.absolute(startIndex);
    }
    int current=0;
    while(rs.next()&¤t<size){
     UserInfo user=new UserInfo();
     user.setUserId(rs.getInt("uid"));
     user.setUsername(rs.getString("username"));
     user.setFace(rs.getString("face"));
     user.setSex(rs.getString("sex"));
     user.setEmail(rs.getString("email"));
     user.setPwdQuestion(rs.getString("pwdQuestion"));
     user.setPwdAnswer(rs.getString("pwdAnswer"));
     user.setRegDate(rs.getTimestamp("regDate"));
     user.setLoginCount(rs.getInt("loginCount"));
     user.setLastLogin(rs.getTimestamp("lastLogin"));
     
     userList.add(user);
     current++;
    }
      }
      catch (Exception ex) {
       System.out.println(ex.getMessage());
          userList=new ArrayList();
      }
      finally {
       SqlTool.closeResultSet(rs);
       SqlTool.closePreparedStatement(pstmt);
       SqlTool.closeConnection(conn);
       return userList;
      }
  }
 }
 
 public boolean grantRole(int uid,String roleName){
  PreparedStatement pstmt=null;
  Connection conn=null;
  ResultSet rs=null;
  boolean success=false;
  conn=SqlTool.getConnection();
  if(conn==null){
   return success;
  }
  else{
   try {
    pstmt=conn.prepareStatement("insert into role(uid,roleName,displayName)values(?,?,?)");
    pstmt.setInt(1,uid);
    pstmt.setString(2,roleName);
    pstmt.setString(3,RoleInfo.convertRoleName(roleName));
    pstmt.executeUpdate();
    success=true;
      }
      catch (Exception ex) {
       success=false;
      }
      finally {
       SqlTool.closePreparedStatement(pstmt);
       SqlTool.closeConnection(conn);
       return success;
      }
  }
 }
 
 public boolean revokeRole(int uid,String roleName){
  PreparedStatement pstmt=null;
  Connection conn=null;
  ResultSet rs=null;
  boolean success=false;
  conn=SqlTool.getConnection();
  if(conn==null){
   return success;
  }
  else{
   try {
    pstmt=conn.prepareStatement("delete from role where uid=? and roleName=?");
    pstmt.setInt(1,uid);
    pstmt.setString(2,roleName);
    pstmt.executeUpdate();
    success=true;
      }
      catch (Exception ex) {
       success=false;
      }
      finally {
       SqlTool.closePreparedStatement(pstmt);
       SqlTool.closeConnection(conn);
       return success;
      }
  }
 }

 /**
  *当用户忘记密码的时候,如果用户回答忘记密码提示问题正确,就重设用户密码。
  *@param uid 忘记用户密码的用户编号
  *@return 重设的密码
  */
 public String resetPassword(int uid){
  PreparedStatement pstmt=null;
  Connection conn=null;
  ResultSet rs=null;
  conn=SqlTool.getConnection();
  if(conn==null){
   return "0";
  }
  else{
   String password="";
   StringBuffer sBuffer=new StringBuffer();
   Random random=new Random();
   for(int i=0;i<8;i++){
    sBuffer.append(random.nextInt(10));
   }
   password=sBuffer.toString();
   try {
    pstmt=conn.prepareStatement("update userinfo set password=? where uid=?");
    pstmt.setString(1,MD5Encode.encode(password));
    pstmt.setInt(2,uid);
    pstmt.executeUpdate();
      }
      catch (Exception ex) {
       password="0";
      }
      finally {
       SqlTool.closePreparedStatement(pstmt);
       SqlTool.closeConnection(conn);
       return password;
      }
  }
 }
 
 /**
  *更改密码
  *@param uid 用户编号
  *@param password 用户的新密码
  *@return 是否成功更改了密码
  */
 public boolean changePassword(int uid,String password){
  PreparedStatement pstmt=null;
  Connection conn=null;
  ResultSet rs=null;
  boolean success=false;
  conn=SqlTool.getConnection();
  if(conn==null){
   return success;
  }
  else{
   try {
    pstmt=conn.prepareStatement("update userinfo set password=? where uid=?");
    pstmt.setString(1,MD5Encode.encode(password));
    pstmt.setInt(2,uid);
    pstmt.executeUpdate();
    success=true;
      }
      catch (Exception ex) {
       success=false;
      }
      finally {
       SqlTool.closePreparedStatement(pstmt);
       SqlTool.closeConnection(conn);
       return success;
      }
  }
 } 
 
 public ArrayList getRoleListByName(String username){
  PreparedStatement pstmt=null;
  Connection conn=null;
  ResultSet rs=null;
  conn=SqlTool.getConnection();
  ArrayList roleList=new ArrayList();
  if(conn==null) {
   return roleList;
  }
  else{
   try {
    pstmt=conn.prepareStatement("select uid,roleName,displayName from role where username=? order by roleName");
    pstmt.setString(1,username);
    rs=pstmt.executeQuery();
    while(rs.next()){
     RoleInfo roleInfo=new RoleInfo();
     roleInfo.setId(rs.getInt("uid"));
     roleInfo.setRoleName(rs.getString("roleName"));
     roleInfo.setDisplayName(rs.getString("displayName"));
     roleList.add(roleInfo);
    }
      }
      catch (Exception ex) {
       return null;
      }
      finally {
       SqlTool.closeResultSet(rs);
       SqlTool.closePreparedStatement(pstmt);
       SqlTool.closeConnection(conn);
       return roleList;
      }
  }
 }
 
 public ArrayList getRoleListById(int id){
  PreparedStatement pstmt=null;
  Connection conn=null;
  ResultSet rs=null;
  conn=SqlTool.getConnection();
  ArrayList roleList=new ArrayList();
  if(conn==null) {
   return roleList;
  }
  else{
   try {
    pstmt=conn.prepareStatement("select uid,roleName,displayName from role where uid=? order by roleName");
    pstmt.setInt(1,id);
    rs=pstmt.executeQuery();
    while(rs.next()){
     RoleInfo roleInfo=new RoleInfo();
     roleInfo.setId(rs.getInt("uid"));
     roleInfo.setRoleName(rs.getString("roleName"));
     roleInfo.setDisplayName(rs.getString("displayName"));
     roleList.add(roleInfo);
    }
      }
      catch (Exception ex) {
       return null;
      }
      finally {
       SqlTool.closeResultSet(rs);
       SqlTool.closePreparedStatement(pstmt);
       SqlTool.closeConnection(conn);
       return roleList;
      }
  }
 }
 
 /**
  *查询注册用户总数
  *@return 网站所有注册用户的总数(包括禁止登录的用户)。
  */
 public int getUserCount(){
  PreparedStatement pstmt=null;
  Connection conn=null;
  ResultSet rs=null;
  int count=0;
  conn=SqlTool.getConnection();
  if(conn==null) {
   return 0;
  }
  else{
   try {
    pstmt=conn.prepareStatement("select uid from userinfo");
    rs=pstmt.executeQuery();
    rs.last();
    count=rs.getRow();
      }
      catch (Exception ex) {
       count=0;
      }
      finally {
       SqlTool.closeResultSet(rs);
       SqlTool.closePreparedStatement(pstmt);
       SqlTool.closeConnection(conn);
       return count;
      }
  }  
 }
 
 /**
  *获取网站最近的一个注册用户
  *@return 用户信息
  */
 public UserInfo getLastUser(){
  PreparedStatement pstmt=null;
  Connection conn=null;
  ResultSet rs=null;
  UserInfo user=null;
  conn=SqlTool.getConnection();
  if(conn==null) {
   return user;
  }
  else{
   try {
    pstmt=conn.prepareStatement("select * from userinfo where isActive=1 order by regDate desc");
    rs=pstmt.executeQuery();
    while(rs.next()){
     user.setUserId(rs.getInt("uid"));
     user.setUsername(rs.getString("username"));
     user.setPassword(rs.getString("password"));
     user.setFace(rs.getString("face"));
     user.setSex(rs.getString("sex"));
     user.setEmail(rs.getString("email"));
     user.setPwdQuestion(rs.getString("pwdQuestion"));
     user.setPwdAnswer(rs.getString("pwdAnswer"));
     user.setRegDate(rs.getTimestamp("regDate"));
     user.setLoginCount(rs.getInt("loginCount"));
     user.setLastLogin(rs.getTimestamp("lastLogin"));
    }
      }
      catch (Exception ex) {
       user=null;
      }
      finally {
       SqlTool.closeResultSet(rs);
       SqlTool.closePreparedStatement(pstmt);
       SqlTool.closeConnection(conn);
       return user;
      }
  }  
 }
 
 public static void main(String[] args){
  UserTool tool=UserTool.getInstance();
  //System.out.println(tool.checkRole("周金桥","bbsAdmin"));
  System.out.println(tool.getUserInfo("周金桥").getLoginCount());
  //boolean success=tool.revokeRole(2,"tradeAdmin");
  
  //tool.getUserList(0,50);
  //tool.getUserInfo(2);
  //UserInfo user=tool.getUserInfo(2);
  //boolean updated=tool.updateUserInfo(2,"790918",user.getFace(),user.getSex(),user.getEmail(),user.getPwdQuestion(),user.getPwdAswer());
  //System.out.println();
 }
}


在几个频繁查询页面多次点击之后,居然出现了没有连接的情况,如下图:



停止点击一段时间后就又恢复到可以使用连接池状态,怪哉!每次使用完之后我都会关闭的,怎么就不能及时关闭呢?



而且日志文件里有如下信息:
2006-8-30 10:06:11 org.logicalcobwebs.proxool.HouseKeeper sweep
警告: #0031 was active for 317484 milliseconds and has been removed automaticaly. The Thread responsible was named 'http-80-Processor23', but the last SQL it performed is unknown because the trace property is not enabled.
2006-8-30 10:06:11 org.logicalcobwebs.proxool.HouseKeeper sweep
警告: #0032 was active for 316281 milliseconds and has been removed automaticaly. The Thread responsible was named 'http-80-Processor23', but the last SQL it performed is unknown because the trace property is not enabled.
2006-8-30 10:06:11 org.logicalcobwebs.proxool.HouseKeeper sweep
警告: #0033 was active for 314734 milliseconds and has been removed automaticaly. The Thread responsible was named 'http-80-Processor23', but the last SQL it performed is unknown because the trace property is not enabled.
2006-8-30 10:06:11 org.logicalcobwebs.proxool.HouseKeeper sweep
警告: #0034 was active for 314062 milliseconds and has been removed automaticaly. The Thread responsible was named 'http-80-Processor23', but the last SQL it performed is unknown because the trace property is not enabled.
2006-8-30 10:06:11 org.logicalcobwebs.proxool.HouseKeeper sweep
警告: #0035 was active for 313328 milliseconds and has been removed automaticaly. The Thread responsible was named 'http-80-Processor23', but the last SQL it performed is unknown because the trace property is not enabled.
2006-8-30 10:06:11 org.logicalcobwebs.proxool.HouseKeeper sweep
警告: #0036 was active for 312406 milliseconds and has been removed automaticaly. The Thread responsible was named 'http-80-Processor23', but the last SQL it performed is unknown because the trace property is not enabled.
2006-8-30 10:06:11 org.logicalcobwebs.proxool.HouseKeeper sweep
警告: #0037 was active for 310234 milliseconds and has been removed automaticaly. The Thread responsible was named 'http-80-Processor23', but the last SQL it performed is unknown because the trace property is not enabled.
2006-8-30 10:08:22 org.logicalcobwebs.proxool.ConnectionPool getConnection
信息: 000181 -000008 (30/30/00) - Couldn't get connection because we are at maximum connection count and there are none available
2006-8-30 10:08:23 org.logicalcobwebs.proxool.ConnectionPool getConnection
信息: 000181 -000009 (30/30/00) - Couldn't get connection because we are at maximum connection count and there are none available
2006-8-30 10:08:23 org.logicalcobwebs.proxool.ConnectionPool getConnection
信息: 000181 -000010 (30/30/00) - Couldn't get connection because we are at maximum connection count and there are none available
2006-8-30 10:08:24 org.logicalcobwebs.proxool.ConnectionPool getConnection
信息: 000181 -000011 (30/30/00) - Couldn't get connection because we are at maximum connection count and there are none available
2006-8-30 10:08:24 org.logicalcobwebs.proxool.ConnectionPool getConnection
信息: 000181 -000012 (30/30/00) - Couldn't get connection because we are at maximum connection count and there are none available


难道调用它自己的方法不能关闭Connection?

有待进一步探索。



2010-12-01更新:这篇文章发布以后,没有想到很多朋友通过搜索引擎找到了它,大多数人都遇到了在本篇中描述的问题,在这里要告诉大家的是有一个Proxool的替代选择,那就是使用BoneCP数据库连接池,它不断支持使用JDBC的方式操作数据库,还可以在Hibernate中使用,关于BoneCP的介绍和详细使用方法请见《Java中的BoneCP数据库连接池用法》
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: