我做c/s啊想用连接池啊!!!!!!!!!!!!但是没找到相关资料啊

girl55 2007-06-15 06:24:31
我用

cs java 连接池

baidu
google都没有找到相关的资料啊

555
...全文
312 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
geggegeda 2007-12-26
  • 打赏
  • 举报
回复
up
lcllcl987 2007-07-03
  • 打赏
  • 举报
回复
ibatis阿:
你看看它的配置文件就明白了:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">

<sqlMapConfig>

<settings cacheModelsEnabled="true" enhancementEnabled="true"
lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="32"
maxSessions="10" maxTransactions="5" useStatementNamespaces="false" />

<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<!--
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />
<property name="JDBC.ConnectionURL"
value="jdbc:mysql://192.168.0.107/tn3270" />
-->
<property name="JDBC.Driver" value="org.hsqldb.jdbcDriver" />
<property name="JDBC.ConnectionURL"
value="jdbc:hsqldb:hsql://192.168.0.107" />
<property name="JDBC.Username" value="sa" />
<property name="JDBC.Password" value="" />
<property name="Pool.MaximumActiveConnections" value="10" />
<property name="Pool.MaximumIdleConnections" value="5" />
<property name="Pool.MaximumCheckoutTime" value="120000" />
<property name="Pool.TimeToWait" value="500" />
<property name="Pool.PingQuery"
value="select 1 from sample" />
<property name="Pool.PingEnabled" value="false" />
<property name="Pool.PingConnectionsOlderThan" value="1" />
<property name="Pool.PingConnectionsNotUsedFor" value="1" />
</dataSource>
</transactionManager>

<sqlMap resource="co/tn3270/gateway/db/GateWayDB.xml" />
<!--<sqlMap url="file:///D:\\project\\Smart3270\\TnGateway\\GateWayDB.xml" />-->
</sqlMapConfig>
dangjisheng 2007-06-20
  • 打赏
  • 举报
回复
http://www.blogjava.net/javajohn/archive/2006/07/17/58532.html这个应该可以用到CS端吧,加载代码就行了吧
girl55 2007-06-20
  • 打赏
  • 举报
回复
http://www.open-open.com/20.htm


Proxool
这里面很多b/s的吧?
我要的是c/s的啊......

555



loveyousomuch 2007-06-20
  • 打赏
  • 举报
回复
看看这个

http://www.open-open.com/20.htm
girl55 2007-06-20
  • 打赏
  • 举报
回复
loveyousomuch

那里有开源的啊
girl55 2007-06-20
  • 打赏
  • 举报
回复
还有吗?

感谢shmilylee

但是这个不好用
总是莫名其妙出问题啊
girl55 2007-06-20
  • 打赏
  • 举报
回复
暗暗暗暗暗暗暗暗啊
shmilylee 2007-06-16
  • 打赏
  • 举报
回复
通常商业数据库JDBC驱动都会提供自已的数据库连接池管理器,无需自己制做。
无论如何,这里给出一个简单实现

实现连接池的关键点:
1. 单实例
2. 由实现类来管理数据库连接,包括连接,刷新
3. 重载java.sql.Connection的close功能。当客户程序调用close时不是真正的断开连接,而是将连接归还连接池管理器。
4. 将数据库属性独立于管理器本身

可选:
1. 实现javax.sql.PooledConnection
2. 实现javax.sql.DataSource
3. 实现javax.sql.ConnectionPoolDataSource

一个简单实现:
import java.io.*;
import java.util.*;
import java.sql.*;

public class ConnectionPool implements Runnable {
static String configFileName; // 如./connection-pool.properties
static Properties config;
private static List list = null;
private static boolean isCreate = false;
/**
* 实现单实例,关键点1
* @throws SQLException
*/
public ConnectionPool () throws SQLException {
if (list == null) init ();
if (!isCreate) {
Thread thread = new Thread(this);
thread.run();
isCreate = true;
}
}

public synchronized Connection getConnection () {
if (list == null) return null;

// 寻找空闲的连接
for (int i = 0; i < list.size(); i ++){
ConnectionWrap conn = (ConnectionWrap)list.get(i);
if (conn.free) {
conn.free = false;
conn.start = System.currentTimeMillis();
return conn;
}
}

// 没有空闲的连接则自动扩展
int step = Integer.parseInt(config.getProperty("step")); // 取得自动扩展的步长
this.addConnection(step);
return (ConnectionWrap)list.get(list.size() - step - 1);
}

public void run () {
long interval = Long.parseLong(config.getProperty("interval")); // 扫描时间
while (true) {
try {
Thread.sleep(interval);
} catch (Exception e) {
// 出错处理
}
scan ();
}
}

/**
* 关键点2,管理连接。一些超时的连接刷新
*/
private void scan () {
int timeout = Integer.parseInt(config.getProperty("timeout"));
for (int i = 0; i < list.size(); i ++) {
ConnectionWrap conn = (ConnectionWrap) list.get(i);
if (conn.start >; 0) {
time = System.currentTimeMillis() - conn.start;
if (time >;= timeout)
conn.close();
}
}

int initCount = Integer.parseInt(config.getProperty("init-count"));
if (list.size() >; initCount) { // 恢复到初始连接数
for (int i = list.size() - 1; i >; initCount; i --) {
ConnectionWrap conn = (ConnectionWrap) list.get(i);
if (conn.free) { // 真正地断开连接
try {
conn.conn.close();
} catch (SQLException ignore){}
list.remove(conn);
}
}
}
}

private void init () throws SQLException {
config = readConfig ();
createConnectionPool (config);
}

/**
* 读取配置文件
* @return java.util.Properties
* @throws SQLException
*/
private void readConfig () throws SQLException {
InputStream in = null;
try {
in = new FileInputStream (configFileName);
config = new Properties ();
config.load(in);
} catch (IOException ioe) { // 出错处理
throw new SQLException (ioe.getMessage());
} finally {
if (in != null)
try { in.close(); } catch (IOException ignore) {}
}
}

private void createConnectionPool () throws SQLException {
String driverName = config.getProperty("driver-name");

int initCount = Integer.parseInt(config.getProperty("init-count"));
int maxCount = Integer.parseInt(config.getProperty("max-count"));
try {
Driver driver = Class.forName(driverName);
addConnection (initCount);
} catch (Exception e) {
throw new SQLException (e.getMessage());
}
}

private void addConnection (int count) throws SQLException {
if (list == null) list = new ArrayList (count);
String url = config.getProperty("url");
String user = config.getProperty("user");
String password = config.getProperty("password");

for (int i = 0; i < count; i ++)
list.add(new ConnectionWrap (url, user, password));
}

class ConnectionWrap implements Connection {
Connection conn = null;
boolean free;
long start;
ConnectionWrap (String url, String user, String password) throws Exception {
Connection conn = DriverManager.getConnection(url, user, password);
free = true;
}

/**
* 这里关键点3,并不真正断开连接,而是归还给管理器
* @return
* @throws SQLException
*/
public boolean close () throws SQLException {
free = true;
start = 0;
}

// 其他java.sql.Connection的方法
// ...
}
}

配置文件示例
driver-name = oracle.jdbc.driver.Driver
url = jdbc:oracle:thin:@host:1521:employee
user = scott
password = tiger
timeout = 60000 # 一分钟
init-count = 10
step = 5
interval = 1000 # 1秒
...

以后在客户端可以这么调用
ConnectionPool pool = new ConnectionPool ();
Connection conn = null;
try {
conn = pool.getConnection ();
// 其他操作
} finally {
if (conn != null) conn.close ();
}

限于篇幅,javax.sql.DataSource, javax.sql.ConnectionPoolDataSource等就不一一列出了

转载於http://www.chinaunix.net/jh/26/30066.html seth的回复!
不是本人写的,google了一下,不知道对你有没有帮助!
loveyousomuch 2007-06-16
  • 打赏
  • 举报
回复
直接找个开源的不得了?

yu_mylove 2007-06-15
  • 打赏
  • 举报
回复
关注......
liujun999999 2007-06-15
  • 打赏
  • 举报
回复
楼主你理解连接池的机制吗,如果理解的话可以自己设计解决
内容概要:本文介绍了一个基于MATLAB R2025b平台的无人机三维路径规划项目,提出将长短期记忆网络(LSTM)与快速扩展随机树(RRT)相结合的LSTM-RRT混合算法。通过构建三维体素环境模型,利用LSTM学习历史路径序列和局部环境特征,预测采样方向以引导RRT的搜索过程,从而提升路径规划的效率、路径质量和对复杂环境的适应能力。文中详细阐述了三维环境建模、LSTM网络结构设计、RRT采样机制以及两者融合策略,并提供了关键模块的MATLAB代码示例,包括环境构建、网络训练、融合采样、路径回溯与三维可视化等。; 适合人群:具备一定MATLAB编程基础和机器学习基础知识,从事无人机导航、路径规划、智能控制等相关领域的科研人员及工程技术人员。; 使用场景及目标:①在城市楼宇、山地等复杂三维环境中为无人机规划安全、高效、平滑的飞行路径;②研究深度学习与传统采样算法的融合机制,提升RRT类算法的智能性与实用性;③作为智能路径规划的教学案例或工程原型,支持算法快速验证与二次开发。; 阅读建议:此资源侧重于算法原理与工程实现的结合,建议读者结合提供的代码示例,深入理解LSTM与RRT的交互逻辑,并在MATLAB环境中动手调试与运行,以掌握其在三维空间中的动态规划过程与可视化效果。

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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