需要代码在java中创建连接池 [英] Need Code to create Connection Pool in java

查看:162
本文介绍了需要代码在java中创建连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要在java中创建连接池的代码吗?
我们如何确保连接池不返回已经在使用的同一对象?
如果客户端在从连接池中取出连接后如何关闭连接?

Need code to create the connection pool in java? How does we make sure that connection pool doesn't return the same object which is already in use? How happens if client closed the connection after taking it out from Connection pool?

我想在Simple Java术语中创建它,并希望了解它在多线程环境中的工作原理。我的意思是哪些方法将被同步,哪些不是。此类也将是一个公共类?如果是,那么任何人都可以访问此类并重新初始化连接池?

I want to create this in Simple Java terms and want to see how it works in Multithreading Env. I mean which methods would be synchronized and which are not. Also will this class would be a public class? If yes then any one can access this class and reinitialize the connection pool?

一些代码如下。但我不知道如何关闭来自池的连接将其返回到池,它不会物理关闭连接。
我也不明白这个因为如果一个连接已经从池中借用,但还没有返回,它不是可用,不能重新分配到池的另一个客户端。

I have some code as below. But i don't how "Closing a connection coming from a pool returns it to the pool, it doesn't close the connection physically." Also i didn't understood this "Because if a connection has been borrowed from the pool and not returned yet, it's not "available" and can't be redistributed to another client of the pool."

import java.util.*;
import java.sql.*;

class ConnectionPoolManager
{

 String databaseUrl = "jdbc:mysql://localhost:3306/myDatabase";
 String userName = "userName";
 String password = "userPass";

 Vector connectionPool = new Vector();

 public ConnectionPoolManager()
 {
  initialize();
 }

 public ConnectionPoolManager(
  //String databaseName,
  String databaseUrl,
  String userName,
  String password
  )
 {
  this.databaseUrl = databaseUrl;
  this.userName = userName;
  this.password = password;
  initialize();
 }

 private void initialize()
 {
  //Here we can initialize all the information that we need
  initializeConnectionPool();
 }

 private void initializeConnectionPool()
 {
  while(!checkIfConnectionPoolIsFull())
  {
   System.out.println("Connection Pool is NOT full. Proceeding with adding new connections");
   //Adding new connection instance until the pool is full
   connectionPool.addElement(createNewConnectionForPool());
  }
  System.out.println("Connection Pool is full.");
 }

 private synchronized boolean checkIfConnectionPoolIsFull()
 {
  final int MAX_POOL_SIZE = 5;

  //Check if the pool size
  if(connectionPool.size() < 5)
  {
   return false;
  }

  return true;
 }

 //Creating a connection
 private Connection createNewConnectionForPool()
 {
  Connection connection = null;

  try
  {
   Class.forName("com.mysql.jdbc.Driver");
   connection = DriverManager.getConnection(databaseUrl, userName, password);
   System.out.println("Connection: "+connection);
  }
  catch(SQLException sqle)
  {
   System.err.println("SQLException: "+sqle);
   return null;
  }
  catch(ClassNotFoundException cnfe)
  {
   System.err.println("ClassNotFoundException: "+cnfe);
   return null;
  }

  return connection;
 }

 public synchronized Connection getConnectionFromPool()
 {
  Connection connection = null;

  //Check if there is a connection available. There are times when all the connections in the pool may be used up
  if(connectionPool.size() > 0)
  {
   connection = (Connection) connectionPool.firstElement();
   connectionPool.removeElementAt(0);
  }
  //Giving away the connection from the connection pool
  return connection;
 }

 public synchronized void returnConnectionToPool(Connection connection)
 {
  //Adding the connection from the client back to the connection pool
  connectionPool.addElement(connection);
 }

 public static void main(String args[])
 {
  ConnectionPoolManager ConnectionPoolManager = new ConnectionPoolManager();
 }

}


推荐答案


需要代码在java中创建连接池吗?

Need code to create the connection pool in java?

不知道什么问题是请不要创建另一个连接池,请使用 C3P0 等现有解决方案, Apache DBCP Proxool BoneCP (该字段中的新玩家)。我将使用C3P0。

Not sure what the question is but don't create yet another connection pool, use an existing solution like C3P0, Apache DBCP, Proxool or BoneCP (a new player in that field). I would use C3P0.


我们如何确保连接池不返回已经在使用的同一个对象? p>

How does we make sure that connection pool doesn't return the same object which is already in use?

因为如果连接已从池中借用而尚未返回,则它只是不在池中,不能分配给另一个

Because if a connection has been borrowed from the pool and not returned yet, it's just not in the pool and can't be assigned to another client of the pool (resources are removed from the pool until they are returned).


如果客户端在连接完成后关闭连接,会发生什么情况从连接池?

How happens if client closed the connection after taking it out from Connection pool?

客户端从池获取的连接实际上不是 java.sql.Connection ,它是 <$ c的包装器(代理) $ c> java.sql.Connection 自定义一些方法的行为。 close()方法是其中之一,并且关闭连接实例,将其返回到池。

The connection a client gets from a pool is not really a java.sql.Connection, it's a wrapper (a proxy) for a java.sql.Connection that customizes the behavior of some methods. The close() method is one of them and does not close the Connection instance but returns it to the pool.

这篇关于需要代码在java中创建连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆