druid连接池的问题connection holder is null 问题求解

鸡冻的三毛 发布于 2015/01/14 17:05
阅读 6K+
收藏 0

各位大神,请教一个问题  @wenshao

druid配置如下

 

#连接池建立时创建的初始化连接数
initialSize = 3
#连接池中最大的活跃连接数
maxActive = 10
#连接池中最小的活跃连接数
minIdle = 3
#获取连接等待时间ms
maxWait = 60000
#检测连接时间  10分钟
timeBetweenEvictionRunsMillis = 600000
#检测未关闭连接大于该值时,则关闭连接  10分钟
minEvictableIdleTimeMillis = 600000
#验证语句
validationQuery = SELECT 1 FROM DUAL
testWhileIdle = true
#从连接池获取连接时,是否检测连接可用性
testOnBorrow = false
#释放连接到连接池时,是否检测连接可用性
testOnReturn = false
#开启psCache缓存
poolPreparedStatements = true
#psCache缓存 100
maxOpenPreparedStatements=100
maxPoolPreparedStatementPerConnectionSize = 50
filters =stat

#对于建立连接过长的连接强制关闭 
removeAbandoned=true 
#如果连接建立时间超过了30分钟,则强制将其关闭 
removeAbandonedTimeout=1800
#将当前关闭动作记录到日志 
logAbandoned=true

 

执行后会报异常,但是又不是每次都报,出现的频率还蛮高的

java.sql.SQLException: connection holder is null
        at com.alibaba.druid.pool.DruidPooledConnection.checkState(DruidPooledConnection.java:1085)
        at com.alibaba.druid.pool.DruidPooledConnection.commit(DruidPooledConnection.java:705)
        at com.xwtec.baseDao.JdbcTemplate.excuteBatch(JdbcTemplate.java:62)
        at com.xwtec.dao.impl.LoadNewsInfoDAOImpl.batchAddNews(LoadNewsInfoDAOImpl.java:44)
        at com.xwtec.service.impl.LoadNewsInfoServiceImpl.loadNewsInfoList(LoadNewsInfoServiceImpl.java:132)
        at com.xwtec.job.LoadNewsJob$1.run(LoadNewsJob.java:49)
        at java.util.TimerThread.mainLoop(Timer.java:512)
        at java.util.TimerThread.run(Timer.java:462)

 

连接池的方法

public class DruidPoolUtils {
 
 private static DataSource ds = null;
 
 private static final ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
 
 static {
  try {
   InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream(
     "druid.properties");
   Properties props = new Properties();
   props.load(in);
   ds = DruidDataSourceFactory.createDataSource(props);
  } catch (Exception ex) {
   ex.printStackTrace();
  }
 }

 /**
  * 获取连接
  * @return
  * @throws Exception
  */
 public synchronized static Connection getConnection() throws SQLException {
  Connection conn = (Connection) threadLocal.get();
  try {
   if (conn == null || conn.isClosed()) {
    conn = ds.getConnection();//使用Druid
    if (conn == null || conn.isClosed()) {
     System.out.println("DruidPoolUtils.getConnection()时发生异常,连接可能已经关闭,再试一次....");
     conn = ds.getConnection();
     System.out.println("DruidPoolUtils.getConnection()时发生异常,连接可能已经关闭,再试一次....conn==" + conn);
    }
    
    conn.setAutoCommit(false);
    threadLocal.set(conn);
   }
  } catch (Exception e) {
   throw new SQLException("数据库访问失败..");
  }
  return conn;
 }

 /**
  * 关闭连接
  * @throws Exception
  */
 public static void closeConnection() throws SQLException {
  Connection conn = (Connection) threadLocal.get();
  if (conn != null && !conn.isClosed()) {
   try {
    conn.close();
   } catch (Exception e) {
    throw new SQLException("关闭数据库连接失败..");
   }
   threadLocal.set(null);
  }
 }

 public static void commit() throws SQLException {
  try {
   getConnection().commit();
  } catch (Exception e) {
   throw new SQLException("数据库提交异常..");
  }
 }

 public static void rollback() throws SQLException{
  try {
   getConnection().rollback();
  } catch (SQLException e) {
   throw new SQLException("数据库回滚异常..");
  }
 }

 public static void close(Statement stmt) throws SQLException {
  if (stmt != null) {
   try {
    stmt.close();
   } catch (SQLException e) {
    throw new SQLException("数据库Statement 关闭异常..");
   }
  }
 }

 public static void close(ResultSet rs) throws SQLException{
  if (rs != null) {
   try {
    rs.close();
   } catch (SQLException e) {
    throw new SQLException("数据库ResultSet 关闭异常..");
   }
  }
 }


}

 

加载中
0
miaomiaoup
miaomiaoup
我也有类似的问题  我也把连接放到threadLocal 中了 猜想原因是该链接使用后没有被close 你现在解决了吗
0
JackFace
JackFace
楼上的问题解决了吗?
返回顶部
顶部