Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Architecture & Framework  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:Struts 数据源配置和使用 [Re:glistar]
arjooe





发贴: 16
积分: 0
于 2003-03-21 15:56 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
在我的Web Service中,我没有使用Struts中Action提供的getDataSource(request)方法来取得Connection, 我利用Jakarta.Commons的DBCP包写了一个Plugin,在ActionServlet初始化时将此连接池对象存在ServletContext中, 以利用它的Pooling Connection特性. 在Action中, 我们使用该对象的getConnection()方法来操作数据库.

我不知道Struts的DataSource本身是否有Connection Pool之功能, 请大家指教.

代码如下:
package bss.plugin;

/**
*
* @author pms24133
*/

import java.util.Iterator;
import java.util.Vector;
import javax.sql.DataSource;
import javax.servlet.ServletContext;

import org.apache.struts.action.Action;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ApplicationConfig;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.config.DataSourceConfig;

import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DataSourceConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDataSource;

public class DBCPplugin implements PlugIn {

private ActionServlet servlet = null;

private Vector dbcpvtr = new Vector();

private String[] dskey = null;

public void destroy(){
// Remove all physical connections represented by datasource stored in ServletContex.

Iterator itr = dbcpvtr.iterator();
while(itr.hasNext()){
try{
GenericObjectPool gop = (GenericObjectPool)itr.next();
gop.close();
gop = null;
}catch(Exception e){
servlet.log("System errors::::" + e);
}finally{
}
}

this.dbcpvtr = null;

for(int i =0; i<dskey.length; i++){
PoolingDataSource pds = (PoolingDataSource) (servlet.getServletContext().
getAttribute(dskey[i]));
pds = null;
}

dskey = null;
}

public void init(ActionServlet servlet,ApplicationConfig config){
// Data Sources configed in /WEB-INF/struts-config.xml,
// and can be retrieved through config.findDatasourceConfigs() in DataSourceConfig object format;
// We assign data from these configs to DBCP config.
this.servlet = servlet;

DataSourceConfig dscs[] = config.findDataSourceConfigs();

if (dscs == null) {
dscs = new DataSourceConfig[0];
servlet.log("No DataSource configed yet.");
}

this.dskey = new String[dscs.length];

// Process each DataSource config according key.
for( int i = 0; i < dscs.length; i++){
// Retrieve each DataSource from ActionServlet Context,
DataSource ds = (DataSource) servlet.getServletConfig().getServletContext().getAttribute( dscs[i].getKey() );

// Define a connection Pool for this Data Source definition,

//ObjectPool connectionPool = new GenericObjectPool(connectionFactory);
ObjectPool connectionPool = new GenericObjectPool(null);

// Construct a Conneciton Factory for this Data Source,
ConnectionFactory connectionFactory =
new DataSourceConnectionFactory(ds);

// Construct connectionpool with connection factory

try{

PoolableConnectionFactory poolableConnectionFactory =
new PoolableConnectionFactory(connectionFactory,
connectionPool,
null,
null,
false,
true);

/*
* Parameter definition of constructor method of PoolableConnectionFactory
*
* connectionFactory - the ConnectionFactory from which to obtain base Connections
* connectionPool - the ObjectPool in which to pool those Connections
* stmtPoolFactory - the KeyedObjectPoolFactory to use to create KeyedObjectPools for pooling PreparedStatements, or null to disable PreparedStatement pooling
* validationQuery - a query to use to validate Connections. Should return at least one row. May be null
* defaultReadOnly - the default "read only" setting for borrowed Connections
* defaultAutoCommit - the default "auto commit" setting for returned Connections
*/

PoolingDataSource dataSource = new PoolingDataSource(connectionPool);

// Store connectionpool to Application Context for further globle use;
servlet.getServletContext().setAttribute( dscs[i].getKey(),dataSource);
dbcpvtr.add(connectionPool);

servlet.log("Store org.apache.commons.dbcp.PoolingDataSource with key '"+ dscs[i].getKey() + "' to Application Context.");
}catch(Exception e){
servlet.log("Initiating Datasource error: " + e.toString() );
}
}
}//End of init method

}//End of class



Arjooe

菜鸟请教,基本程序调试不通,急急急急急急!!!

话题树型展开
人气 标题 作者 字数 发贴时间
19566 Struts 数据源配置和使用 glistar 2503 2003-03-17 11:12
11802 Re:Struts 数据源配置和使用 dapan 31 2003-03-17 12:35
11573 Re:Struts 数据源配置和使用 floater 275 2003-03-19 03:39
11791 Re:Struts 数据源配置和使用 glistar 171 2003-03-19 09:13
11393 Re:Struts 数据源配置和使用 floater 433 2003-03-25 01:13
13197 Re:Struts 数据源配置和使用 arjooe 5287 2003-03-21 15:56
11515 Re:Struts 数据源配置和使用 glistar 76 2003-03-21 16:02
11547 Re:Struts 数据源配置和使用 arjooe 3 2003-03-21 17:21
12285 Re:Struts 数据源配置和使用 lihongtao 21 2003-03-25 09:04

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923