请详细谈论一下,数据库连接池的原理,和使用数据库连接池 的好处??
最好能够附带一个自定义的数据库连接池的例子!!!
不胜感激!!!!!1
连接池的基本原理? 以及使用连接池的好处?
答案:2 悬赏:40
解决时间 2021-03-03 13:49
- 提问者网友:美人如花
- 2021-03-02 21:22
最佳答案
- 二级知识专家网友:晚安听书人
- 2021-03-02 22:37
package test;
import java.sql.*;
import java.util.*;
public class DBConnpool
{
private int inUse = 0;
private Vector connections = new Vector();
private String poolname = "dbconnpool";
private String dbid = "jdbc:mysql://localhost:3306/teasystem";
private String drivername = "com.mysql.jdbc.Driver";
private String username = "root";
private String password = "123";
private int maxconn = 5000;
public DBConnpool(){}
public void setdbid(String dbid)
{
this.dbid = dbid;
}
public void setusername(String username)
{
this.username = username;
}
public void setpassword(String password)
{
this.password = password;
}
public void setmaxconn(int maxconn)
{
this.maxconn = maxconn;
}
public String getdbid()
{
return dbid;
}
public String getusername()
{
return username;
}
public String getpassword()
{
return password;
}
public int getmaxconn()
{
return maxconn;
}
//将连接返还给连接池
public synchronized void reConnection(Connection conn)
{
Connection con = conn;
connections.addElement(con);
inUse--;
}
//从连接池获取一个连接
public synchronized Connection getConnection()
{
Connection con = null;
if(connections.size()>0)
{
con = (Connection)connections.elementAt(0);
connections.removeElementAt(0);
try{
if(con.isClosed())
{
con = getConnection();
}
}catch(Exception e){
e.printStackTrace();
}
}else if(maxconn == 0||inUse
{
con = newConnection();
}
if(con != null)
{
inUse++;
}
return con;
}
private Connection newConnection()
{
Connection con = null;
try{
Class.forName(drivername);
con = DriverManager.getConnection(dbid,username,password);
}catch(Exception e){
e.printStackTrace();
return null;
}
return con;
}
public synchronized void closeConn()
{
Enumeration allConnections = connections.elements();
while(allConnections.hasMoreElements())
{
Connection con = (Connection)allConnections.nextElement();
try{
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
使用连接池,把暂时不使用的链接放入连接池,到需要使用的时候,从连接池中取出链接使用
import java.sql.*;
import java.util.*;
public class DBConnpool
{
private int inUse = 0;
private Vector
private String poolname = "dbconnpool";
private String dbid = "jdbc:mysql://localhost:3306/teasystem";
private String drivername = "com.mysql.jdbc.Driver";
private String username = "root";
private String password = "123";
private int maxconn = 5000;
public DBConnpool(){}
public void setdbid(String dbid)
{
this.dbid = dbid;
}
public void setusername(String username)
{
this.username = username;
}
public void setpassword(String password)
{
this.password = password;
}
public void setmaxconn(int maxconn)
{
this.maxconn = maxconn;
}
public String getdbid()
{
return dbid;
}
public String getusername()
{
return username;
}
public String getpassword()
{
return password;
}
public int getmaxconn()
{
return maxconn;
}
//将连接返还给连接池
public synchronized void reConnection(Connection conn)
{
Connection con = conn;
connections.addElement(con);
inUse--;
}
//从连接池获取一个连接
public synchronized Connection getConnection()
{
Connection con = null;
if(connections.size()>0)
{
con = (Connection)connections.elementAt(0);
connections.removeElementAt(0);
try{
if(con.isClosed())
{
con = getConnection();
}
}catch(Exception e){
e.printStackTrace();
}
}else if(maxconn == 0||inUse
con = newConnection();
}
if(con != null)
{
inUse++;
}
return con;
}
private Connection newConnection()
{
Connection con = null;
try{
Class.forName(drivername);
con = DriverManager.getConnection(dbid,username,password);
}catch(Exception e){
e.printStackTrace();
return null;
}
return con;
}
public synchronized void closeConn()
{
Enumeration allConnections = connections.elements();
while(allConnections.hasMoreElements())
{
Connection con = (Connection)allConnections.nextElement();
try{
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
使用连接池,把暂时不使用的链接放入连接池,到需要使用的时候,从连接池中取出链接使用
全部回答
- 1楼网友:邪性洒脱
- 2021-03-02 23:36
优点:连接高效、安全、复用
------解决方案--------------------------------------------------------
优缺点是在不同的情况下体现的,比如现在连接池里有30个连接:
1、如果应用同时最大只有10个以下,那么只有缺点,占用了资源。
2、如果应用同时最大的时候有20-30个连接,优点是和数据库连接花费的时间很少,速度快,可以复用。
------解决方案--------------------------------------------------------
优缺点是在不同的情况下体现的,比如现在连接池里有30个连接:
1、如果应用同时最大只有10个以下,那么只有缺点,占用了资源。
2、如果应用同时最大的时候有20-30个连接,优点是和数据库连接花费的时间很少,速度快,可以复用。
------解决方案--------------------------------------------------------
缓冲 初始化最初(配置) 回话同在
数据库连接池的基本原理是在内部对象池中维护一定数量的数据库连接,并对外暴露数据库连接获取和返回方法。如:
外部使用者可通过getconnection 方法获取连接,使用完毕后再通过releaseconnection 方法将连接返回,注意此时连接并没有关闭,而是由连接池管理器回收,并为下一次使用做好准备。
------解决方案--------------------------------------------------------探讨优缺点是在不同的情况下体现的,比如现在连接池里有30个连接:
1、如果应用同时最大只有10个以下,那么只有缺点,占用了资源。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯