错误:创建一个空数组列表以保存连接(连接池)

时间:2017-07-29 09:34:37

标签: java mysql jdbc connection-pooling

我想在我的项目中实现连接池(Mysql Database)。 我得到了相同的示例代码链接:Click here

  • 我创建了3个班级
  • 包含数据库配置的
  • 创建和管理连接的JdbcConnectionPool
  • DataSource 类以获取连接并将连接返回到连接池

enter image description here

JdbcConnectionPool.class 中,我收到错误:

List<connection> availableConnections = new ArrayList<connection>();

错误说com.java.abc包中的create class &#34; connection&#34;

我的 JdbcConnectionPool.class

package com.java.abc;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.Connection;

public class JdbcConnectionPool {

 List<connection> availableConnections = new ArrayList<connection>();

 public JdbcConnectionPool()
 {
  initializeConnectionPool();
 }

 private void initializeConnectionPool()
 {
  while(!checkIfConnectionPoolIsFull())
  {
   availableConnections.add(createNewConnectionForPool());
  }
 }

 private synchronized boolean checkIfConnectionPoolIsFull()
 {
  final int MAX_POOL_SIZE = Configuration.getInstance().DB_MAX_CONNECTIONS;

  if(availableConnections.size() < MAX_POOL_SIZE)
  {
   return false;
  }

  return true;
 }

 //Creating a connection
 private Connection createNewConnectionForPool()
 {
  Configuration config = Configuration.getInstance();
  try {
   Class.forName(config.DB_DRIVER);
   Connection connection = (Connection) DriverManager.getConnection(
     config.DB_URL, config.DB_USER_NAME, config.DB_PASSWORD);
   return connection;
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return null;

 }

 public synchronized Connection getConnectionFromPool()
 {
  Connection connection = null;
  if(availableConnections.size() > 0)
  {
   connection = (Connection) availableConnections.get(0);
   availableConnections.remove(0);
  }
  return connection;
 }

 public synchronized void returnConnectionToPool(Connection connection)
 {
  availableConnections.add(connection);
 }
}

如何修复此错误。?

0 个答案:

没有答案