druid连接池的问题

wanlitian9 发布于 2013/11/12 11:16
阅读 839
收藏 0

druid配置如下

driverClassName=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf8
username=root
password=root

initialSize=2

maxActive=100


testOnBorrow=false
testOnReturn=false
testWhileIdle=true
validationQuery=SELECT 1 FROM DUAL
minIdle=1

maxWait=30000

经常出现

java.sql.SQLException: connection holder is null
    com.alibaba.druid.pool.DruidPooledConnection.checkState(DruidPooledConnection.java:1023)
    com.alibaba.druid.pool.DruidPooledConnection.prepareStatement(DruidPooledConnection.java:306)
    org.apache.jsp.list_jsp._jspService(list_jsp.java:121)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

连接代码如下

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSource;


import com.alibaba.druid.pool.DruidDataSourceFactory;


public class DBConnection {
    private static DruidDataSource ds;
    // 在同一个线程中保存某个对象
    private static final ThreadLocal<Connection> connLocal =
        new ThreadLocal<Connection>();
    static{
        try{//加载连接池参数,实例化连接池对象
            Properties props = new Properties();
            props.load(DBConnection.class.getClassLoader()
                .getResourceAsStream("dbcp.properties"));
            ds =(DruidDataSource) DruidDataSourceFactory
                    .createDataSource(props);
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
    
    public static Connection getConnection()
            throws SQLException{
        // 获取
        Connection conn = connLocal.get();
                 System.out.println("connectCount"+ds.getConnectCount());
                 System.out.println("activeCount"+ds.getActiveCount());
                 System.out.println("PooledCount"+ds.getPoolingCount());
                 System.out.println("PooledpeakCount"+ds.getPoolingPeak());
                 
                 
                       
        
        // 获取不到创建
        if(conn==null) {
            //从连接池中获取Connection返回
            conn = ds.getConnection();
            connLocal.set(conn);
        }
        return conn;
    }
    
    //释放Connection
    public static void close() throws SQLException{
        Connection conn = connLocal.get();
        connLocal.set(null);
        
        if(conn!=null&&!conn.isClosed()){
            conn.close();
                        connLocal.remove();
        }
    }

}

druid版本是0.2.12

请大家指教

请大家指教 是

以下是问题补充:

@wanlitian9:@wenshao 麻烦您看一下 (2013/11/14 08:19)
加载中
0
jackliang
jackliang
你换新版本试试1.0.0
返回顶部
顶部