如何在oracle中建立连接池阿?

mymoto 2003-07-24 12:40:06
请问如何创建,并且用java如何连接连接池,或者说连接池是靠java创建的,不懂,请求答案。谢谢
...全文
37 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qinchangyan 2003-07-25
  • 打赏
  • 举报
回复
首先,构造连接池对象:
ConnectionPool pool = new ConnectionPool("jdbc:oracle:thin:@host:1526:sid","username","password","oracle.jdbc.driver.OracleDriver",5,1);

然后,从连接池中获得连接:
Connection conn = pool.getConnection();

连接用完后,将连接返回给连接池:
if ( conn != null) pool.returnConnection(conn);

mymoto 2003-07-24
  • 打赏
  • 举报
回复
能讲讲上面代码如何用吗,或给个调用的例子,谢谢了
qinchangyan 2003-07-24
  • 打赏
  • 举报
回复
是用Java创建连接池吗?如果是,这有一段程序:
import java.sql.*;
import java.util.*;

public class ConnectionPool {
private Hashtable connections;
private int increment;
private String dbURL, user, password;

public ConnectionPool(String dbURL,
String user,
String password,
String driverClassName,
int initialConnections,
int increment)
throws SQLException, ClassNotFoundException {

// Load the specified driver class
Class.forName(driverClassName);

this.dbURL = dbURL;
this.user = user;
this.password = password;
this.increment = increment;

connections = new Hashtable();

// Put our pool of Connections in the Hashtable
// The FALSE value indicates they're unused
for(int i = 0; i < initialConnections; i++) {
connections.put(DriverManager.getConnection(dbURL, user, password),
Boolean.FALSE);
}
}

public Connection getConnection() throws SQLException {
Connection con = null;

Enumeration cons = connections.keys();

synchronized (connections) {
while(cons.hasMoreElements()) {
con = (Connection)cons.nextElement();

Boolean b = (Boolean)connections.get(con);
if (b == Boolean.FALSE) {
// So we found an unused connection.
// Test its integrity with a quick setAutoCommit(true) call.
// For production use, more testing should be performed,
// such as executing a simple query.
try {
con.setAutoCommit(true);
}
catch(SQLException e) {
// Problem with the connection, replace it.
con = DriverManager.getConnection(dbURL, user, password);
}
// Update the Hashtable to show this one's taken
connections.put(con, Boolean.TRUE);
// Return the connection
return con;
}
}
}

// If we get here, there were no free connections.
// We've got to make more.
for(int i = 0; i < increment; i++) {
connections.put(DriverManager.getConnection(dbURL, user, password),
Boolean.FALSE);
}

// Recurse to get one of the new connections.
return getConnection();
}

public void returnConnection(Connection returned) {
Connection con;
Enumeration cons = connections.keys();
while (cons.hasMoreElements()) {
con = (Connection)cons.nextElement();
if (con == returned) {
connections.put(con, Boolean.FALSE);
break;
}
}
}
}
!chen 2003-07-24
  • 打赏
  • 举报
回复
UP

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧